From 56d7e71d9025ee1018eeacfb62ce179f7e0c6000 Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 14 Feb 2025 16:33:33 +0100 Subject: [PATCH] add username+pass login on NO_AUTH mode --- dashboard/nuxt.config.ts | 2 +- dashboard/package.json | 1 + dashboard/pages/login.vue | 88 +++++++++++++++++----------- dashboard/server/api/auth/no_auth.ts | 14 +++-- docker-compose.yml | 4 +- 5 files changed, 68 insertions(+), 41 deletions(-) diff --git a/dashboard/nuxt.config.ts b/dashboard/nuxt.config.ts index a96796c..75e129b 100644 --- a/dashboard/nuxt.config.ts +++ b/dashboard/nuxt.config.ts @@ -55,7 +55,7 @@ export default defineNuxtConfig({ 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, + NOAUTH_USER_PASS: process.env.NOAUTH_USER_PASS, MODE: process.env.MODE || 'NONE', SELFHOSTED: process.env.SELFHOSTED || 'FALSE', public: { diff --git a/dashboard/package.json b/dashboard/package.json index ec9cc53..89ecdfd 100644 --- a/dashboard/package.json +++ b/dashboard/package.json @@ -7,6 +7,7 @@ "build:prod": "npm run workspace:shared && nuxt build --dotenv .env.prod", "dev": "npm run workspace:shared && nuxt dev --dotenv .env.testmode", "dev:prod": "npm run workspace:shared && nuxi dev --dotenv .env.prod", + "dev:docker": "npm run workspace:shared && nuxi dev --dotenv .env.docker", "generate": "nuxt generate", "preview": "nuxt preview", "postinstall": "nuxt prepare", diff --git a/dashboard/pages/login.vue b/dashboard/pages/login.vue index 2518019..11bcb00 100644 --- a/dashboard/pages/login.vue +++ b/dashboard/pages/login.vue @@ -13,32 +13,6 @@ const useCodeClientWrapper = isNoAuth.value === false ? 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.user = user; - - console.log('LOGIN DONE - USER', loggedUser.user); - - 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: any) { - alert('Error during login.' + ex.message); - } -} - const { isReady, login } = useCodeClientWrapper({ onSuccess: handleOnSuccess, onError: handleOnError, }); const router = useRouter(); @@ -121,6 +95,39 @@ function goBackToEmailLogin() { password.value = ''; } +async function signInSelfhosted() { + try { + const result = await $fetch(`/api/auth/no_auth`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ email: email.value, password: password.value }) + }); + if (result.error) { + if (result.errorMessage) return alert(result.errorMessage); + 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.user = user; + + console.log('LOGIN DONE - USER', loggedUser.user); + + 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: any) { + alert('Error during login.' + ex.message); + } +} + async function signInWithCredentials() { try { @@ -176,7 +183,8 @@ async function signInWithCredentials() { Sign in -
+
Track web analytics and custom events with extreme simplicity in under 30 sec.
@@ -217,7 +225,8 @@ async function signInWithCredentials() {
-
+
@@ -247,17 +256,30 @@ async function signInWithCredentials() {
+ +
-
- + class="flex text-[1.3rem] flex-col gap-4 items-center px-8 py-3 relative z-[2]"> +
+ + + +
+
+ + Sign in +
- Continue as Admin
+ + + +
-
+
By continuing you are accepting
our diff --git a/dashboard/server/api/auth/no_auth.ts b/dashboard/server/api/auth/no_auth.ts index bafc3a1..489c38c 100644 --- a/dashboard/server/api/auth/no_auth.ts +++ b/dashboard/server/api/auth/no_auth.ts @@ -2,7 +2,7 @@ import { createUserJwt } from '~/server/AuthManager'; import { UserModel } from '@schema/UserSchema'; -const { NOAUTH_USER_EMAIL, NOAUTH_USER_NAME, public: publicRuntime } = useRuntimeConfig(); +const { NOAUTH_USER_EMAIL, NOAUTH_USER_PASS, public: publicRuntime } = useRuntimeConfig(); const noAuthMode = publicRuntime.AUTH_MODE == 'NO_AUTH'; @@ -18,11 +18,15 @@ export default defineEventHandler(async event => { return { error: true, access_token: '' } } - if (!NOAUTH_USER_NAME) { - console.error('NOAUTH_USER_NAME is required in NO_AUTH mode'); + if (!NOAUTH_USER_PASS) { + console.error('NOAUTH_USER_PASS is required in NO_AUTH mode'); return { error: true, access_token: '' } } + const body = await readBody(event); + + if (body.email != NOAUTH_USER_EMAIL || body.password != NOAUTH_USER_PASS) return { error: true, access_token: '', errorMessage: 'Username or password invalid' } + const user = await UserModel.findOne({ email: NOAUTH_USER_EMAIL }); if (user) return { @@ -35,8 +39,8 @@ export default defineEventHandler(async event => { const newUser = new UserModel({ email: NOAUTH_USER_EMAIL, - given_name: NOAUTH_USER_NAME, - name: NOAUTH_USER_NAME, + given_name: NOAUTH_USER_EMAIL.split('@')[0] || 'NONAME', + name: NOAUTH_USER_EMAIL.split('@')[0] || 'NONAME', locale: 'no-auth', picture: '', created_at: Date.now() diff --git a/docker-compose.yml b/docker-compose.yml index 360493a..c6a9cc9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -90,9 +90,9 @@ services: # NO_AUTH or GOOGLE NUXT_PUBLIC_AUTH_MODE: 'NO_AUTH' - # Default user created in NO_AUTH mode + # Credentials to login in NO_AUTH mode NUXT_NOAUTH_USER_EMAIL: 'default@user.com' - NUXT_NOAUTH_USER_NAME: "defaultuser" + NUXT_NOAUTH_USER_PASS: "litlyx123" NUXT_SELFHOSTED: 'true' NUXT_PUBLIC_SELFHOSTED: 'true'