Skip to content

Commit a7a548a

Browse files
committed
✨ Enhance User Model: Refactor user properties to include username, avatar, and social media IDs; implement password hashing before save; add serialization method for Inertia response.
1 parent c15d3ae commit a7a548a

1 file changed

Lines changed: 51 additions & 5 deletions

File tree

‎app/models/user.ts‎

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DateTime } from 'luxon'
2-
import hash from '@adonisjs/core/services/hash'
2+
import { BaseModel, beforeSave, column } from '@adonisjs/lucid/orm'
33
import { compose } from '@adonisjs/core/helpers'
4-
import { BaseModel, column } from '@adonisjs/lucid/orm'
4+
import hash from '@adonisjs/core/services/hash'
55
import { withAuthFinder } from '@adonisjs/auth/mixins/lucid'
66

77
const AuthFinder = withAuthFinder(() => hash.use('scrypt'), {
@@ -14,17 +14,63 @@ export default class User extends compose(BaseModel, AuthFinder) {
1414
declare id: number
1515

1616
@column()
17-
declare fullName: string | null
17+
declare username: string
18+
19+
@column()
20+
declare name: string | null
1821

1922
@column()
2023
declare email: string
2124

2225
@column({ serializeAs: null })
2326
declare password: string
2427

28+
@column()
29+
declare avatar: string | null
30+
31+
@column()
32+
declare githubId: string | null
33+
34+
@column()
35+
declare twitterId: string | null
36+
37+
@column()
38+
declare isAdmin: boolean
39+
40+
@column()
41+
declare isSponsor: boolean
42+
43+
@column.dateTime()
44+
declare emailVerifiedAt: DateTime | null
45+
46+
@column()
47+
declare rememberMeToken: string | null
48+
2549
@column.dateTime({ autoCreate: true })
2650
declare createdAt: DateTime
2751

2852
@column.dateTime({ autoCreate: true, autoUpdate: true })
29-
declare updatedAt: DateTime | null
30-
}
53+
declare updatedAt: DateTime
54+
55+
@beforeSave()
56+
static async hashPassword(user: User) {
57+
if (user.$dirty.password) {
58+
user.password = await hash.make(user.password)
59+
}
60+
}
61+
62+
/**
63+
* Serialize the model for Inertia
64+
*/
65+
serialize() {
66+
return {
67+
id: this.id,
68+
username: this.username,
69+
name: this.name,
70+
email: this.email,
71+
avatar: this.avatar,
72+
isAdmin: this.isAdmin,
73+
isSponsor: this.isSponsor,
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)