Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AB-0002]: google auth configuration simplified #262

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Configuration reference: http://docs.postiz.com/configuration/reference

# === Required Settings
DATABASE_URL="postgresql://postiz-user:postiz-password@localhost:5432/postiz-db-local"
DATABASE_URL="postgresql://postiz-local:postiz-local-pwd@localhost:5432/postiz-db-local"
OleksandrKucherenko marked this conversation as resolved.
Show resolved Hide resolved
REDIS_URL="redis://localhost:6379"
JWT_SECRET="random string for your JWT secret, make it long"
FRONTEND_URL="http://localhost:4200"
NEXT_PUBLIC_BACKEND_URL="http://localhost:3000"
BACKEND_INTERNAL_URL="http://localhost:3000"
#
# Hint: use command below to generate a random password and copy it to clipboard
# openssl rand -base64 32 | tr -d '\n' | pbcopy && echo "Random password copied to clipboard"
#
JWT_SECRET="random string for your JWT secret, make it long"
OleksandrKucherenko marked this conversation as resolved.
Show resolved Hide resolved

## These are dummy values, you must create your own from Cloudflare.
## Remember to set your public internet IP address in the allow-list for the API token.
## https://developers.cloudflare.com/fundamentals/api/get-started/create-token/
##
## Cloudflare is currently required to save things like social media avatars for accounts.
CLOUDFLARE_ACCOUNT_ID="QhcMSXQyPuMCRpSQcSYdEuTYgHeCXHbu"
Expand All @@ -19,7 +24,6 @@ CLOUDFLARE_BUCKETNAME="postiz"
CLOUDFLARE_BUCKET_URL="https://QhcMSXQyPuMCRpSQcSYdEuTYgHeCXHbu.r2.cloudflarestorage.com/"
CLOUDFLARE_REGION="auto"


# === Common optional Settings

## This is a dummy key, you must create your own from Resend.
Expand All @@ -30,15 +34,14 @@ CLOUDFLARE_REGION="auto"
#EMAIL_FROM_NAME=""

# Where will social media icons be saved - local or cloudflare.
STORAGE_PROVIDER="local"
STORAGE_PROVIDER="local"

# Your upload directory path if you host your files locally, otherwise Cloudflare will be used.
#UPLOAD_DIRECTORY=""

# Your upload directory path if you host your files locally, otherwise Cloudflare will be used.
#NEXT_PUBLIC_UPLOAD_STATIC_DIRECTORY=""


# Social Media API Settings
X_API_KEY=""
X_API_SECRET=""
Expand All @@ -48,8 +51,6 @@ LINKEDIN_CLIENT_ID=""
LINKEDIN_CLIENT_SECRET=""
REDDIT_CLIENT_ID=""
REDDIT_CLIENT_SECRET=""
GITHUB_CLIENT_ID=""
GITHUB_CLIENT_SECRET=""
BEEHIIVE_API_KEY=""
BEEHIIVE_PUBLICATION_ID=""
THREADS_APP_ID=""
Expand All @@ -73,6 +74,14 @@ SLACK_SIGNING_SECRET=""
MASTODON_CLIENT_ID=""
MASTODON_CLIENT_SECRET=""

# Login/Authentication Settings
GITHUB_CLIENT_ID=""
GITHUB_CLIENT_SECRET=""

# if varaible not provided we fallbase to YOUTUBE_CLIENT_ID and YOUTUBE_CLIENT_SECRET
GOOGLE_CLIENT_ID=""
GOOGLE_CLIENT_SECRET=""

# Misc Settings
OPENAI_API_KEY=""
NEXT_PUBLIC_DISCORD_SUPPORT=""
Expand Down
29 changes: 23 additions & 6 deletions apps/backend/src/services/auth/providers/google.provider.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import type { ProvidersInterface } from '@gitroom/backend/services/auth/providers.interface';
import { makeId } from '@gitroom/nestjs-libraries/services/make.is';
import type { OAuth2Client } from 'google-auth-library/build/src/auth/oauth2client';
import { google } from 'googleapis';
import { OAuth2Client } from 'google-auth-library/build/src/auth/oauth2client';
import { ProvidersInterface } from '@gitroom/backend/services/auth/providers.interface';

type EnvVar = string | undefined | null;

const coalesceES6 = (...args: EnvVar[]) =>
args.find((_) => ![null, undefined].includes(_));

const {
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
YOUTUBE_CLIENT_ID = '',
YOUTUBE_CLIENT_SECRET = '',
} = process.env;

const CLIENT_ID = [GOOGLE_CLIENT_ID, YOUTUBE_CLIENT_ID];
const CLIENT_SECRET = [GOOGLE_CLIENT_SECRET, YOUTUBE_CLIENT_SECRET];

const clientAndYoutube = () => {
const client = new google.auth.OAuth2({
clientId: process.env.YOUTUBE_CLIENT_ID,
clientSecret: process.env.YOUTUBE_CLIENT_SECRET,
const options = {
clientId: coalesceES6(...CLIENT_ID),
clientSecret: coalesceES6(...CLIENT_SECRET),
redirectUri: `${process.env.FRONTEND_URL}/integrations/social/youtube`,
});
};

const client = new google.auth.OAuth2(options);

const youtube = (newClient: OAuth2Client) =>
google.youtube({
Expand Down