@@ -5,6 +5,7 @@ import type { FallbackIfNever } from '@douglasneuroinformatics/libjs';
55import { ConfigurableModuleBuilder } from '@nestjs/common' ;
66import { Prisma } from '@prisma/client' ;
77import type { DefaultSelection } from '@prisma/client/runtime/library' ;
8+ import type { IfNever } from 'type-fest' ;
89import type { z } from 'zod' ;
910
1011import { defineToken } from '../../utils/token.utils.js' ;
@@ -33,9 +34,10 @@ type AppConditions = FallbackIfNever<PrismaQuery, unknown>;
3334
3435type AppAbility = PureAbility < AppAbilities , AppConditions > ;
3536
36- type DefineAbility < TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown } > = (
37+ type DefineAbility < TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown } , TMetadata = any > = (
3738 ability : AbilityBuilder < AppAbility > ,
38- tokenPayload : TPayload
39+ tokenPayload : TPayload ,
40+ metadata : TMetadata
3941) => void ;
4042
4143type Permission = RawRuleOf < PureAbility < [ AppAction , AppSubjectName ] > > ;
@@ -51,34 +53,44 @@ interface JwtPayload {
5153 permissions : Permission [ ] ;
5254}
5355
54- type UserQueryResult < TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown } > = {
56+ type UserQueryResult <
57+ TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown } ,
58+ TMetadata = never
59+ > = IfNever < TMetadata , { } , { metadata : TMetadata } > & {
5560 hashedPassword : string ;
5661 tokenPayload : TPayload ;
5762} ;
5863
5964type UserQuery <
6065 TLoginCredentials extends BaseLoginCredentials = BaseLoginCredentials ,
61- TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown }
62- > = ( credentials : TLoginCredentials ) => Promise < null | UserQueryResult < TPayload > > ;
66+ TPayload extends { [ key : string ] : unknown } = { [ key : string ] : unknown } ,
67+ TMetadata = any
68+ > = ( credentials : TLoginCredentials ) => Promise < null | UserQueryResult < TPayload , TMetadata > > ;
6369
6470type LoginResponseBody = {
6571 accessToken : string ;
6672} ;
6773
6874type AuthModuleOptions <
6975 TLoginCredentialsSchema extends BaseLoginCredentialsSchema = BaseLoginCredentialsSchema ,
70- TPayloadSchema extends z . ZodType < { [ key : string ] : unknown } > = z . ZodType < { [ key : string ] : unknown } >
76+ TPayloadSchema extends z . ZodType < { [ key : string ] : unknown } > = z . ZodType < { [ key : string ] : unknown } > ,
77+ TMetadataSchema extends z . ZodTypeAny = z . ZodNever
7178> = {
72- defineAbility : ( ability : AbilityBuilder < AppAbility > , tokenPayload : z . TypeOf < TPayloadSchema > ) => any ;
79+ defineAbility : (
80+ ability : AbilityBuilder < AppAbility > ,
81+ tokenPayload : z . TypeOf < TPayloadSchema > ,
82+ metadata : z . TypeOf < TMetadataSchema >
83+ ) => any ;
7384 schemas : {
7485 loginCredentials : TLoginCredentialsSchema ;
86+ metadata ?: TMetadataSchema ;
7587 tokenPayload : TPayloadSchema ;
7688 } ;
77- userQuery : UserQuery < z . TypeOf < TLoginCredentialsSchema > , z . TypeOf < TPayloadSchema > > ;
89+ userQuery : UserQuery < z . TypeOf < TLoginCredentialsSchema > , z . TypeOf < TPayloadSchema > , z . TypeOf < TMetadataSchema > > ;
7890} ;
7991
8092export const { ConfigurableModuleClass : ConfigurableAuthModule , MODULE_OPTIONS_TOKEN : AUTH_MODULE_OPTIONS_TOKEN } =
81- new ConfigurableModuleBuilder < AuthModuleOptions < any , any > > ( )
93+ new ConfigurableModuleBuilder < AuthModuleOptions < any , any , any > > ( )
8294 . setClassMethodName ( 'forRoot' )
8395 . setExtras ( { } , ( definition ) => ( {
8496 ...definition ,
0 commit comments