Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Add login link token to email
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 4, 2020
1 parent 47ceb5b commit 170ba99
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/_staart/helpers/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ export const passwordResetToken = (id: number) =>
export const loginToken = (user: users) =>
generateToken(user, TOKEN_EXPIRY_LOGIN, Tokens.LOGIN);

/**
* Generate a new login link JWT
*/
export const loginLinkToken = (user: users) =>
generateToken({ id: user.id }, TOKEN_EXPIRY_LOGIN, Tokens.LOGIN_LINK);

/**
* Generate a new 2FA JWT
*/
Expand Down
1 change: 1 addition & 0 deletions src/_staart/interfaces/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export enum Templates {

export enum Tokens {
LOGIN = "auth",
LOGIN_LINK = "login-link",
API_KEY = "api-key",
ACCESS_TOKEN = "access-token",
TWO_FACTOR = "2fa",
Expand Down
8 changes: 6 additions & 2 deletions src/_staart/rest/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
INSUFFICIENT_PERMISSION,
INVALID_2FA_TOKEN,
INVALID_LOGIN,
MISSING_PASSWORD,
NOT_ENABLED_2FA,
RESOURCE_NOT_FOUND,
USER_NOT_FOUND,
Expand All @@ -23,6 +22,7 @@ import { deleteItemFromCache } from "../helpers/cache";
import {
checkInvalidatedToken,
getLoginResponse,
loginLinkToken,
passwordResetToken,
postLoginTokens,
resendEmailVerificationToken,
Expand Down Expand Up @@ -86,7 +86,11 @@ export const login = async (
throw new Error(USER_NOT_FOUND);
}
if (!user.password) {
await mail({ template: Templates.LOGIN_LINK, data: user, to: email });
await mail({
template: Templates.LOGIN_LINK,
data: { ...user, token: loginLinkToken(user) },
to: email,
});
return { success: true, message: "login-link-sent" };
}
const isPasswordCorrect = await compare(password, user.password);
Expand Down

0 comments on commit 170ba99

Please sign in to comment.