- removed all controller methods from 'app/Controlles/Http/Admin/UsersControllers.ts' - merged all authentication methods inside 'app/Controllers/Http/Auth/UserController.ts'
This commit is contained in:
parent
68928b5e07
commit
4efa53673f
7 changed files with 133 additions and 187 deletions
|
@ -1,10 +1,9 @@
|
|||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
||||
import User from 'App/Models/User';
|
||||
// import Hash from '@ioc:Adonis/Core/Hash';
|
||||
// import InvalidCredentialException from 'App/Exceptions/InvalidCredentialException';
|
||||
// import AuthValidator from 'App/Validators/AuthValidator';
|
||||
import { RenderResponse } from '@ioc:EidelLev/Inertia';
|
||||
import TwoFactorAuthProvider from 'App/Services/TwoFactorAuthProvider';
|
||||
import Hash from '@ioc:Adonis/Core/Hash';
|
||||
import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
||||
|
||||
// Here we are generating secret and recovery codes for the user that’s enabling 2FA and storing them to our database.
|
||||
export default class UserController {
|
||||
|
@ -26,6 +25,46 @@ export default class UserController {
|
|||
});
|
||||
}
|
||||
|
||||
public async accountInfoStore({ auth, request, response, session }) {
|
||||
const passwordSchema = schema.create({
|
||||
old_password: schema.string({ trim: true }, [rules.required()]),
|
||||
new_password: schema.string({ trim: true }, [rules.minLength(8), rules.maxLength(255), rules.confirmed('confirm_password')]),
|
||||
confirm_password: schema.string({ trim: true }, [rules.required()]),
|
||||
});
|
||||
try {
|
||||
await request.validate({ schema: passwordSchema });
|
||||
} catch (error) {
|
||||
// return response.badRequest(error.messages);
|
||||
throw error;
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await auth.user;
|
||||
const { old_password, new_password } = request.only(['old_password', 'new_password']);
|
||||
|
||||
// if (!(old_password && new_password && confirm_password)) {
|
||||
// return response.status(400).send({ warning: 'Old password and new password are required.' });
|
||||
// }
|
||||
|
||||
// Verify if the provided old password matches the user's current password
|
||||
const isSame = await Hash.verify(user.password, old_password);
|
||||
if (!isSame) {
|
||||
return response.flash({ warning: 'Old password is incorrect.' }).redirect().back();
|
||||
}
|
||||
|
||||
// Hash the new password before updating the user's password
|
||||
user.password = new_password;
|
||||
await user.save();
|
||||
|
||||
// return response.status(200).send({ message: 'Password updated successfully.' });
|
||||
session.flash('Password updated successfully.');
|
||||
return response.redirect().toRoute('settings.user.index');
|
||||
} catch (error) {
|
||||
// return response.status(500).send({ message: 'Internal server error.' });
|
||||
return response.flash('warning', `Invalid server state. Internal server error.`).redirect().back();
|
||||
}
|
||||
}
|
||||
|
||||
public async enableTwoFactorAuthentication({ auth, response, session }: HttpContextContract): Promise<void> {
|
||||
// const user: User | undefined = auth?.user;
|
||||
const user = (await User.find(auth.user?.id)) as User;
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue