@tylix/auth 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -7
- package/src/generators/AuthGenerator.js +32 -0
- package/src/generators/templates/auth-controller-ts.tyx +14 -11
- package/src/generators/templates/auth-controller.tyx +11 -10
- package/src/generators/templates/mailer-ts.tyx +9 -7
- package/src/generators/templates/mailer.tyx +10 -8
- package/src/generators/templates/reset-password-mail-ts.tyx +15 -0
- package/src/generators/templates/reset-password-mail.tyx +16 -0
- package/src/generators/templates/reset-password-view.html +3 -0
- package/src/generators/templates/verify-email-mail-ts.tyx +15 -0
- package/src/generators/templates/verify-email-mail.tyx +16 -0
- package/src/generators/templates/verify-email-view.html +3 -0
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tylix/auth",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "node --test src/**/*.test.js"
|
|
9
9
|
},
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@tylix/core": "^0.2.0",
|
|
12
|
+
"@tylix/generator": "^0.2.0",
|
|
13
|
+
"@tylix/orm": "^0.2.0",
|
|
14
|
+
"@tylix/shared": "^0.2.0"
|
|
15
|
+
}
|
|
16
16
|
}
|
|
@@ -59,6 +59,38 @@ export class AuthGenerator {
|
|
|
59
59
|
{ overwrite: true },
|
|
60
60
|
)
|
|
61
61
|
|
|
62
|
+
results.verifyEmailMail = await writeFile(
|
|
63
|
+
path.join(baseDir, 'app', 'mail', `VerifyEmailMail.${ext}`),
|
|
64
|
+
await read('verify-email-mail'),
|
|
65
|
+
{ overwrite: true },
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
results.resetPasswordMail = await writeFile(
|
|
69
|
+
path.join(baseDir, 'app', 'mail', `ResetPasswordMail.${ext}`),
|
|
70
|
+
await read('reset-password-mail'),
|
|
71
|
+
{ overwrite: true },
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
await fs.mkdir(path.join(baseDir, 'resources', 'mail'), { recursive: true })
|
|
75
|
+
|
|
76
|
+
results.verifyEmailView = await writeFile(
|
|
77
|
+
path.join(baseDir, 'resources', 'mail', 'verify-email.html'),
|
|
78
|
+
await fs.readFile(
|
|
79
|
+
path.join(TEMPLATES_DIR, 'verify-email-view.html'),
|
|
80
|
+
'utf-8',
|
|
81
|
+
),
|
|
82
|
+
{ overwrite: true },
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
results.resetPasswordView = await writeFile(
|
|
86
|
+
path.join(baseDir, 'resources', 'mail', 'reset-password.html'),
|
|
87
|
+
await fs.readFile(
|
|
88
|
+
path.join(TEMPLATES_DIR, 'reset-password-view.html'),
|
|
89
|
+
'utf-8',
|
|
90
|
+
),
|
|
91
|
+
{ overwrite: true },
|
|
92
|
+
)
|
|
93
|
+
|
|
62
94
|
results.userMigration = await writeFile(
|
|
63
95
|
path.join(
|
|
64
96
|
baseDir,
|
|
@@ -36,15 +36,18 @@ const DEFAULT_ACCESS_TOKEN_TTL_SECONDS = 15 * 60;
|
|
|
36
36
|
interface AuthControllerOptions {
|
|
37
37
|
secret: string;
|
|
38
38
|
tokenExpiresInSeconds?: number;
|
|
39
|
+
mailConfig: unknown;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
export class AuthController {
|
|
42
43
|
secret: string;
|
|
43
44
|
accessTokenTtlSeconds: number;
|
|
45
|
+
mailConfig: unknown;
|
|
44
46
|
|
|
45
|
-
constructor({ secret, tokenExpiresInSeconds }: AuthControllerOptions) {
|
|
47
|
+
constructor({ secret, tokenExpiresInSeconds, mailConfig }: AuthControllerOptions) {
|
|
46
48
|
this.secret = secret;
|
|
47
49
|
this.accessTokenTtlSeconds = tokenExpiresInSeconds ?? DEFAULT_ACCESS_TOKEN_TTL_SECONDS;
|
|
50
|
+
this.mailConfig = mailConfig;
|
|
48
51
|
}
|
|
49
52
|
|
|
50
53
|
async register(req: TylixRequest, res: TylixResponse) {
|
|
@@ -63,12 +66,12 @@ export class AuthController {
|
|
|
63
66
|
return res.status(500).json({ error: "Failed to create user." });
|
|
64
67
|
}
|
|
65
68
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
69
|
+
const verificationToken = signToken(
|
|
70
|
+
{ userId: user.id, purpose: "email-verify" },
|
|
71
|
+
this.secret,
|
|
72
|
+
{ expiresInSeconds: 60 * 60 * 24 },
|
|
73
|
+
);
|
|
74
|
+
await sendVerificationEmail(user, verificationToken, this.mailConfig);
|
|
72
75
|
|
|
73
76
|
return res.status(201).json({
|
|
74
77
|
user: User.toSafeObject(user),
|
|
@@ -195,10 +198,10 @@ export class AuthController {
|
|
|
195
198
|
const users = await User.all();
|
|
196
199
|
const user = users.find((u) => u.email === req.body.email);
|
|
197
200
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
201
|
+
if (user) {
|
|
202
|
+
const { rawToken } = await PasswordReset.issue(user.email);
|
|
203
|
+
await sendPasswordResetEmail(user, rawToken, this.mailConfig);
|
|
204
|
+
}
|
|
202
205
|
|
|
203
206
|
return res.json({
|
|
204
207
|
ok: true,
|
|
@@ -33,9 +33,10 @@ import {
|
|
|
33
33
|
const DEFAULT_ACCESS_TOKEN_TTL_SECONDS = 15 * 60; // 15 minutes, used if config doesn't specify one
|
|
34
34
|
|
|
35
35
|
export class AuthController {
|
|
36
|
-
constructor({ secret, tokenExpiresInSeconds } = {}) {
|
|
36
|
+
constructor({ secret, tokenExpiresInSeconds, mailConfig } = {}) {
|
|
37
37
|
this.secret = secret;
|
|
38
38
|
this.accessTokenTtlSeconds = tokenExpiresInSeconds ?? DEFAULT_ACCESS_TOKEN_TTL_SECONDS;
|
|
39
|
+
this.mailConfig = mailConfig;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
async register(req, res) {
|
|
@@ -52,11 +53,11 @@ export class AuthController {
|
|
|
52
53
|
const user = await User.create(req.body);
|
|
53
54
|
|
|
54
55
|
const verificationToken = signToken(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
56
|
+
{ userId: user.id, purpose: "email-verify" },
|
|
57
|
+
this.secret,
|
|
58
|
+
{ expiresInSeconds: 60 * 60 * 24 },
|
|
59
|
+
);
|
|
60
|
+
await sendVerificationEmail(user, verificationToken, this.mailConfig);
|
|
60
61
|
|
|
61
62
|
return res.status(201).json({
|
|
62
63
|
user: User.toSafeObject(user),
|
|
@@ -185,10 +186,10 @@ export class AuthController {
|
|
|
185
186
|
|
|
186
187
|
// Always respond the same way whether or not the email exists,
|
|
187
188
|
// so this endpoint can't be used to enumerate registered users.
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
189
|
+
if (user) {
|
|
190
|
+
const { rawToken } = await PasswordReset.issue(user.email);
|
|
191
|
+
await sendPasswordResetEmail(user, rawToken, this.mailConfig);
|
|
192
|
+
}
|
|
192
193
|
|
|
193
194
|
return res.json({
|
|
194
195
|
ok: true,
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* Version: 0.1.0
|
|
6
6
|
* Feature: Auth
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* this just logs the links so you can click them during development.
|
|
8
|
+
* Sends real emails through @tylix/mail. Switch drivers (log → smtp)
|
|
9
|
+
* in your .env / tylix.config.ts — no code changes needed here.
|
|
11
10
|
*/
|
|
12
11
|
|
|
12
|
+
import { VerifyEmailMail } from "./VerifyEmailMail.js";
|
|
13
|
+
import { ResetPasswordMail } from "./ResetPasswordMail.js";
|
|
14
|
+
|
|
13
15
|
const APP_URL = process.env.APP_URL ?? "http://localhost:3000";
|
|
14
16
|
|
|
15
17
|
interface MailableUser {
|
|
@@ -17,12 +19,12 @@ interface MailableUser {
|
|
|
17
19
|
[key: string]: unknown;
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export async function sendVerificationEmail(user: MailableUser, token: string) {
|
|
22
|
+
export async function sendVerificationEmail(user: MailableUser, token: string, mailConfig: unknown) {
|
|
21
23
|
const verifyUrl = `${APP_URL}/api/auth/verify-email?token=${token}`;
|
|
22
|
-
|
|
24
|
+
await new VerifyEmailMail(verifyUrl).send(user.email, mailConfig);
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
export async function sendPasswordResetEmail(user: MailableUser, token: string) {
|
|
27
|
+
export async function sendPasswordResetEmail(user: MailableUser, token: string, mailConfig: unknown) {
|
|
26
28
|
const resetUrl = `${APP_URL}/reset-password?token=${token}&email=${encodeURIComponent(user.email)}`;
|
|
27
|
-
|
|
29
|
+
await new ResetPasswordMail(resetUrl).send(user.email, mailConfig);
|
|
28
30
|
}
|
|
@@ -5,19 +5,21 @@
|
|
|
5
5
|
* Version: 0.1.0
|
|
6
6
|
* Feature: Auth
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* this just logs the links so you can click them during development.
|
|
8
|
+
* Sends real emails through @tylix/mail. Switch drivers (log → smtp)
|
|
9
|
+
* in your .env / tylix.config.js — no code changes needed here.
|
|
11
10
|
*/
|
|
12
11
|
|
|
12
|
+
import { VerifyEmailMail } from "./VerifyEmailMail.js";
|
|
13
|
+
import { ResetPasswordMail } from "./ResetPasswordMail.js";
|
|
14
|
+
|
|
13
15
|
const APP_URL = process.env.APP_URL ?? "http://localhost:3000";
|
|
14
16
|
|
|
15
|
-
export async function sendVerificationEmail(user, token) {
|
|
17
|
+
export async function sendVerificationEmail(user, token, mailConfig) {
|
|
16
18
|
const verifyUrl = `${APP_URL}/api/auth/verify-email?token=${token}`;
|
|
17
|
-
|
|
19
|
+
await new VerifyEmailMail(verifyUrl).send(user.email, mailConfig);
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export async function sendPasswordResetEmail(user, token) {
|
|
22
|
+
export async function sendPasswordResetEmail(user, token, mailConfig) {
|
|
21
23
|
const resetUrl = `${APP_URL}/reset-password?token=${token}&email=${encodeURIComponent(user.email)}`;
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
+
await new ResetPasswordMail(resetUrl).send(user.email, mailConfig);
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Mailable } from '@tylix/mail';
|
|
2
|
+
|
|
3
|
+
export class ResetPasswordMail extends Mailable {
|
|
4
|
+
constructor(private resetUrl: string) {
|
|
5
|
+
super();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
build() {
|
|
9
|
+
return {
|
|
10
|
+
subject: 'Reset your password',
|
|
11
|
+
view: 'reset-password',
|
|
12
|
+
data: { resetUrl: this.resetUrl },
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Mailable } from '@tylix/mail';
|
|
2
|
+
|
|
3
|
+
export class ResetPasswordMail extends Mailable {
|
|
4
|
+
constructor(resetUrl) {
|
|
5
|
+
super();
|
|
6
|
+
this.resetUrl = resetUrl;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
build() {
|
|
10
|
+
return {
|
|
11
|
+
subject: 'Reset your password',
|
|
12
|
+
view: 'reset-password',
|
|
13
|
+
data: { resetUrl: this.resetUrl },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Mailable } from '@tylix/mail';
|
|
2
|
+
|
|
3
|
+
export class VerifyEmailMail extends Mailable {
|
|
4
|
+
constructor(private verifyUrl: string) {
|
|
5
|
+
super();
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
build() {
|
|
9
|
+
return {
|
|
10
|
+
subject: 'Verify your email',
|
|
11
|
+
view: 'verify-email',
|
|
12
|
+
data: { verifyUrl: this.verifyUrl },
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Mailable } from '@tylix/mail';
|
|
2
|
+
|
|
3
|
+
export class VerifyEmailMail extends Mailable {
|
|
4
|
+
constructor(verifyUrl) {
|
|
5
|
+
super();
|
|
6
|
+
this.verifyUrl = verifyUrl;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
build() {
|
|
10
|
+
return {
|
|
11
|
+
subject: 'Verify your email',
|
|
12
|
+
view: 'verify-email',
|
|
13
|
+
data: { verifyUrl: this.verifyUrl },
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|