2023-03-03 15:54:28 +00:00
|
|
|
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';
|
|
|
|
|
|
|
|
export default class AuthController {
|
2023-06-22 15:20:04 +00:00
|
|
|
// login function
|
|
|
|
public async login({ request, response, auth, session }: HttpContextContract) {
|
|
|
|
// console.log({
|
|
|
|
// registerBody: request.body(),
|
|
|
|
// });
|
2023-03-03 15:54:28 +00:00
|
|
|
await request.validate(AuthValidator);
|
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
const plainPassword = await request.input('password');
|
|
|
|
const email = await request.input('email');
|
|
|
|
// grab uid and password values off request body
|
|
|
|
// const { email, password } = request.only(['email', 'password'])
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
try {
|
|
|
|
// attempt to login
|
|
|
|
await auth.use('web').attempt(email, plainPassword);
|
|
|
|
} catch (error) {
|
|
|
|
// if login fails, return vague form message and redirect back
|
|
|
|
session.flash('message', 'Your username, email, or password is incorrect');
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// otherwise, redirect todashboard
|
2023-12-01 09:44:19 +00:00
|
|
|
response.redirect('/app/dashboard');
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// logout function
|
|
|
|
public async logout({ auth, response }: HttpContextContract) {
|
|
|
|
// await auth.logout();
|
|
|
|
await auth.use('web').logout();
|
|
|
|
response.redirect('/app/login');
|
|
|
|
// return response.status(200);
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|