mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-11 16:28:37 +01:00
add email login
This commit is contained in:
24
dashboard/server/api/auth/login.post.ts
Normal file
24
dashboard/server/api/auth/login.post.ts
Normal 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 }) }
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user