Skip to content

Commit

Permalink
feat(authentication): make resend verification threshold configurable (
Browse files Browse the repository at this point in the history
…#1265)

* feat: make resend verification threshold configurable

* refactor: change sus initialization
  • Loading branch information
ChrisPdgn authored Dec 19, 2024
1 parent 1294bb5 commit cc982f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions modules/authentication/src/config/local.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export default {
format: 'String',
default: '',
},
resend_threshold: {
doc: 'Defines the threshold in milliseconds for resending verification',
format: 'Number',
default: 60000,
},
},
forgot_password_redirect_uri: {
doc: 'Defines where the user should be redirected once they click the forgot password link',
Expand Down
15 changes: 9 additions & 6 deletions modules/authentication/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,18 @@ export namespace AuthUtils {
);
}

export function checkResendThreshold(token: Token, notBefore: number = 600000) {
export function checkResendThreshold(token: Token, notBefore?: number) {
const threshold =
notBefore ||
ConfigController.getInstance().config.local.verification.resend_threshold;
const diffInMilliSec = Math.abs(new Date(token.createdAt).getTime() - Date.now());
if (diffInMilliSec < notBefore) {
const remainTime = Math.ceil((notBefore - diffInMilliSec) / notBefore);
if (diffInMilliSec < threshold) {
const remainTimeInSec = Math.ceil((threshold - diffInMilliSec) / 1000);
throw new GrpcError(
status.RESOURCE_EXHAUSTED,
'Verification code not sent. You have to wait ' +
remainTime +
' minutes to try again',
'Verification not sent. You have to wait ' +
remainTimeInSec +
' seconds to try again',
);
} else {
return true;
Expand Down

0 comments on commit cc982f0

Please sign in to comment.