File tree 1 file changed +32
-0
lines changed
packages/core/src/service/helpers/external-authentication
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -239,4 +239,36 @@ export class ExternalAuthenticationService {
239
239
240
240
return customer ?. user ;
241
241
}
242
+
243
+ /**
244
+ * @description
245
+ * Looks up a User based on their identifier from an external authentication
246
+ * provider. Creates the user if does not exist. Unlike {@link findCustomerUser} and {@link findAdministratorUser},
247
+ * this method does not enforce that the User is associated with a Customer or
248
+ * Administrator account.
249
+ *
250
+ */
251
+ async createUser (
252
+ ctx : RequestContext ,
253
+ config : {
254
+ strategy : string ;
255
+ externalIdentifier : string ;
256
+ } ,
257
+ ) : Promise < User > {
258
+ const user = await this . findUser ( ctx , config . strategy , config . externalIdentifier ) ;
259
+ if ( user ) {
260
+ return user ;
261
+ }
262
+ const newUser = new User ( ) ;
263
+ const authMethod = await this . connection . getRepository ( ctx , ExternalAuthenticationMethod ) . save (
264
+ new ExternalAuthenticationMethod ( {
265
+ externalIdentifier : config . externalIdentifier ,
266
+ strategy : config . strategy ,
267
+ } ) ,
268
+ ) ;
269
+ newUser . identifier = config . externalIdentifier ;
270
+ newUser . authenticationMethods = [ authMethod ] ;
271
+ const savedUser = await this . connection . getRepository ( ctx , User ) . save ( newUser ) ;
272
+ return savedUser ;
273
+ }
242
274
}
You can’t perform that action at this time.
0 commit comments