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

Commit 268dc56

Browse files
✨ Send password reset on register
1 parent 6ff04ad commit 268dc56

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "staart",
3-
"version": "1.0.39",
3+
"version": "1.0.40",
44
"main": "index.js",
55
"repository": "git@github.com:AnandChowdhary/staart.git",
66
"author": "Anand Chowdhary <mail@anandchowdhary.com>",

src/crud/email.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { dateToDateTime } from "../helpers/utils";
1111
import { KeyValue } from "../interfaces/general";
1212
import { User } from "../interfaces/tables/user";
1313
import { getUser } from "./user";
14-
import { ErrorCode, Templates, CacheCategories } from "../interfaces/enum";
14+
import { ErrorCode, Templates } from "../interfaces/enum";
1515
import { emailVerificationToken } from "../helpers/jwt";
1616
import { mail } from "../helpers/mail";
1717
import { InsertResult } from "../interfaces/mysql";
18-
import { deleteItemFromCache, cachedQuery } from "../helpers/cache";
18+
import { sendNewPassword } from "../rest/auth";
1919

2020
/**
2121
* Create a new email for a user
@@ -42,6 +42,8 @@ export const createEmail = async (
4242
email.email,
4343
await getUser(email.userId)
4444
);
45+
} else {
46+
await sendNewPassword(email.email);
4547
}
4648
return result;
4749
};

src/interfaces/enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export enum ErrorCode {
9191
export enum Templates {
9292
EMAIL_VERIFY = "email-verify",
9393
PASSWORD_RESET = "password-reset",
94+
NEW_PASSWORD = "new-password",
9495
UNAPPROVED_LOCATION = "unapproved-location"
9596
}
9697

src/rest/auth.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export const register = async (
116116
userId,
117117
email
118118
},
119-
true,
119+
!emailVerified,
120120
!!emailVerified
121121
);
122122
const emailId = newEmail.insertId;
@@ -133,19 +133,28 @@ export const register = async (
133133
return { created: true, userId };
134134
};
135135

136-
export const sendPasswordReset = async (email: string, locals: Locals) => {
136+
export const sendPasswordReset = async (email: string, locals?: Locals) => {
137137
const user = await getUserByEmail(email);
138138
if (!user.id) throw new Error(ErrorCode.USER_NOT_FOUND);
139139
const token = await passwordResetToken(user.id);
140140
await mail(email, Templates.PASSWORD_RESET, { name: user.name, token });
141-
await createEvent(
142-
{
143-
userId: user.id,
144-
type: EventType.AUTH_PASSWORD_RESET_REQUESTED,
145-
data: { token }
146-
},
147-
locals
148-
);
141+
if (locals)
142+
await createEvent(
143+
{
144+
userId: user.id,
145+
type: EventType.AUTH_PASSWORD_RESET_REQUESTED,
146+
data: { token }
147+
},
148+
locals
149+
);
150+
return;
151+
};
152+
153+
export const sendNewPassword = async (email: string) => {
154+
const user = await getUserByEmail(email);
155+
if (!user.id) throw new Error(ErrorCode.USER_NOT_FOUND);
156+
const token = await passwordResetToken(user.id);
157+
await mail(email, Templates.NEW_PASSWORD, { name: user.name, token });
149158
return;
150159
};
151160

@@ -341,7 +350,7 @@ export const salesforce = new ClientOAuth2({
341350
redirectUri: redirectUri("salesforce"),
342351
authorizationUri: "https://login.salesforce.com/services/oauth2/authorize",
343352
accessTokenUri: "https://login.salesforce.com/services/oauth2/token",
344-
scopes: ["email", "name"]
353+
scopes: ["email"]
345354
});
346355
export const salesforceCallback = async (url: string, locals: Locals) => {
347356
const response = await salesforce.code.getToken(url);

src/templates/new-password.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set a password
2+
3+
Dear {{name}},
4+
5+
Click on the following button to set up a password for your new account:
6+
7+
<a style="background: #555; padding: 1rem 2rem; font-size: 120%; color: #fff; display: inline-block; margin: 2rem auto; border-radius: 0.25rem" href="{{frontendUrl}}/auth/token?subject=password-reset&token={{token}}">Set a password</a>
8+
9+
If the above link doesn't work, you can copy and paste this URL in your browser: {{frontendUrl}}/auth/token?subject=password-reset&token={{token}}.
10+
11+
If you didn't request this email, you can safely ignore it.

0 commit comments

Comments
 (0)