-
-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from bigcapitalhq/big-163-user-email-verifica…
…tion-after-signing-up feat: User email verification after signing-up.
- Loading branch information
Showing
38 changed files
with
1,193 additions
and
54 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
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
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
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
57 changes: 57 additions & 0 deletions
57
packages/server/src/services/Authentication/AuthSignupConfirm.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,57 @@ | ||
import { Inject, Service } from 'typedi'; | ||
import { ServiceError } from '@/exceptions'; | ||
import { SystemUser } from '@/system/models'; | ||
import { ERRORS } from './_constants'; | ||
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; | ||
import events from '@/subscribers/events'; | ||
import { | ||
IAuthSignUpVerifiedEventPayload, | ||
IAuthSignUpVerifingEventPayload, | ||
} from '@/interfaces'; | ||
|
||
@Service() | ||
export class AuthSignupConfirmService { | ||
@Inject() | ||
private eventPublisher: EventPublisher; | ||
|
||
/** | ||
* Verifies the provided user's email after signing-up. | ||
* @throws {ServiceErrors} | ||
* @param {IRegisterDTO} signupDTO | ||
* @returns {Promise<ISystemUser>} | ||
*/ | ||
public async signUpConfirm( | ||
email: string, | ||
verifyToken: string | ||
): Promise<SystemUser> { | ||
const foundUser = await SystemUser.query().findOne({ email, verifyToken }); | ||
|
||
if (!foundUser) { | ||
throw new ServiceError(ERRORS.SIGNUP_CONFIRM_TOKEN_INVALID); | ||
} | ||
const userId = foundUser.id; | ||
|
||
// Triggers `signUpConfirming` event. | ||
await this.eventPublisher.emitAsync(events.auth.signUpConfirming, { | ||
email, | ||
verifyToken, | ||
userId, | ||
} as IAuthSignUpVerifingEventPayload); | ||
|
||
const updatedUser = await SystemUser.query().patchAndFetchById( | ||
foundUser.id, | ||
{ | ||
verified: true, | ||
verifyToken: '', | ||
} | ||
); | ||
// Triggers `signUpConfirmed` event. | ||
await this.eventPublisher.emitAsync(events.auth.signUpConfirmed, { | ||
email, | ||
verifyToken, | ||
userId, | ||
} as IAuthSignUpVerifiedEventPayload); | ||
|
||
return updatedUser as SystemUser; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/server/src/services/Authentication/AuthSignupResend.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,34 @@ | ||
import { Inject, Service } from 'typedi'; | ||
import { ServiceError } from '@/exceptions'; | ||
import { SystemUser } from '@/system/models'; | ||
import { ERRORS } from './_constants'; | ||
|
||
@Service() | ||
export class AuthSignupConfirmResend { | ||
@Inject('agenda') | ||
private agenda: any; | ||
|
||
/** | ||
* Resends the email confirmation of the given user. | ||
* @param {number} userId - User ID. | ||
* @returns {Promise<void>} | ||
*/ | ||
public async signUpConfirmResend(userId: number) { | ||
const user = await SystemUser.query().findById(userId).throwIfNotFound(); | ||
|
||
// Throw error if the user is already verified. | ||
if (user.verified) { | ||
throw new ServiceError(ERRORS.USER_ALREADY_VERIFIED); | ||
} | ||
// Throw error if the verification token is not exist. | ||
if (!user.verifyToken) { | ||
throw new ServiceError(ERRORS.USER_ALREADY_VERIFIED); | ||
} | ||
const payload = { | ||
email: user.email, | ||
token: user.verifyToken, | ||
fullName: user.firstName, | ||
}; | ||
await this.agenda.now('send-signup-verify-mail', payload); | ||
} | ||
} |
Oops, something went wrong.