add email login

This commit is contained in:
Emily
2024-10-10 15:49:55 +02:00
parent 0a7f2b58d0
commit 80e3b0caa9
14 changed files with 531 additions and 27 deletions

View File

@@ -0,0 +1,24 @@
import { createUserJwt } from '~/server/AuthManager';
import { UserModel } from '@schema/UserSchema';
import crypto from 'crypto';
import { PasswordModel } from '@schema/PasswordSchema';
export default defineEventHandler(async event => {
const { email, password } = await readBody(event);
const user = await UserModel.findOne({ email });
if (!user) return { error: true, message: 'Email or Password wrong' }
const hash = crypto.createHash('sha256');
const hashedPassword = hash.update(password + '_litlyx').digest('hex');
const target = await PasswordModel.findOne({ email, password: hashedPassword });
if (!target) return { error: true, message: 'Email or Password wrong' }
return { error: false, access_token: createUserJwt({ email: target.email, name: user.name }) }
});