diff options
author | 2024-02-10 18:19:14 -0700 | |
---|---|---|
committer | 2024-02-13 06:59:44 +0530 | |
commit | 7584d2d7a7110aef0331ebfa178b2295842c59fa (patch) | |
tree | 900cd71237e6231b57936fcce77ff229cd459041 /app/Models/User.ts | |
parent | upgrade recipes submodule (diff) | |
download | ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.gz ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.tar.zst ferdium-server-7584d2d7a7110aef0331ebfa178b2295842c59fa.zip |
refactor: project maintenance
- work in progress
Diffstat (limited to 'app/Models/User.ts')
-rw-r--r-- | app/Models/User.ts | 85 |
1 files changed, 38 insertions, 47 deletions
diff --git a/app/Models/User.ts b/app/Models/User.ts index 0b8e688..cc2c553 100644 --- a/app/Models/User.ts +++ b/app/Models/User.ts | |||
@@ -1,75 +1,70 @@ | |||
1 | import { DateTime } from 'luxon'; | 1 | import { DateTime } from 'luxon' |
2 | import { | 2 | import { BaseModel, beforeSave, column, hasMany } from '@adonisjs/lucid/orm' |
3 | BaseModel, | 3 | import hash from '@adonisjs/core/services/hash' |
4 | beforeSave, | 4 | import emitter from '@adonisjs/core/services/emitter' |
5 | column, | 5 | import moment from 'moment' |
6 | HasMany, | 6 | import Encryption from '@ioc:Adonis/Core/Encryption' |
7 | hasMany, | 7 | import randtoken from 'rand-token' |
8 | } from '@ioc:Adonis/Lucid/Orm'; | 8 | import Token from './Token.js' |
9 | import Hash from '@ioc:Adonis/Core/Hash'; | 9 | import Workspace from './Workspace.js' |
10 | import Event from '@ioc:Adonis/Core/Event'; | 10 | import Service from './Service.js' |
11 | import moment from 'moment'; | 11 | import mail from '@adonisjs/mail/services/main' |
12 | import Encryption from '@ioc:Adonis/Core/Encryption'; | 12 | import { url } from '#config/app' |
13 | import randtoken from 'rand-token'; | 13 | import { mailFrom } from '#config/dashboard' |
14 | import Token from './Token'; | 14 | import { HasMany } from '@adonisjs/lucid/types/relations' |
15 | import Workspace from './Workspace'; | ||
16 | import Service from './Service'; | ||
17 | import Mail from '@ioc:Adonis/Addons/Mail'; | ||
18 | import { url } from 'Config/app'; | ||
19 | import { mailFrom } from 'Config/dashboard'; | ||
20 | 15 | ||
21 | export default class User extends BaseModel { | 16 | export default class User extends BaseModel { |
22 | @column({ isPrimary: true }) | 17 | @column({ isPrimary: true }) |
23 | public id: number; | 18 | public id: number |
24 | 19 | ||
25 | @column() | 20 | @column() |
26 | public email: string; | 21 | public email: string |
27 | 22 | ||
28 | @column() | 23 | @column() |
29 | public username: string; | 24 | public username: string |
30 | 25 | ||
31 | @column() | 26 | @column() |
32 | public password: string; | 27 | public password: string |
33 | 28 | ||
34 | @column() | 29 | @column() |
35 | public lastname: string; | 30 | public lastname: string |
36 | 31 | ||
37 | // TODO: Type the settings object. | 32 | // TODO: Type the settings object. |
38 | @column() | 33 | @column() |
39 | public settings: object; | 34 | public settings: object |
40 | 35 | ||
41 | @column.dateTime({ autoCreate: true }) | 36 | @column.dateTime({ autoCreate: true }) |
42 | public created_at: DateTime; | 37 | public created_at: DateTime |
43 | 38 | ||
44 | @column.dateTime({ autoCreate: true, autoUpdate: true }) | 39 | @column.dateTime({ autoCreate: true, autoUpdate: true }) |
45 | public updated_at: DateTime; | 40 | public updated_at: DateTime |
46 | 41 | ||
47 | @beforeSave() | 42 | @beforeSave() |
48 | public static async hashPassword(user: User) { | 43 | public static async hashPassword(user: User) { |
49 | if (user.$dirty.password) { | 44 | if (user.$dirty.password) { |
50 | user.password = await Hash.make(user.password); | 45 | user.password = await hash.make(user.password) |
51 | } | 46 | } |
52 | } | 47 | } |
53 | 48 | ||
54 | @hasMany(() => Token, { | 49 | @hasMany(() => Token, { |
55 | foreignKey: 'user_id', | 50 | foreignKey: 'user_id', |
56 | }) | 51 | }) |
57 | public tokens: HasMany<typeof Token>; | 52 | public tokens: HasMany<typeof Token> |
58 | 53 | ||
59 | @hasMany(() => Service, { | 54 | @hasMany(() => Service, { |
60 | foreignKey: 'userId', | 55 | foreignKey: 'userId', |
61 | }) | 56 | }) |
62 | public services: HasMany<typeof Service>; | 57 | public services: HasMany<typeof Service> |
63 | 58 | ||
64 | @hasMany(() => Workspace, { | 59 | @hasMany(() => Workspace, { |
65 | foreignKey: 'userId', | 60 | foreignKey: 'userId', |
66 | }) | 61 | }) |
67 | public workspaces: HasMany<typeof Workspace>; | 62 | public workspaces: HasMany<typeof Workspace> |
68 | 63 | ||
69 | public async forgotPassword(): Promise<void> { | 64 | public async forgotPassword(): Promise<void> { |
70 | const token = await this.generateToken(this, 'forgot_password'); | 65 | const token = await this.generateToken(this, 'forgot_password') |
71 | 66 | ||
72 | await Mail.send(message => { | 67 | await mail.send((message) => { |
73 | message | 68 | message |
74 | .from(mailFrom) | 69 | .from(mailFrom) |
75 | .to(this.email) | 70 | .to(this.email) |
@@ -78,13 +73,13 @@ export default class User extends BaseModel { | |||
78 | username: this.username, | 73 | username: this.username, |
79 | appUrl: url, | 74 | appUrl: url, |
80 | token: token, | 75 | token: token, |
81 | }); | 76 | }) |
82 | }); | 77 | }) |
83 | 78 | ||
84 | await Event.emit('forgot:password', { | 79 | await emitter.emit('forgot:password', { |
85 | user: this, | 80 | user: this, |
86 | token, | 81 | token, |
87 | }); | 82 | }) |
88 | } | 83 | } |
89 | 84 | ||
90 | private async generateToken(user: User, type: string): Promise<string> { | 85 | private async generateToken(user: User, type: string): Promise<string> { |
@@ -93,21 +88,17 @@ export default class User extends BaseModel { | |||
93 | .query() | 88 | .query() |
94 | .where('type', type) | 89 | .where('type', type) |
95 | .where('is_revoked', false) | 90 | .where('is_revoked', false) |
96 | .where( | 91 | .where('updated_at', '>=', moment().subtract(24, 'hours').format('YYYY-MM-DD HH:mm:ss')) |
97 | 'updated_at', | ||
98 | '>=', | ||
99 | moment().subtract(24, 'hours').format('YYYY-MM-DD HH:mm:ss'), | ||
100 | ); | ||
101 | 92 | ||
102 | const row = await query.first(); | 93 | const row = await query.first() |
103 | if (row) { | 94 | if (row) { |
104 | return row.token; | 95 | return row.token |
105 | } | 96 | } |
106 | 97 | ||
107 | const token = Encryption.encrypt(randtoken.generate(16)); | 98 | const token = Encryption.encrypt(randtoken.generate(16)) |
108 | 99 | ||
109 | await user.related('tokens').create({ type, token }); | 100 | await user.related('tokens').create({ type, token }) |
110 | 101 | ||
111 | return token; | 102 | return token |
112 | } | 103 | } |
113 | } | 104 | } |