mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
add password reset + password change
This commit is contained in:
10
dashboard/server/api/user/password/can_change.ts
Normal file
10
dashboard/server/api/user/password/can_change.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
|
||||
import { PasswordModel } from "@schema/PasswordSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return;
|
||||
const hasPassword = await PasswordModel.exists({ email: userData.user.email });
|
||||
if (hasPassword) return { can_change: true };
|
||||
return { can_change: false }
|
||||
});
|
||||
33
dashboard/server/api/user/password/change.post.ts
Normal file
33
dashboard/server/api/user/password/change.post.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
import crypto from 'crypto';
|
||||
import { PasswordModel } from '@schema/PasswordSchema';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const userData = getRequestUser(event);
|
||||
if (!userData?.logged) return;
|
||||
|
||||
const { old_password, new_password } = await readBody(event);
|
||||
|
||||
if (new_password.length < 6) return { error: true, message: 'Password too short' }
|
||||
|
||||
const target = await PasswordModel.findOne({ email: userData.user.email });
|
||||
if (!target) return { error: true, message: 'Internal error. User not found.' }
|
||||
|
||||
const hashOld = crypto.createHash('sha256');
|
||||
const hashedPasswordOld = hashOld.update(old_password + '_litlyx').digest('hex');
|
||||
|
||||
if (target.password !== hashedPasswordOld) {
|
||||
return { error: true, message: 'Old password not correct' }
|
||||
}
|
||||
|
||||
const hashNew = crypto.createHash('sha256');
|
||||
const hashedPasswordNew = hashNew.update(new_password + '_litlyx').digest('hex');
|
||||
|
||||
target.password = hashedPasswordNew;
|
||||
|
||||
await target.save();
|
||||
|
||||
return { error: false, message: 'Success' }
|
||||
|
||||
});
|
||||
26
dashboard/server/api/user/password/reset.ts
Normal file
26
dashboard/server/api/user/password/reset.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
import crypto from 'crypto';
|
||||
import { PasswordModel } from '@schema/PasswordSchema';
|
||||
import EmailService from '@services/EmailService'
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const { email } = await readBody(event);
|
||||
|
||||
const target = await PasswordModel.findOne({ email });
|
||||
if (!target) return { error: true, message: 'Internal error. User not found.' }
|
||||
|
||||
|
||||
const newPass = crypto.randomBytes(5).toString('hex');
|
||||
|
||||
const hash = crypto.createHash('sha256');
|
||||
const hashedPassword = hash.update(newPass + '_litlyx').digest('hex');
|
||||
|
||||
target.password = hashedPassword;
|
||||
await target.save();
|
||||
|
||||
await EmailService.sendResetPasswordEmail(email, newPass);
|
||||
|
||||
return { error: false, message: 'Password changed' }
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user