Skip to content

Commit f06cfd2

Browse files
feat(users): prevent multi sign up (#8)
* feat(identity): sign-in deny multi register flag * feat(identity): cleanup * feat: add tests for deny auth register flow * Update microservices/users/src/services/identity-provider/abstract.ts Co-authored-by: Mikhail Yarmaliuk <[email protected]> * Update microservices/users/src/services/identity-provider/abstract.ts Co-authored-by: Mikhail Yarmaliuk <[email protected]> * feat(firebase-test): update name --------- Co-authored-by: Mikhail Yarmaliuk <[email protected]>
1 parent 5b18ec3 commit f06cfd2

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

microservices/users/__tests__/services/identity-provider/firebase-test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ describe('services/sign-up', () => {
100100
expect(await waitResult(service.signIn({ isDenyRegister: true }))).to.throw('User not found.');
101101
});
102102

103+
it('should registration throw error: user exist (split sign up/sign in)', async () => {
104+
FirebaseSdkStub.resolves(
105+
firebaseMock({ user: { email: '[email protected]', emailVerified: true } }),
106+
);
107+
TypeormMock.queryBuilder.getOne.resolves(mockUser);
108+
109+
expect(await waitResult(service.signIn({ isDenyAuthViaRegister: true }))).to.throw(
110+
'User already exists.',
111+
);
112+
});
113+
103114
it('should register new user', async () => {
104115
const email = '[email protected]';
105116

microservices/users/src/services/identity-provider/abstract.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ export interface ISingInReturn {
1212
isNew: boolean;
1313
}
1414

15-
export type TSingInParams = { isDenyRegister?: boolean } & Record<string, any>;
15+
/**
16+
* isDenyRegister - prevent sign in if user exist
17+
* isDenyAuthViaRegister - prevent sign in if user exist (split sign up/sign in)
18+
*/
19+
export type TSingInParams = { isDenyRegister?: boolean; isDenyAuthViaRegister?: boolean } & Record<
20+
string,
21+
any
22+
>;
1623

1724
/**
1825
* Abstract class for identity providers

microservices/users/src/services/identity-provider/firebase.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ class Firebase extends Abstract {
1717
/**
1818
* @inheritDoc
1919
*/
20-
public async signIn({ isDenyRegister }: TSingInParams = {}): Promise<ISingInReturn> {
20+
public async signIn({
21+
isDenyRegister,
22+
isDenyAuthViaRegister,
23+
}: TSingInParams = {}): Promise<ISingInReturn> {
2124
const [firebaseUser, providerType] = await this.getFirebaseUser();
22-
let user = await this.userRepository.findUserByIdentifier(this.provider, firebaseUser.uid);
23-
let isNew = false;
25+
const user = await this.userRepository.findUserByIdentifier(this.provider, firebaseUser.uid);
2426

2527
if (!user) {
2628
if (isDenyRegister) {
@@ -30,11 +32,22 @@ class Firebase extends Abstract {
3032
});
3133
}
3234

33-
user = await this.register(firebaseUser, providerType);
34-
isNew = true;
35+
const userResult = await this.register(firebaseUser, providerType);
36+
37+
return { user: userResult, isNew: true };
38+
}
39+
40+
if (isDenyAuthViaRegister) {
41+
/**
42+
* If user registered - error
43+
*/
44+
throw new BaseException({
45+
status: 500,
46+
message: 'User already exists.',
47+
});
3548
}
3649

37-
return { user, isNew };
50+
return { user, isNew: false };
3851
}
3952

4053
/**

0 commit comments

Comments
 (0)