Skip to content

Commit

Permalink
feat(core): Create a user from external authentication (#3005)
Browse files Browse the repository at this point in the history
  • Loading branch information
arrrrny authored Aug 11, 2024
1 parent 0ff8288 commit bb28d70
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,36 @@ export class ExternalAuthenticationService {

return customer?.user;
}

/**
* @description
* Looks up a User based on their identifier from an external authentication
* provider. Creates the user if does not exist. Unlike {@link findCustomerUser} and {@link findAdministratorUser},
* this method does not enforce that the User is associated with a Customer or
* Administrator account.
*
*/
async createUser(
ctx: RequestContext,
config: {
strategy: string;
externalIdentifier: string;
},
): Promise<User> {
const user = await this.findUser(ctx, config.strategy, config.externalIdentifier);
if (user) {
return user;
}
const newUser = new User();
const authMethod = await this.connection.getRepository(ctx, ExternalAuthenticationMethod).save(
new ExternalAuthenticationMethod({
externalIdentifier: config.externalIdentifier,
strategy: config.strategy,
}),
);
newUser.identifier = config.externalIdentifier;
newUser.authenticationMethods = [authMethod];
const savedUser = await this.connection.getRepository(ctx, User).save(newUser);
return savedUser;
}
}

0 comments on commit bb28d70

Please sign in to comment.