2024-03-14 19:25:27 +00:00
|
|
|
|
import type { HttpContext } from '@adonisjs/core/http';
|
2024-04-30 09:50:50 +00:00
|
|
|
|
import User from '#models/user';
|
2024-03-14 19:25:27 +00:00
|
|
|
|
// import { RenderResponse } from '@ioc:EidelLev/Inertia';
|
2024-04-23 17:36:45 +00:00
|
|
|
|
import TwoFactorAuthProvider from '#app/services/TwoFactorAuthProvider';
|
2024-03-14 19:25:27 +00:00
|
|
|
|
import hash from '@adonisjs/core/services/hash';
|
2024-05-16 11:47:06 +00:00
|
|
|
|
// import { schema, rules } from '@adonisjs/validator';
|
|
|
|
|
import vine, { SimpleMessagesProvider } from '@vinejs/vine';
|
2023-12-29 14:54:49 +00:00
|
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
|
/**
|
|
|
|
|
* Show the user a form to change their personal information & password.
|
|
|
|
|
*
|
|
|
|
|
* @return — \Inertia\Response
|
|
|
|
|
*/
|
2024-03-14 19:25:27 +00:00
|
|
|
|
public async accountInfo({ inertia, auth }: HttpContext) {
|
2023-12-29 14:54:49 +00:00
|
|
|
|
// const user = auth.user;
|
|
|
|
|
const user = (await User.find(auth.user?.id)) as User;
|
|
|
|
|
// const id = request.param('id');
|
|
|
|
|
// const user = await User.query().where('id', id).firstOrFail();
|
|
|
|
|
|
|
|
|
|
return inertia.render('Auth/AccountInfo', {
|
|
|
|
|
user: user,
|
|
|
|
|
twoFactorEnabled: user.isTwoFactorEnabled,
|
2024-01-19 14:33:46 +00:00
|
|
|
|
// code: await TwoFactorAuthProvider.generateQrCode(user),
|
2023-12-29 14:54:49 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
|
public async accountInfoStore({ auth, request, response, session }: HttpContext) {
|
2024-05-16 11:47:06 +00:00
|
|
|
|
// 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()]),
|
|
|
|
|
// });
|
|
|
|
|
const passwordSchema = vine.object({
|
|
|
|
|
// first step
|
|
|
|
|
old_password: vine
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.regex(/^[a-zA-Z0-9]+$/),
|
|
|
|
|
new_password: vine.string().confirmed({ confirmationField: 'confirm_password' }).trim().minLength(8).maxLength(255),
|
2024-02-06 15:39:33 +00:00
|
|
|
|
});
|
|
|
|
|
try {
|
2024-05-16 11:47:06 +00:00
|
|
|
|
// await request.validate({ schema: passwordSchema });
|
|
|
|
|
const validator = vine.compile(passwordSchema);
|
|
|
|
|
await request.validateUsing(validator);
|
2024-02-06 15:39:33 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
// return response.badRequest(error.messages);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2024-03-14 19:25:27 +00:00
|
|
|
|
const user = await auth.user as User;
|
2024-02-06 15:39:33 +00:00
|
|
|
|
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
|
2024-03-14 19:25:27 +00:00
|
|
|
|
const isSame = await hash.verify(user.password, old_password);
|
2024-02-06 15:39:33 +00:00
|
|
|
|
if (!isSame) {
|
2024-03-14 19:25:27 +00:00
|
|
|
|
return response.flash('warning', 'Old password is incorrect.').redirect().back();
|
2024-02-06 15:39:33 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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.' });
|
2024-03-14 19:25:27 +00:00
|
|
|
|
session.flash({ message: 'Password updated successfully.' });
|
2024-05-16 11:47:06 +00:00
|
|
|
|
return response.redirect().toRoute('settings.user');
|
2024-02-06 15:39:33 +00:00
|
|
|
|
} catch (error) {
|
|
|
|
|
// return response.status(500).send({ message: 'Internal server error.' });
|
|
|
|
|
return response.flash('warning', `Invalid server state. Internal server error.`).redirect().back();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
|
public async enableTwoFactorAuthentication({ auth, response, session }: HttpContext): Promise<void> {
|
2023-12-29 14:54:49 +00:00
|
|
|
|
// const user: User | undefined = auth?.user;
|
|
|
|
|
const user = (await User.find(auth.user?.id)) as User;
|
|
|
|
|
|
|
|
|
|
user.twoFactorSecret = TwoFactorAuthProvider.generateSecret(user);
|
|
|
|
|
user.twoFactorRecoveryCodes = await TwoFactorAuthProvider.generateRecoveryCodes();
|
|
|
|
|
await user.save();
|
|
|
|
|
|
|
|
|
|
session.flash('message', 'Two factor authentication enabled.');
|
|
|
|
|
return response.redirect().back();
|
|
|
|
|
// return inertia.render('Auth/AccountInfo', {
|
|
|
|
|
// // status: {
|
|
|
|
|
// // type: 'success',
|
|
|
|
|
// // message: 'Two factor authentication enabled.',
|
|
|
|
|
// // },
|
|
|
|
|
// user: user,
|
|
|
|
|
// twoFactorEnabled: user.isTwoFactorEnabled,
|
|
|
|
|
// code: await TwoFactorAuthProvider.generateQrCode(user),
|
|
|
|
|
// recoveryCodes: user.twoFactorRecoveryCodes,
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
|
public async disableTwoFactorAuthentication({ auth, response, session }: HttpContext): Promise<void> {
|
2024-04-25 13:17:22 +00:00
|
|
|
|
const user: User | undefined = auth.user;
|
2023-12-29 14:54:49 +00:00
|
|
|
|
|
2024-04-25 13:17:22 +00:00
|
|
|
|
if (user) {
|
|
|
|
|
user.twoFactorSecret = null;
|
|
|
|
|
user.twoFactorRecoveryCodes = null;
|
|
|
|
|
await user.save();
|
|
|
|
|
session.flash('message', 'Two-factor authentication disabled.');
|
|
|
|
|
} else {
|
|
|
|
|
session.flash('error', 'User not found.');
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 14:54:49 +00:00
|
|
|
|
return response.redirect().back();
|
|
|
|
|
// return inertia.render('Auth/AccountInfo', {
|
|
|
|
|
// // status: {
|
|
|
|
|
// // type: 'success',
|
|
|
|
|
// // message: 'Two factor authentication disabled.',
|
|
|
|
|
// // },
|
|
|
|
|
// user: user,
|
|
|
|
|
// twoFactorEnabled: user.isTwoFactorEnabled,
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public async fetchRecoveryCodes({ auth, view }) {
|
|
|
|
|
// const user = auth?.user;
|
|
|
|
|
|
|
|
|
|
// return view.render('pages/settings', {
|
|
|
|
|
// twoFactorEnabled: user.isTwoFactorEnabled,
|
|
|
|
|
// recoveryCodes: user.twoFactorRecoveryCodes,
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
}
|