@mantiq/auth 0.5.7 → 0.5.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/AuthenticatableModel.ts +54 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Authenticatable } from './contracts/Authenticatable.ts'
|
|
2
|
+
import { applyHasApiTokens } from './HasApiTokens.ts'
|
|
3
|
+
|
|
4
|
+
// Accept both abstract and concrete constructors
|
|
5
|
+
type AbstractConstructor<T = any> = abstract new (...args: any[]) => T
|
|
6
|
+
|
|
7
|
+
interface TokenMethods {
|
|
8
|
+
createToken(name: string, abilities?: string[], expiresAt?: Date): Promise<{ accessToken: any; plainTextToken: string }>
|
|
9
|
+
tokens(): any
|
|
10
|
+
currentAccessToken(): any
|
|
11
|
+
tokenCan(ability: string): boolean
|
|
12
|
+
tokenCant(ability: string): boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Mixin that adds Authenticatable + HasApiTokens to any Model class.
|
|
17
|
+
*
|
|
18
|
+
* Provides default implementations for conventional column names:
|
|
19
|
+
* - id (primary key)
|
|
20
|
+
* - password (hashed password)
|
|
21
|
+
* - remember_token (remember me token)
|
|
22
|
+
*
|
|
23
|
+
* Also adds token methods: createToken(), tokens(), tokenCan(), tokenCant()
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* import { AuthenticatableModel } from '@mantiq/auth'
|
|
27
|
+
* import { Model } from '@mantiq/database'
|
|
28
|
+
*
|
|
29
|
+
* export class User extends AuthenticatableModel(Model) {
|
|
30
|
+
* static override fillable = ['name', 'email', 'password']
|
|
31
|
+
* static override hidden = ['password', 'remember_token']
|
|
32
|
+
* }
|
|
33
|
+
*/
|
|
34
|
+
export function AuthenticatableModel<T extends AbstractConstructor>(Base: T) {
|
|
35
|
+
abstract class AuthModel extends Base implements Authenticatable {
|
|
36
|
+
getAuthIdentifierName(): string { return 'id' }
|
|
37
|
+
getAuthIdentifier(): string | number { return (this as any).getAttribute('id') }
|
|
38
|
+
getAuthPasswordName(): string { return 'password' }
|
|
39
|
+
getAuthPassword(): string { return (this as any).getAttribute('password') as string }
|
|
40
|
+
getRememberToken(): string | null { return ((this as any).getAttribute('remember_token') as string) ?? null }
|
|
41
|
+
setRememberToken(token: string | null): void { (this as any).setAttribute('remember_token', token) }
|
|
42
|
+
getRememberTokenName(): string { return 'remember_token' }
|
|
43
|
+
|
|
44
|
+
declare createToken: TokenMethods['createToken']
|
|
45
|
+
declare tokens: TokenMethods['tokens']
|
|
46
|
+
declare currentAccessToken: TokenMethods['currentAccessToken']
|
|
47
|
+
declare tokenCan: TokenMethods['tokenCan']
|
|
48
|
+
declare tokenCant: TokenMethods['tokenCant']
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
applyHasApiTokens(AuthModel)
|
|
52
|
+
|
|
53
|
+
return AuthModel as AbstractConstructor<Authenticatable & TokenMethods> & T
|
|
54
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -43,6 +43,7 @@ export { Attempting, Authenticated, Login, Failed, Logout, Registered, Lockout }
|
|
|
43
43
|
export { PersonalAccessToken } from './models/PersonalAccessToken.ts'
|
|
44
44
|
|
|
45
45
|
// ── Mixins ────────────────────────────────────────────────────────────────────
|
|
46
|
+
export { AuthenticatableModel } from './AuthenticatableModel.ts'
|
|
46
47
|
export { applyHasApiTokens } from './HasApiTokens.ts'
|
|
47
48
|
export { applyAuthorizable } from './Authorizable.ts'
|
|
48
49
|
|