소스 검색

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

Ahmet TOK 1 년 전
부모
커밋
bb28d70ca6
1개의 변경된 파일32개의 추가작업 그리고 0개의 파일을 삭제
  1. 32 0
      packages/core/src/service/helpers/external-authentication/external-authentication.service.ts

+ 32 - 0
packages/core/src/service/helpers/external-authentication/external-authentication.service.ts

@@ -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;
+    }
 }