-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core,schemas): implement the register flow (#6237)
* feat(core,schemas): implement the register flow implement the register flow * refactor(core,schemas): relocate the profile type defs relocate the profile type defs * fix(core): fix the validation guard logic fix the validation guard logic * fix(core): fix social and sso identity not created bug fix social and sso identity not created bug * fix(core): fix social identities profile key fix social identities profile key * fix(core): fix sso query method fix sso query method
- Loading branch information
Showing
10 changed files
with
449 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/core/src/routes/experience/classes/validators/profile-validator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import RequestError from '#src/errors/RequestError/index.js'; | ||
import type Libraries from '#src/tenants/Libraries.js'; | ||
import type Queries from '#src/tenants/Queries.js'; | ||
import assertThat from '#src/utils/assert-that.js'; | ||
|
||
import type { InteractionProfile } from '../../types.js'; | ||
|
||
export class ProfileValidator { | ||
constructor( | ||
private readonly libraries: Libraries, | ||
private readonly queries: Queries, | ||
/** UserId is required for existing user profile update validation */ | ||
private readonly userId?: string | ||
) {} | ||
|
||
public async guardProfileUniquenessAcrossUsers(profile: InteractionProfile) { | ||
const { hasUser, hasUserWithEmail, hasUserWithPhone, hasUserWithIdentity } = this.queries.users; | ||
const { userSsoIdentities } = this.queries; | ||
|
||
const { username, primaryEmail, primaryPhone, socialIdentity, enterpriseSsoIdentity } = profile; | ||
|
||
if (username) { | ||
assertThat( | ||
!(await hasUser(username)), | ||
new RequestError({ | ||
code: 'user.username_already_in_use', | ||
status: 422, | ||
}) | ||
); | ||
} | ||
|
||
if (primaryEmail) { | ||
assertThat( | ||
!(await hasUserWithEmail(primaryEmail)), | ||
new RequestError({ | ||
code: 'user.email_already_in_use', | ||
status: 422, | ||
}) | ||
); | ||
} | ||
|
||
if (primaryPhone) { | ||
assertThat( | ||
!(await hasUserWithPhone(primaryPhone)), | ||
new RequestError({ | ||
code: 'user.phone_already_in_use', | ||
status: 422, | ||
}) | ||
); | ||
} | ||
|
||
if (socialIdentity) { | ||
const { | ||
target, | ||
userInfo: { id }, | ||
} = socialIdentity; | ||
|
||
assertThat( | ||
!(await hasUserWithIdentity(target, id)), | ||
new RequestError({ | ||
code: 'user.identity_already_in_use', | ||
status: 422, | ||
}) | ||
); | ||
} | ||
|
||
if (enterpriseSsoIdentity) { | ||
const { issuer, identityId } = enterpriseSsoIdentity; | ||
const userSsoIdentity = await userSsoIdentities.findUserSsoIdentityBySsoIdentityId( | ||
issuer, | ||
identityId | ||
); | ||
|
||
assertThat( | ||
!userSsoIdentity, | ||
new RequestError({ | ||
code: 'user.identity_already_in_use', | ||
status: 422, | ||
}) | ||
); | ||
} | ||
|
||
// TODO: Password validation | ||
} | ||
} |
Oops, something went wrong.