1 | import {
|
2 | E_INVALID_CREDENTIALS
|
3 | } from "../../chunk-UGHJLKDI.js";
|
4 | import {
|
5 | __decorateClass
|
6 | } from "../../chunk-CZCFTIBB.js";
|
7 |
|
8 |
|
9 | import { RuntimeException } from "@adonisjs/core/exceptions";
|
10 | import { beforeSave } from "@adonisjs/lucid/orm";
|
11 | function withAuthFinder(hash, options) {
|
12 | return (superclass) => {
|
13 | class UserWithUserFinder extends superclass {
|
14 | static async hashPassword(user) {
|
15 | if (user.$dirty[options.passwordColumnName]) {
|
16 | ;
|
17 | user[options.passwordColumnName] = await hash().make(
|
18 | user[options.passwordColumnName]
|
19 | );
|
20 | }
|
21 | }
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | static findForAuth(uids, value) {
|
28 | const query = this.query();
|
29 | uids.forEach((uid) => query.orWhere(uid, value));
|
30 | return query.limit(1).first();
|
31 | }
|
32 | |
33 |
|
34 |
|
35 |
|
36 | static async verifyCredentials(uid, password) {
|
37 | if (!uid || !password) {
|
38 | throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
39 | }
|
40 | const user = await this.findForAuth(options.uids, uid);
|
41 | if (!user) {
|
42 | await hash().make(password);
|
43 | throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
44 | }
|
45 | const passwordHash = user[options.passwordColumnName];
|
46 | if (!passwordHash) {
|
47 | throw new RuntimeException(
|
48 | `Cannot verify password during login. The value of column "${options.passwordColumnName}" is undefined or null`
|
49 | );
|
50 | }
|
51 | if (await hash().verify(passwordHash, password)) {
|
52 | return user;
|
53 | }
|
54 | throw new E_INVALID_CREDENTIALS("Invalid user credentials");
|
55 | }
|
56 | }
|
57 | __decorateClass([
|
58 | beforeSave()
|
59 | ], UserWithUserFinder, "hashPassword", 1);
|
60 | return UserWithUserFinder;
|
61 | };
|
62 | }
|
63 | export {
|
64 | withAuthFinder
|
65 | };
|
66 |
|
\ | No newline at end of file |