Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Create a user from external authentication #3005

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading