diff options
author | MCMXC <16797721+mcmxcdev@users.noreply.github.com> | 2024-02-10 18:19:14 -0700 |
---|---|---|
committer | Vijay A <vraravam@users.noreply.github.com> | 2024-02-17 21:51:20 +0530 |
commit | a61e73c33b2e80d5af58e3dcfa2efe74245cd065 (patch) | |
tree | d5343ad245b4156662920896dedf14d9284051f8 /app/Controllers/Http/Dashboard/LoginController.ts | |
parent | upgrade node to 20.11.1 and other minor versions for pkgs (#104) (diff) | |
download | ferdium-server-a61e73c33b2e80d5af58e3dcfa2efe74245cd065.tar.gz ferdium-server-a61e73c33b2e80d5af58e3dcfa2efe74245cd065.tar.zst ferdium-server-a61e73c33b2e80d5af58e3dcfa2efe74245cd065.zip |
refactor: project maintenance
- work in progress
Diffstat (limited to 'app/Controllers/Http/Dashboard/LoginController.ts')
-rw-r--r-- | app/Controllers/Http/Dashboard/LoginController.ts | 58 |
1 files changed, 25 insertions, 33 deletions
diff --git a/app/Controllers/Http/Dashboard/LoginController.ts b/app/Controllers/Http/Dashboard/LoginController.ts index ffb9eeb..5a54448 100644 --- a/app/Controllers/Http/Dashboard/LoginController.ts +++ b/app/Controllers/Http/Dashboard/LoginController.ts | |||
@@ -1,26 +1,21 @@ | |||
1 | import type { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'; | 1 | import type { HttpContext } from '@adonisjs/core/http' |
2 | import { schema, rules, validator } from '@ioc:Adonis/Core/Validator'; | 2 | import { schema, rules, validator } from '@adonisjs/validator' |
3 | import User from 'App/Models/User'; | 3 | import User from '#app/Models/User' |
4 | import crypto from 'node:crypto'; | 4 | import crypto from 'node:crypto' |
5 | import { handleVerifyAndReHash } from '../../../../helpers/PasswordHash'; | 5 | import { handleVerifyAndReHash } from '../../../../helpers/PasswordHash.js' |
6 | 6 | ||
7 | export default class LoginController { | 7 | export default class LoginController { |
8 | /** | 8 | /** |
9 | * Display the login form | 9 | * Display the login form |
10 | */ | 10 | */ |
11 | public async show({ view }: HttpContextContract) { | 11 | public async show({ view }: HttpContext) { |
12 | return view.render('dashboard/login'); | 12 | return view.render('dashboard/login') |
13 | } | 13 | } |
14 | 14 | ||
15 | /** | 15 | /** |
16 | * Login a user | 16 | * Login a user |
17 | */ | 17 | */ |
18 | public async login({ | 18 | public async login({ request, response, auth, session }: HttpContext) { |
19 | request, | ||
20 | response, | ||
21 | auth, | ||
22 | session, | ||
23 | }: HttpContextContract) { | ||
24 | try { | 19 | try { |
25 | await validator.validate({ | 20 | await validator.validate({ |
26 | schema: schema.create({ | 21 | schema: schema.create({ |
@@ -28,54 +23,51 @@ export default class LoginController { | |||
28 | password: schema.string([rules.required()]), | 23 | password: schema.string([rules.required()]), |
29 | }), | 24 | }), |
30 | data: request.only(['mail', 'password']), | 25 | data: request.only(['mail', 'password']), |
31 | }); | 26 | }) |
32 | } catch { | 27 | } catch { |
33 | session.flash({ | 28 | session.flash({ |
34 | type: 'danger', | 29 | type: 'danger', |
35 | message: 'Invalid mail or password', | 30 | message: 'Invalid mail or password', |
36 | }); | 31 | }) |
37 | session.flashExcept(['password']); | 32 | session.flashExcept(['password']) |
38 | 33 | ||
39 | return response.redirect('/user/login'); | 34 | return response.redirect('/user/login') |
40 | } | 35 | } |
41 | 36 | ||
42 | try { | 37 | try { |
43 | const { mail, password } = request.all(); | 38 | const { mail, password } = request.all() |
44 | 39 | ||
45 | // Check if user with email exists | 40 | // Check if user with email exists |
46 | const user = await User.query().where('email', mail).first(); | 41 | const user = await User.query().where('email', mail).first() |
47 | if (!user?.email) { | 42 | if (!user?.email) { |
48 | throw new Error('User credentials not valid (Invalid email)'); | 43 | throw new Error('User credentials not valid (Invalid email)') |
49 | } | 44 | } |
50 | 45 | ||
51 | const hashedPassword = crypto | 46 | const hashedPassword = crypto.createHash('sha256').update(password).digest('base64') |
52 | .createHash('sha256') | ||
53 | .update(password) | ||
54 | .digest('base64'); | ||
55 | 47 | ||
56 | // Verify password | 48 | // Verify password |
57 | let isMatchedPassword = false; | 49 | let isMatchedPassword = false |
58 | try { | 50 | try { |
59 | isMatchedPassword = await handleVerifyAndReHash(user, hashedPassword); | 51 | isMatchedPassword = await handleVerifyAndReHash(user, hashedPassword) |
60 | } catch (error) { | 52 | } catch (error) { |
61 | return response.internalServerError({ message: error.message }); | 53 | return response.internalServerError({ message: error.message }) |
62 | } | 54 | } |
63 | 55 | ||
64 | if (!isMatchedPassword) { | 56 | if (!isMatchedPassword) { |
65 | throw new Error('User credentials not valid (Invalid password)'); | 57 | throw new Error('User credentials not valid (Invalid password)') |
66 | } | 58 | } |
67 | 59 | ||
68 | await auth.use('web').login(user); | 60 | await auth.use('web').login(user) |
69 | 61 | ||
70 | return response.redirect('/user/account'); | 62 | return response.redirect('/user/account') |
71 | } catch { | 63 | } catch { |
72 | session.flash({ | 64 | session.flash({ |
73 | type: 'danger', | 65 | type: 'danger', |
74 | message: 'Invalid mail or password', | 66 | message: 'Invalid mail or password', |
75 | }); | 67 | }) |
76 | session.flashExcept(['password']); | 68 | session.flashExcept(['password']) |
77 | 69 | ||
78 | return response.redirect('/user/login'); | 70 | return response.redirect('/user/login') |
79 | } | 71 | } |
80 | } | 72 | } |
81 | } | 73 | } |