add username+pass login on NO_AUTH mode

This commit is contained in:
Emily
2025-02-14 16:33:33 +01:00
parent 30229d4b97
commit 56d7e71d90
5 changed files with 68 additions and 41 deletions

View File

@@ -55,7 +55,7 @@ export default defineNuxtConfig({
STRIPE_SECRET_TEST: process.env.STRIPE_SECRET_TEST, STRIPE_SECRET_TEST: process.env.STRIPE_SECRET_TEST,
STRIPE_WH_SECRET_TEST: process.env.STRIPE_WH_SECRET_TEST, STRIPE_WH_SECRET_TEST: process.env.STRIPE_WH_SECRET_TEST,
NOAUTH_USER_EMAIL: process.env.NOAUTH_USER_EMAIL, 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', MODE: process.env.MODE || 'NONE',
SELFHOSTED: process.env.SELFHOSTED || 'FALSE', SELFHOSTED: process.env.SELFHOSTED || 'FALSE',
public: { public: {

View File

@@ -7,6 +7,7 @@
"build:prod": "npm run workspace:shared && nuxt build --dotenv .env.prod", "build:prod": "npm run workspace:shared && nuxt build --dotenv .env.prod",
"dev": "npm run workspace:shared && nuxt dev --dotenv .env.testmode", "dev": "npm run workspace:shared && nuxt dev --dotenv .env.testmode",
"dev:prod": "npm run workspace:shared && nuxi dev --dotenv .env.prod", "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", "generate": "nuxt generate",
"preview": "nuxt preview", "preview": "nuxt preview",
"postinstall": "nuxt prepare", "postinstall": "nuxt prepare",

View File

@@ -13,32 +13,6 @@ const useCodeClientWrapper = isNoAuth.value === false ?
return { isReady: false, login: () => { } } 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<any>('/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<boolean>('/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 { isReady, login } = useCodeClientWrapper({ onSuccess: handleOnSuccess, onError: handleOnError, });
const router = useRouter(); const router = useRouter();
@@ -121,6 +95,39 @@ function goBackToEmailLogin() {
password.value = ''; 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<any>('/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<boolean>('/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() { async function signInWithCredentials() {
try { try {
@@ -176,7 +183,8 @@ async function signInWithCredentials() {
Sign in Sign in
</div> </div>
<div class="text-lyx-lightmode-text/80 dark:text-lyx-text/80 text-[1.2rem] font-light text-center w-[70%] poppins mt-2"> <div
class="text-lyx-lightmode-text/80 dark:text-lyx-text/80 text-[1.2rem] font-light text-center w-[70%] poppins mt-2">
Track web analytics and custom events Track web analytics and custom events
with extreme simplicity in under 30 sec. with extreme simplicity in under 30 sec.
<br> <br>
@@ -217,7 +225,8 @@ async function signInWithCredentials() {
</div> </div>
<div v-if="!isNoAuth && !isEmailLogin" class="flex flex-col text-lyx-lightmode-text dark:text-lyx-text gap-2"> <div v-if="!isNoAuth && !isEmailLogin"
class="flex flex-col text-lyx-lightmode-text dark:text-lyx-text gap-2">
<div @click="login" <div @click="login"
class="hover:bg-lyx-primary bg-white dark:bg-transparent cursor-pointer flex text-[1.3rem] gap-4 items-center border-[1px] border-gray-400 rounded-lg px-8 py-3 relative z-[2]"> class="hover:bg-lyx-primary bg-white dark:bg-transparent cursor-pointer flex text-[1.3rem] gap-4 items-center border-[1px] border-gray-400 rounded-lg px-8 py-3 relative z-[2]">
@@ -247,17 +256,30 @@ async function signInWithCredentials() {
</div> </div>
<div v-if="isNoAuth" @click="loginWithoutAuth" <div v-if="isNoAuth" @click="loginWithoutAuth"
class="hover:bg-accent cursor-pointer flex text-[1.3rem] gap-4 items-center border-[1px] border-gray-400 rounded-lg px-8 py-3 relative z-[2]"> class="flex text-[1.3rem] flex-col gap-4 items-center px-8 py-3 relative z-[2]">
<div class="flex items-center"> <div class="flex flex-col gap-4 z-[100] w-[20vw] min-w-[20rem]">
<i class="far fa-crown"></i> <LyxUiInput class="px-3 py-2" placeholder="Email" v-model="email"></LyxUiInput>
<LyxUiInput class="px-3 py-2" placeholder="Password" v-model="password" type="password">
</LyxUiInput>
</div>
<div class="flex justify-center mt-4 z-[100]">
<LyxUiButton @click="signInSelfhosted()" class="text-center" type="primary">
Sign in
</LyxUiButton>
</div> </div>
Continue as Admin
</div> </div>
</div> </div>
<div class="text-[.9rem] poppins mt-20 text-lyx-lightmode-text-dark dark:text-lyx-text-dark text-center relative z-[2]"> <div
class="text-[.9rem] poppins mt-20 text-lyx-lightmode-text-dark dark:text-lyx-text-dark text-center relative z-[2]">
By continuing you are accepting By continuing you are accepting
<br> <br>
our our

View File

@@ -2,7 +2,7 @@
import { createUserJwt } from '~/server/AuthManager'; import { createUserJwt } from '~/server/AuthManager';
import { UserModel } from '@schema/UserSchema'; 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'; const noAuthMode = publicRuntime.AUTH_MODE == 'NO_AUTH';
@@ -18,11 +18,15 @@ export default defineEventHandler(async event => {
return { error: true, access_token: '' } return { error: true, access_token: '' }
} }
if (!NOAUTH_USER_NAME) { if (!NOAUTH_USER_PASS) {
console.error('NOAUTH_USER_NAME is required in NO_AUTH mode'); console.error('NOAUTH_USER_PASS is required in NO_AUTH mode');
return { error: true, access_token: '' } 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 }); const user = await UserModel.findOne({ email: NOAUTH_USER_EMAIL });
if (user) return { if (user) return {
@@ -35,8 +39,8 @@ export default defineEventHandler(async event => {
const newUser = new UserModel({ const newUser = new UserModel({
email: NOAUTH_USER_EMAIL, email: NOAUTH_USER_EMAIL,
given_name: NOAUTH_USER_NAME, given_name: NOAUTH_USER_EMAIL.split('@')[0] || 'NONAME',
name: NOAUTH_USER_NAME, name: NOAUTH_USER_EMAIL.split('@')[0] || 'NONAME',
locale: 'no-auth', locale: 'no-auth',
picture: '', picture: '',
created_at: Date.now() created_at: Date.now()

View File

@@ -90,9 +90,9 @@ services:
# NO_AUTH or GOOGLE # NO_AUTH or GOOGLE
NUXT_PUBLIC_AUTH_MODE: 'NO_AUTH' 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_EMAIL: 'default@user.com'
NUXT_NOAUTH_USER_NAME: "defaultuser" NUXT_NOAUTH_USER_PASS: "litlyx123"
NUXT_SELFHOSTED: 'true' NUXT_SELFHOSTED: 'true'
NUXT_PUBLIC_SELFHOSTED: 'true' NUXT_PUBLIC_SELFHOSTED: 'true'