From d3b173940bd173267c771dff8b8e0ae279a538a0 Mon Sep 17 00:00:00 2001 From: Emily Date: Wed, 3 Jul 2024 01:37:27 +0200 Subject: [PATCH] add no_auth login --- dashboard/nuxt.config.ts | 4 +- dashboard/pages/login.vue | 45 ++++++++++++++++- .../server/api/auth/google_login.post.ts | 2 - dashboard/server/api/auth/no_auth.ts | 49 +++++++++++++++++++ docker-compose.yml | 22 ++++++--- 5 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 dashboard/server/api/auth/no_auth.ts diff --git a/dashboard/nuxt.config.ts b/dashboard/nuxt.config.ts index 3265af5..93dba8e 100644 --- a/dashboard/nuxt.config.ts +++ b/dashboard/nuxt.config.ts @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'; const gooleSignInConfig: any = { googleSignIn: { - clientId: process.env.GOOGLE_AUTH_CLIENT_ID + clientId: process.env.GOOGLE_AUTH_CLIENT_ID || 'NONE' } } @@ -43,6 +43,8 @@ export default defineNuxtConfig({ STRIPE_WH_SECRET: process.env.STRIPE_WH_SECRET, STRIPE_SECRET_TEST: process.env.STRIPE_SECRET_TEST, STRIPE_WH_SECRET_TEST: process.env.STRIPE_WH_SECRET_TEST, + NOAUTH_USER_EMAIL: process.env.NOAUTH_USER_EMAIL, + NOAUTH_USER_NAME: process.env.NOAUTH_USER_NAME, public: { PAYPAL_CLIENT_ID: '' } diff --git a/dashboard/pages/login.vue b/dashboard/pages/login.vue index eb7fba3..38c2977 100644 --- a/dashboard/pages/login.vue +++ b/dashboard/pages/login.vue @@ -2,8 +2,41 @@ definePageMeta({ layout: 'none' }); +const { GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig(); -const { isReady, login } = useCodeClient({ onSuccess: handleOnSuccess, onError: handleOnError, }); +const isNoAuth = ref(GOOGLE_AUTH_CLIENT_ID == undefined); + +const useCodeClientWrapper = isNoAuth.value === true ? useCodeClient : (...args: any) => { + return { isReady: false, login: () => { } } +} + +async function loginWithoutAuth() { + try { + const result = await $fetch('/api/auth/no_auth'); + if (result.error) return alert('Error during login, please try again'); + + setToken(result.access_token); + + const user = await $fetch('/api/user/me', { headers: { 'Authorization': 'Bearer ' + token.value } }) + const loggedUser = useLoggedUser(); + loggedUser.value = user; + + console.log('LOGIN DONE - USER', loggedUser.value); + + const isFirstTime = await $fetch('/api/user/is_first_time', { headers: { 'Authorization': 'Bearer ' + token.value } }) + + if (isFirstTime === true) { + router.push('/project_creation?just_logged=true'); + } else { + router.push('/?just_logged=true'); + } + + } catch (ex) { + alert('Error during login.', ex.message); + } +} + +const { isReady, login } = useCodeClientWrapper({ onSuccess: handleOnSuccess, onError: handleOnError, }); const router = useRouter(); @@ -80,7 +113,7 @@ function handleOnError(errorResponse: any) {
-
@@ -88,6 +121,14 @@ function handleOnError(errorResponse: any) { Continue with Google
+
+
+ +
+ Continue as Admin +
+
diff --git a/dashboard/server/api/auth/google_login.post.ts b/dashboard/server/api/auth/google_login.post.ts index f11a59d..435cc43 100644 --- a/dashboard/server/api/auth/google_login.post.ts +++ b/dashboard/server/api/auth/google_login.post.ts @@ -3,8 +3,6 @@ import { OAuth2Client } from 'google-auth-library'; import { createUserJwt } from '~/server/AuthManager'; import { UserModel } from '@schema/UserSchema'; import EmailService from '@services/EmailService'; -import { ProjectModel } from '@schema/ProjectSchema'; -import StripeService from '~/server/services/StripeService'; const { GOOGLE_AUTH_CLIENT_SECRET, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig() diff --git a/dashboard/server/api/auth/no_auth.ts b/dashboard/server/api/auth/no_auth.ts new file mode 100644 index 0000000..5aa116e --- /dev/null +++ b/dashboard/server/api/auth/no_auth.ts @@ -0,0 +1,49 @@ + +import { createUserJwt } from '~/server/AuthManager'; +import { UserModel } from '@schema/UserSchema'; + +const { NOAUTH_USER_EMAIL, NOAUTH_USER_NAME, GOOGLE_AUTH_CLIENT_ID } = useRuntimeConfig(); + +const noAuthMode = GOOGLE_AUTH_CLIENT_ID.length == 0; + +export default defineEventHandler(async event => { + + if (!noAuthMode) { + console.error('Endpoint available only in NO_AUTH mode'); + return { error: true, access_token: '' } + } + + if (!NOAUTH_USER_EMAIL) { + console.error('NOAUTH_USER_EMAIL is required in NO_AUTH mode'); + return { error: true, access_token: '' } + } + + if (!NOAUTH_USER_NAME) { + console.error('NOAUTH_USER_NAME is required in NO_AUTH mode'); + return { error: true, access_token: '' } + } + + const user = await UserModel.findOne({ email: NOAUTH_USER_EMAIL }); + + if (user) return { + error: false, + access_token: createUserJwt({ + email: user.email, + name: user.name + }) + } + + const newUser = new UserModel({ + email: NOAUTH_USER_EMAIL, + given_name: NOAUTH_USER_NAME, + name: NOAUTH_USER_NAME, + locale: 'no-auth', + picture: '', + created_at: Date.now() + }); + + const savedUser = await newUser.save(); + + return { error: false, access_token: createUserJwt({ email: savedUser.email, name: savedUser.name }) } + +}); \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 0cb9cd6..89fe1ae 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -85,10 +85,16 @@ services: NUXT_AUTH_JWT_SECRET: "litlyx_jwt_secret" - # Used to register / login + # Optional - Used to register / login via google - NUXT_GOOGLE_AUTH_CLIENT_ID: "" - NUXT_GOOGLE_AUTH_CLIENT_SECRET: "" + # NUXT_GOOGLE_AUTH_CLIENT_ID: "" + # NUXT_GOOGLE_AUTH_CLIENT_SECRET: "" + + + # Default user created to login if no GOOGLE_AUTH_CLIENT_ID is provided + + NOAUTH_USER_EMAIL: 'default@user.com' + NOAUTH_USER_NAME: "defaultuser" # Optional - Used for tests @@ -99,16 +105,16 @@ services: # Optional - Stripe secret - Used to change plans of the projects - #NUXT_STRIPE_SECRET: "" - #NUXT_STRIPE_WH_SECRET: "" + # NUXT_STRIPE_SECRET: "" + # NUXT_STRIPE_WH_SECRET: "" build: dockerfile: ./dashboard/Dockerfile - args: + #args: - # Used to register / login + # Optional - Used to register / login via google - GOOGLE_AUTH_CLIENT_ID: "" + # GOOGLE_AUTH_CLIENT_ID: "" volumes: mongo-data: