- 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
|
@ -4,10 +4,10 @@ import Role from 'App/Models/Role';
|
|||
import type { ModelQueryBuilderContract } from '@ioc:Adonis/Lucid/Orm';
|
||||
import CreateUserValidator from 'App/Validators/CreateUserValidator';
|
||||
import UpdateUserValidator from 'App/Validators/UpdateUserValidator';
|
||||
import { RenderResponse } from '@ioc:EidelLev/Inertia';
|
||||
// import { RenderResponse } from '@ioc:EidelLev/Inertia';
|
||||
// import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
||||
// import Hash from '@ioc:Adonis/Core/Hash';
|
||||
// import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
||||
import Hash from '@ioc:Adonis/Core/Hash';
|
||||
import { schema, rules } from '@ioc:Adonis/Core/Validator';
|
||||
|
||||
export default class UsersController {
|
||||
public async index({ auth, request, inertia }: HttpContextContract) {
|
||||
|
@ -163,87 +163,6 @@ export default class UsersController {
|
|||
return response.redirect().toRoute('user.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the user a form to change their personal information & password.
|
||||
*
|
||||
* @return — \Inertia\Response
|
||||
*/
|
||||
public accountInfo({ inertia, auth }: HttpContextContract): RenderResponse {
|
||||
const user = auth.user;
|
||||
// const id = request.param('id');
|
||||
// const user = await User.query().where('id', id).firstOrFail();
|
||||
|
||||
return inertia.render('Admin/User/AccountInfo', {
|
||||
user: user,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the modified personal information for a user.
|
||||
*
|
||||
* @param HttpContextContract ctx
|
||||
* @return : RedirectContract
|
||||
*/
|
||||
public async accountInfoStoreOld({ request, response, auth, session }: HttpContextContract) {
|
||||
// validate update form
|
||||
await request.validate(UpdateUserValidator);
|
||||
|
||||
const payload = request.only(['login', 'email']);
|
||||
auth.user?.merge(payload);
|
||||
const user = await auth.user?.save();
|
||||
// $user = \Auth::user()->update($request->except(['_token']));
|
||||
let message;
|
||||
if (user) {
|
||||
message = 'Account updated successfully.';
|
||||
} else {
|
||||
message = 'Error while saving. Please try again.';
|
||||
}
|
||||
|
||||
session.flash(message);
|
||||
return response.redirect().toRoute('admin.account.info');
|
||||
//->with('message', __($message));
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
// private async syncRoles(userId: number, roleIds: Array<number>) {
|
||||
// const user = await User.findOrFail(userId)
|
||||
// // const roles: Role[] = await Role.query().whereIn('id', roleIds);
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue