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

@@ -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()