33
44import {
55 ThunderIDNodeClient ,
6+ ThunderIDRuntimeError ,
7+ extractUserClaimsFromIdToken ,
8+ generateFlattenedUserProfile ,
9+ getUsersMe ,
10+ getUsersMeMeta ,
11+ resolveResourceEndpoint ,
12+ updateMeProfile ,
13+ type AttributeSchema ,
614 type AuthClientConfig ,
715 type IdToken ,
816 type Storage ,
@@ -44,6 +52,7 @@ class ThunderIDNuxtClient extends ThunderIDNodeClient<ThunderIDNuxtConfig> {
4452 clientId : config . clientId ! ,
4553 clientSecret : config . clientSecret || undefined ,
4654 enablePKCE : true ,
55+ endpoints : config . endpoints ,
4756 scopes : config . scopes || [ 'openid' , 'profile' ] ,
4857 tokenRequest : config . tokenRequest ,
4958 } as AuthClientConfig < ThunderIDNuxtConfig > ;
@@ -124,8 +133,23 @@ class ThunderIDNuxtClient extends ThunderIDNodeClient<ThunderIDNuxtConfig> {
124133 return ( configData ?. afterSignOutUrl as string ) || ( configData ?. afterSignInUrl as string ) || '/' ;
125134 }
126135
127- override getUser ( sessionId ?: string ) : Promise < User > {
128- return super . getUser ( sessionId ) ;
136+ override async getUser ( sessionId ?: string ) : Promise < User > {
137+ try {
138+ const configData : AuthClientConfig < ThunderIDNuxtConfig > = await this . getStorageManager ( ) . getConfigData ( ) ;
139+ const baseUrl : string | undefined = configData ?. baseUrl ;
140+
141+ const profile : User = await getUsersMe ( {
142+ baseUrl,
143+ url : resolveResourceEndpoint ( 'usersMe' , configData ) ,
144+ headers : {
145+ Authorization : `Bearer ${ await this . getAccessToken ( sessionId ) } ` ,
146+ } ,
147+ } ) ;
148+
149+ return profile ;
150+ } catch ( error ) {
151+ return await super . getUser ( sessionId ) ;
152+ }
129153 }
130154
131155 override getAccessToken ( sessionId ?: string ) : Promise < string > {
@@ -144,13 +168,68 @@ class ThunderIDNuxtClient extends ThunderIDNodeClient<ThunderIDNuxtConfig> {
144168 return super . exchangeToken ( config , sessionId ) as unknown as Promise < TokenResponse | Response > ;
145169 }
146170
147- override async getUserProfile ( sessionId : string ) : Promise < UserProfile > {
148- const user : User = await this . getUser ( sessionId ) ;
149- return { flattenedProfile : user , profile : user } ;
171+ override async getUserProfile ( sessionId ?: string ) : Promise < UserProfile > {
172+ try {
173+ const configData : AuthClientConfig < ThunderIDNuxtConfig > = await this . getStorageManager ( ) . getConfigData ( ) ;
174+ const baseUrl : string | undefined = configData ?. baseUrl ;
175+
176+ const profile : User = await getUsersMe ( {
177+ baseUrl,
178+ url : resolveResourceEndpoint ( 'usersMe' , configData ) ,
179+ headers : {
180+ Authorization : `Bearer ${ await this . getAccessToken ( sessionId ) } ` ,
181+ } ,
182+ } ) ;
183+
184+ return {
185+ flattenedProfile : generateFlattenedUserProfile ( profile ) ,
186+ profile,
187+ } ;
188+ } catch ( error ) {
189+ const claims = extractUserClaimsFromIdToken ( await super . getDecodedIdToken ( sessionId ) ) ;
190+ return {
191+ flattenedProfile : claims ,
192+ profile : claims ,
193+ } ;
194+ }
195+ }
196+
197+ override async updateUserProfile ( config : UpdateMeProfileConfig , sessionId ?: string ) : Promise < User > {
198+ try {
199+ const configData : AuthClientConfig < ThunderIDNuxtConfig > = await this . getStorageManager ( ) . getConfigData ( ) ;
200+ const baseUrl : string | undefined = configData ?. baseUrl ;
201+
202+ return updateMeProfile ( {
203+ baseUrl,
204+ url : resolveResourceEndpoint ( 'usersMe' , configData ) ,
205+ headers : {
206+ Authorization : `Bearer ${ await this . getAccessToken ( sessionId ) } ` ,
207+ } ,
208+ payload : ( config as any ) ?. payload ?? config ,
209+ } ) ;
210+ } catch ( error ) {
211+ throw new ThunderIDRuntimeError (
212+ `Failed to update user profile: ${ error instanceof Error ? error . message : String ( error ) } ` ,
213+ 'ThunderIDNuxtClient-UpdateProfileError-001' ,
214+ 'nuxt' ,
215+ 'An error occurred while updating the user profile. Please check your configuration and network connection.' ,
216+ ) ;
217+ }
150218 }
151219
152- override async updateUserProfile ( config : UpdateMeProfileConfig , sessionId : string ) : Promise < User > {
153- throw new Error ( 'Profile updates are not supported for the ThunderID platform.' ) ;
220+ async getUserSchema ( sessionId ?: string ) : Promise < Record < string , AttributeSchema > | null > {
221+ const configData : AuthClientConfig < ThunderIDNuxtConfig > = await this . getStorageManager ( ) . getConfigData ( ) ;
222+ const baseUrl : string | undefined = configData ?. baseUrl ;
223+
224+ const metaRes = await getUsersMeMeta ( {
225+ baseUrl,
226+ url : resolveResourceEndpoint ( 'usersMeMeta' , configData ) ,
227+ headers : {
228+ Authorization : `Bearer ${ await this . getAccessToken ( sessionId ) } ` ,
229+ } ,
230+ } ) ;
231+
232+ return metaRes ?. schema ?? null ;
154233 }
155234
156235 public override getStorageManager ( ) : any {
0 commit comments