This commit is contained in:
parent
f828ca4491
commit
cb51a4136f
167 changed files with 21485 additions and 21212 deletions
|
@ -1,16 +1,20 @@
|
|||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
||||
import User from 'App/Models/User';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
import User from '#models/User';
|
||||
// import Hash from '@ioc:Adonis/Core/Hash';
|
||||
// import InvalidCredentialException from 'App/Exceptions/InvalidCredentialException';
|
||||
import AuthValidator from 'App/Validators/AuthValidator';
|
||||
import AuthValidator from '#app/Validators/AuthValidator';
|
||||
|
||||
import TwoFactorAuthProvider from 'App/Services/TwoFactorAuthProvider';
|
||||
import TwoFactorAuthProvider from '#app/Services/TwoFactorAuthProvider';
|
||||
// import { Authenticator } from '@adonisjs/auth';
|
||||
// import { LoginState } from 'Contracts/enums';
|
||||
// import { StatusCodes } from 'http-status-codes';
|
||||
|
||||
// interface MyHttpsContext extends HttpContext {
|
||||
// auth: Authenticator<User>
|
||||
// }
|
||||
export default class AuthController {
|
||||
// login function
|
||||
public async login({ request, response, auth, session }: HttpContextContract) {
|
||||
// login function{ request, auth, response }:HttpContext
|
||||
public async login({ request, response, auth, session }: HttpContext) {
|
||||
// console.log({
|
||||
// registerBody: request.body(),
|
||||
// });
|
||||
|
@ -25,7 +29,9 @@ export default class AuthController {
|
|||
// // attempt to verify credential and login user
|
||||
// await auth.use('web').attempt(email, plainPassword);
|
||||
|
||||
const user = await auth.use('web').verifyCredentials(email, password);
|
||||
// const user = await auth.use('web').verifyCredentials(email, password);
|
||||
const user = await User.verifyCredentials(email, password)
|
||||
|
||||
if (user.isTwoFactorEnabled) {
|
||||
// session.put("login.id", user.id);
|
||||
// return view.render("pages/two-factor-challenge");
|
||||
|
@ -38,8 +44,9 @@ export default class AuthController {
|
|||
// state: state,
|
||||
// new_user_id: user.id,
|
||||
// });
|
||||
}
|
||||
await auth.login(user);
|
||||
}
|
||||
|
||||
await auth.use('web').login(user);
|
||||
} catch (error) {
|
||||
// if login fails, return vague form message and redirect back
|
||||
session.flash('message', 'Your username, email, or password is incorrect');
|
||||
|
@ -50,7 +57,7 @@ export default class AuthController {
|
|||
response.redirect('/apps/dashboard');
|
||||
}
|
||||
|
||||
public async twoFactorChallenge({ request, session, auth, response }) {
|
||||
public async twoFactorChallenge({ request, session, auth, response }: HttpContext) {
|
||||
const { code, recoveryCode, login_id } = request.only(['code', 'recoveryCode', 'login_id']);
|
||||
// const user = await User.query().where('id', session.get('login.id')).firstOrFail();
|
||||
const user = await User.query().where('id', login_id).firstOrFail();
|
||||
|
@ -59,7 +66,7 @@ export default class AuthController {
|
|||
const isValid = await TwoFactorAuthProvider.validate(user, code);
|
||||
if (isValid) {
|
||||
// login user and redirect to dashboard
|
||||
await auth.login(user);
|
||||
await auth.use('web').login(user);
|
||||
response.redirect('/apps/dashboard');
|
||||
} else {
|
||||
session.flash('message', 'Your tow factor code is incorrect');
|
||||
|
@ -70,17 +77,17 @@ export default class AuthController {
|
|||
if (codes.includes(recoveryCode)) {
|
||||
user.twoFactorRecoveryCodes = codes.filter((c) => c !== recoveryCode);
|
||||
await user.save();
|
||||
await auth.login(user);
|
||||
await auth.use('web').login(user);
|
||||
response.redirect('/apps/dashboard');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// logout function
|
||||
public async logout({ auth, response }: HttpContextContract) {
|
||||
public async logout({ auth, response }: HttpContext) {
|
||||
// await auth.logout();
|
||||
await auth.use('web').logout();
|
||||
response.redirect('/app/login');
|
||||
return response.redirect('/app/login');
|
||||
// return response.status(200);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
|
||||
import User from 'App/Models/User';
|
||||
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';
|
||||
import type { HttpContext } from '@adonisjs/core/http';
|
||||
import User from '#app/Models/User';
|
||||
// import { RenderResponse } from '@ioc:EidelLev/Inertia';
|
||||
import TwoFactorAuthProvider from '#app/Services/TwoFactorAuthProvider';
|
||||
import hash from '@adonisjs/core/services/hash';
|
||||
import { schema, rules } from '@adonisjs/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 {
|
||||
|
@ -12,7 +12,7 @@ export default class UserController {
|
|||
*
|
||||
* @return — \Inertia\Response
|
||||
*/
|
||||
public async accountInfo({ inertia, auth }: HttpContextContract): RenderResponse {
|
||||
public async accountInfo({ inertia, auth }: HttpContext) {
|
||||
// const user = auth.user;
|
||||
const user = (await User.find(auth.user?.id)) as User;
|
||||
// const id = request.param('id');
|
||||
|
@ -25,7 +25,7 @@ export default class UserController {
|
|||
});
|
||||
}
|
||||
|
||||
public async accountInfoStore({ auth, request, response, session }) {
|
||||
public async accountInfoStore({ auth, request, response, session }: HttpContext) {
|
||||
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')]),
|
||||
|
@ -39,7 +39,7 @@ export default class UserController {
|
|||
}
|
||||
|
||||
try {
|
||||
const user = await auth.user;
|
||||
const user = await auth.user as User;
|
||||
const { old_password, new_password } = request.only(['old_password', 'new_password']);
|
||||
|
||||
// if (!(old_password && new_password && confirm_password)) {
|
||||
|
@ -47,9 +47,9 @@ export default class UserController {
|
|||
// }
|
||||
|
||||
// Verify if the provided old password matches the user's current password
|
||||
const isSame = await Hash.verify(user.password, old_password);
|
||||
const isSame = await hash.verify(user.password, old_password);
|
||||
if (!isSame) {
|
||||
return response.flash({ warning: 'Old password is incorrect.' }).redirect().back();
|
||||
return response.flash('warning', 'Old password is incorrect.').redirect().back();
|
||||
}
|
||||
|
||||
// Hash the new password before updating the user's password
|
||||
|
@ -57,7 +57,7 @@ export default class UserController {
|
|||
await user.save();
|
||||
|
||||
// return response.status(200).send({ message: 'Password updated successfully.' });
|
||||
session.flash('Password updated successfully.');
|
||||
session.flash({ message: 'Password updated successfully.' });
|
||||
return response.redirect().toRoute('settings.user.index');
|
||||
} catch (error) {
|
||||
// return response.status(500).send({ message: 'Internal server error.' });
|
||||
|
@ -65,7 +65,7 @@ export default class UserController {
|
|||
}
|
||||
}
|
||||
|
||||
public async enableTwoFactorAuthentication({ auth, response, session }: HttpContextContract): Promise<void> {
|
||||
public async enableTwoFactorAuthentication({ auth, response, session }: HttpContext): Promise<void> {
|
||||
// const user: User | undefined = auth?.user;
|
||||
const user = (await User.find(auth.user?.id)) as User;
|
||||
|
||||
|
@ -87,7 +87,7 @@ export default class UserController {
|
|||
// });
|
||||
}
|
||||
|
||||
public async disableTwoFactorAuthentication({ auth, response, session }): Promise<void> {
|
||||
public async disableTwoFactorAuthentication({ auth, response, session }: HttpContext): Promise<void> {
|
||||
const user = auth?.user;
|
||||
|
||||
user.twoFactorSecret = null;
|
||||
|
|
Loading…
Add table
editor.link_modal.header
Reference in a new issue