UNPKG

2.12 kBJavaScriptView Raw
1import {
2 E_INVALID_CREDENTIALS
3} from "../../chunk-UGHJLKDI.js";
4import {
5 __decorateClass
6} from "../../chunk-CZCFTIBB.js";
7
8// src/mixins/lucid.ts
9import { RuntimeException } from "@adonisjs/core/exceptions";
10import { beforeSave } from "@adonisjs/lucid/orm";
11function 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 * Finds the user for authentication via "verifyCredentials".
24 * Feel free to override this method customize the user
25 * lookup behavior.
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 * Find a user by uid and verify their password. This method is
34 * safe from timing attacks.
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}
63export {
64 withAuthFinder
65};
66//# sourceMappingURL=lucid.js.map
\No newline at end of file