Skip to content

Commit bb28d70

Browse files
authored
feat(core): Create a user from external authentication (#3005)
1 parent 0ff8288 commit bb28d70

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

packages/core/src/service/helpers/external-authentication/external-authentication.service.ts

+32
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,36 @@ export class ExternalAuthenticationService {
239239

240240
return customer?.user;
241241
}
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+
}
242274
}

0 commit comments

Comments
 (0)