stackkit 0.2.8 → 0.2.9
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/README.md +4 -0
- package/dist/lib/fs/files.js +1 -1
- package/dist/lib/generation/code-generator.d.ts +2 -7
- package/dist/lib/generation/code-generator.js +120 -56
- package/dist/lib/utils/fs-helpers.d.ts +1 -1
- package/modules/auth/authjs/generator.json +16 -16
- package/modules/auth/better-auth/files/express/middlewares/authorize.ts +121 -41
- package/modules/auth/better-auth/files/express/modules/auth/auth.controller.ts +257 -0
- package/modules/auth/better-auth/files/express/modules/auth/auth.interface.ts +23 -0
- package/modules/auth/better-auth/files/express/modules/auth/auth.route.ts +22 -0
- package/modules/auth/better-auth/files/express/modules/auth/auth.service.ts +403 -0
- package/modules/auth/better-auth/files/express/templates/google-redirect.ejs +91 -0
- package/modules/auth/better-auth/files/express/templates/otp.ejs +87 -0
- package/modules/auth/better-auth/files/express/types/express.d.ts +6 -8
- package/modules/auth/better-auth/files/express/utils/cookie.ts +19 -0
- package/modules/auth/better-auth/files/express/utils/jwt.ts +34 -0
- package/modules/auth/better-auth/files/express/utils/token.ts +66 -0
- package/modules/auth/better-auth/files/nextjs/api/auth/[...all]/route.ts +1 -1
- package/modules/auth/better-auth/files/nextjs/lib/auth/auth-guards.ts +11 -1
- package/modules/auth/better-auth/files/nextjs/templates/email-otp.tsx +74 -0
- package/modules/auth/better-auth/files/shared/config/env.ts +113 -0
- package/modules/auth/better-auth/files/shared/lib/auth-client.ts +1 -1
- package/modules/auth/better-auth/files/shared/lib/auth.ts +151 -62
- package/modules/auth/better-auth/files/shared/prisma/schema.prisma +22 -11
- package/modules/auth/better-auth/files/shared/utils/email.ts +71 -0
- package/modules/auth/better-auth/generator.json +159 -81
- package/modules/database/mongoose/generator.json +18 -18
- package/modules/database/prisma/generator.json +44 -44
- package/package.json +1 -1
- package/templates/express/env.example +2 -2
- package/templates/express/eslint.config.mjs +7 -0
- package/templates/express/node_modules/.bin/acorn +17 -0
- package/templates/express/node_modules/.bin/eslint +17 -0
- package/templates/express/node_modules/.bin/tsc +17 -0
- package/templates/express/node_modules/.bin/tsserver +17 -0
- package/templates/express/node_modules/.bin/tsx +17 -0
- package/templates/express/package.json +12 -6
- package/templates/express/src/app.ts +15 -7
- package/templates/express/src/config/cors.ts +8 -7
- package/templates/express/src/config/env.ts +26 -5
- package/templates/express/src/config/logger.ts +2 -2
- package/templates/express/src/config/rate-limit.ts +2 -2
- package/templates/express/src/modules/health/health.controller.ts +13 -11
- package/templates/express/src/routes/index.ts +1 -6
- package/templates/express/src/server.ts +12 -12
- package/templates/express/src/shared/errors/app-error.ts +16 -0
- package/templates/express/src/shared/middlewares/error.middleware.ts +154 -12
- package/templates/express/src/shared/middlewares/not-found.middleware.ts +2 -1
- package/templates/express/src/shared/utils/catch-async.ts +11 -0
- package/templates/express/src/shared/utils/pagination.ts +6 -1
- package/templates/express/src/shared/utils/send-response.ts +25 -0
- package/templates/nextjs/lib/env.ts +19 -8
- package/modules/auth/better-auth/files/shared/lib/email/email-service.ts +0 -33
- package/modules/auth/better-auth/files/shared/lib/email/email-templates.ts +0 -89
- package/templates/express/eslint.config.cjs +0 -42
- package/templates/express/src/config/helmet.ts +0 -5
- package/templates/express/src/modules/health/health.service.ts +0 -6
- package/templates/express/src/shared/errors/error-codes.ts +0 -9
- package/templates/express/src/shared/logger/logger.ts +0 -20
- package/templates/express/src/shared/utils/async-handler.ts +0 -9
- package/templates/express/src/shared/utils/response.ts +0 -9
|
@@ -1,46 +1,42 @@
|
|
|
1
|
+
import { Role, UserStatus } from "@prisma/client";
|
|
1
2
|
import { betterAuth } from "better-auth";
|
|
3
|
+
import { bearer, emailOTP } from "better-auth/plugins";
|
|
2
4
|
{{#if combo == "prisma:express"}}
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
getVerificationEmailTemplate,
|
|
7
|
-
} from "../../shared/email/email-templates";
|
|
8
|
-
import { prisma } from "../../database/prisma";
|
|
5
|
+
import { envVars } from "../config/env";
|
|
6
|
+
import { sendEmail } from "../shared/utils/email";
|
|
7
|
+
import { prisma } from "../database/prisma";
|
|
9
8
|
import { prismaAdapter } from "better-auth/adapters/prisma";
|
|
10
9
|
{{/if}}
|
|
11
10
|
|
|
12
11
|
{{#if combo == "prisma:nextjs"}}
|
|
13
|
-
import { getPasswordResetEmailTemplate, getVerificationEmailTemplate } from "../service/email/email-templates";
|
|
14
12
|
import { sendEmail } from "../service/email/email-service";
|
|
15
13
|
import { prisma } from "../database/prisma";
|
|
14
|
+
import { envVars } from "@/lib/env";
|
|
16
15
|
import { prismaAdapter } from "better-auth/adapters/prisma";
|
|
17
16
|
{{/if}}
|
|
18
17
|
|
|
19
18
|
{{#if combo == "mongoose:express"}}
|
|
19
|
+
import { envVars } from "../config/env";
|
|
20
20
|
import { sendEmail } from "../../shared/email/email-service";
|
|
21
|
-
import {
|
|
22
|
-
getPasswordResetEmailTemplate,
|
|
23
|
-
getVerificationEmailTemplate,
|
|
24
|
-
} from "../../shared/email/email-templates";
|
|
25
21
|
import { mongoose } from "../../database/mongoose";
|
|
26
22
|
import { mongodbAdapter } from "better-auth/adapters/mongodb";
|
|
27
23
|
{{/if}}
|
|
28
24
|
|
|
29
25
|
{{#if combo == "mongoose:nextjs"}}
|
|
30
|
-
import { getPasswordResetEmailTemplate, getVerificationEmailTemplate } from "../service/email/email-templates";
|
|
31
26
|
import { sendEmail } from "../service/email/email-service";
|
|
32
27
|
import { mongoose } from "../database/mongoose";
|
|
28
|
+
import { envVars } from "@/lib/env";
|
|
33
29
|
import { mongodbAdapter } from "better-auth/adapters/mongodb";
|
|
34
30
|
{{/if}}
|
|
35
31
|
|
|
36
|
-
export async function initAuth() {
|
|
37
32
|
{{#if database == "mongoose"}}
|
|
33
|
+
export async function initAuth() {
|
|
38
34
|
const mongooseInstance = await mongoose();
|
|
39
35
|
const client = mongooseInstance.connection.getClient();
|
|
40
36
|
const db = client.db();
|
|
41
37
|
{{/if}}
|
|
42
38
|
|
|
43
|
-
return betterAuth({
|
|
39
|
+
{{#if database == "mongoose"}}return{{else}}export const auth = {{/if}} betterAuth({
|
|
44
40
|
{{#if database == "prisma"}}
|
|
45
41
|
database: prismaAdapter(prisma, {
|
|
46
42
|
provider: "{{prismaProvider}}",
|
|
@@ -49,76 +45,169 @@ return betterAuth({
|
|
|
49
45
|
{{#if database == "mongoose"}}
|
|
50
46
|
database: mongodbAdapter(db, { client }),
|
|
51
47
|
{{/if}}
|
|
52
|
-
baseURL:
|
|
53
|
-
secret:
|
|
54
|
-
trustedOrigins: [
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
role: {
|
|
58
|
-
type: "string",
|
|
59
|
-
defaultValue: "USER",
|
|
60
|
-
required: true,
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
},
|
|
48
|
+
baseURL: envVars.BETTER_AUTH_URL,
|
|
49
|
+
secret: envVars.BETTER_AUTH_SECRET,
|
|
50
|
+
trustedOrigins: [
|
|
51
|
+
envVars.FRONTEND_URL || envVars.BETTER_AUTH_URL || "http://localhost:3000",
|
|
52
|
+
],
|
|
64
53
|
emailAndPassword: {
|
|
65
54
|
enabled: true,
|
|
66
55
|
requireEmailVerification: true,
|
|
67
|
-
sendResetPassword: async ({ user, url }) => {
|
|
68
|
-
const { html, text } = getPasswordResetEmailTemplate(user, url);
|
|
69
|
-
await sendEmail({
|
|
70
|
-
to: user.email,
|
|
71
|
-
subject: "Reset Your Password",
|
|
72
|
-
text,
|
|
73
|
-
html,
|
|
74
|
-
});
|
|
75
|
-
},
|
|
76
56
|
},
|
|
77
57
|
socialProviders: {
|
|
78
58
|
google: {
|
|
79
|
-
|
|
59
|
+
clientId: envVars.GOOGLE_CLIENT_ID as string,
|
|
60
|
+
clientSecret: envVars.GOOGLE_CLIENT_SECRET as string,
|
|
61
|
+
accessType: "offline",
|
|
80
62
|
prompt: "select_account consent",
|
|
81
|
-
|
|
82
|
-
|
|
63
|
+
mapProfileToUser: () => {
|
|
64
|
+
return {
|
|
65
|
+
role: Role.USER,
|
|
66
|
+
status: UserStatus.ACTIVE,
|
|
67
|
+
needPasswordChange: false,
|
|
68
|
+
emailVerified: true,
|
|
69
|
+
isDeleted: false,
|
|
70
|
+
deletedAt: null,
|
|
71
|
+
};
|
|
72
|
+
},
|
|
83
73
|
},
|
|
84
74
|
},
|
|
85
75
|
emailVerification: {
|
|
86
|
-
|
|
87
|
-
const { html, text } = getVerificationEmailTemplate(user, url);
|
|
88
|
-
await sendEmail({
|
|
89
|
-
to: user.email,
|
|
90
|
-
subject: "Verify Your Email Address",
|
|
91
|
-
text,
|
|
92
|
-
html,
|
|
93
|
-
});
|
|
94
|
-
},
|
|
76
|
+
sendOnSignUp: true,
|
|
95
77
|
sendOnSignIn: true,
|
|
78
|
+
autoSignInAfterVerification: true,
|
|
96
79
|
},
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
80
|
+
user: {
|
|
81
|
+
additionalFields: {
|
|
82
|
+
role: {
|
|
83
|
+
type: "string",
|
|
84
|
+
required: true,
|
|
85
|
+
defaultValue: Role.USER,
|
|
86
|
+
},
|
|
87
|
+
status: {
|
|
88
|
+
type: "string",
|
|
89
|
+
required: true,
|
|
90
|
+
defaultValue: UserStatus.ACTIVE,
|
|
91
|
+
},
|
|
92
|
+
needPasswordChange: {
|
|
93
|
+
type: "boolean",
|
|
94
|
+
required: true,
|
|
95
|
+
defaultValue: false,
|
|
96
|
+
},
|
|
97
|
+
isDeleted: {
|
|
98
|
+
type: "boolean",
|
|
99
|
+
required: true,
|
|
100
|
+
defaultValue: false,
|
|
101
|
+
},
|
|
102
|
+
deletedAt: {
|
|
103
|
+
type: "date",
|
|
104
|
+
required: false,
|
|
105
|
+
defaultValue: null,
|
|
106
|
+
},
|
|
105
107
|
},
|
|
106
108
|
},
|
|
109
|
+
plugins: [
|
|
110
|
+
bearer(),
|
|
111
|
+
emailOTP({
|
|
112
|
+
overrideDefaultEmailVerification: true,
|
|
113
|
+
async sendVerificationOTP({ email, otp, type }) {
|
|
114
|
+
if (type === "email-verification") {
|
|
115
|
+
const user = await prisma.user.findUnique({
|
|
116
|
+
where: {
|
|
117
|
+
email,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (!user) {
|
|
122
|
+
console.error(
|
|
123
|
+
`User with email ${email} not found. Cannot send verification OTP.`,
|
|
124
|
+
);
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (user && user.role === Role.SUPER_ADMIN) {
|
|
129
|
+
console.log(
|
|
130
|
+
`User with email ${email} is a super admin. Skipping sending verification OTP.`,
|
|
131
|
+
);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (user && !user.emailVerified) {
|
|
136
|
+
sendEmail({
|
|
137
|
+
to: email,
|
|
138
|
+
subject: "Verify your email",
|
|
139
|
+
templateName: "otp",
|
|
140
|
+
templateData: {
|
|
141
|
+
name: user.name,
|
|
142
|
+
otp,
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
} else if (type === "forget-password") {
|
|
147
|
+
const user = await prisma.user.findUnique({
|
|
148
|
+
where: {
|
|
149
|
+
email,
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
if (user) {
|
|
154
|
+
sendEmail({
|
|
155
|
+
to: email,
|
|
156
|
+
subject: "Password Reset OTP",
|
|
157
|
+
templateName: "otp",
|
|
158
|
+
templateData: {
|
|
159
|
+
name: user.name,
|
|
160
|
+
otp,
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
expiresIn: 2 * 60, // 2 minutes in seconds
|
|
167
|
+
otpLength: 6,
|
|
168
|
+
}),
|
|
169
|
+
],
|
|
107
170
|
session: {
|
|
171
|
+
expiresIn: 60 * 60 * 60 * 24, // 1 day in seconds
|
|
172
|
+
updateAge: 60 * 60 * 60 * 24, // 1 day in seconds
|
|
108
173
|
cookieCache: {
|
|
109
174
|
enabled: true,
|
|
110
|
-
maxAge: 60 * 60 *
|
|
175
|
+
maxAge: 60 * 60 * 60 * 24, // 1 day in seconds
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
redirectURLs: {
|
|
179
|
+
signIn: `${envVars.BETTER_AUTH_URL}/api/v1/auth/google/success`,
|
|
180
|
+
},
|
|
181
|
+
advanced: {
|
|
182
|
+
// disableCSRFCheck: true,
|
|
183
|
+
useSecureCookies: false,
|
|
184
|
+
cookies: {
|
|
185
|
+
state: {
|
|
186
|
+
attributes: {
|
|
187
|
+
sameSite: "none",
|
|
188
|
+
secure: true,
|
|
189
|
+
httpOnly: true,
|
|
190
|
+
path: "/",
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
sessionToken: {
|
|
194
|
+
attributes: {
|
|
195
|
+
sameSite: "none",
|
|
196
|
+
secure: true,
|
|
197
|
+
httpOnly: true,
|
|
198
|
+
path: "/",
|
|
199
|
+
},
|
|
200
|
+
},
|
|
111
201
|
},
|
|
112
|
-
|
|
113
|
-
updateAge: 60 * 60 * 24,
|
|
114
|
-
cookieName: "better-auth.session_token",
|
|
115
|
-
}
|
|
202
|
+
},
|
|
116
203
|
})
|
|
204
|
+
{{#if database == "mongoose"}}
|
|
117
205
|
};
|
|
118
206
|
|
|
119
|
-
export let auth:
|
|
207
|
+
export let auth: ReturnType<typeof betterAuth> | undefined = undefined;
|
|
120
208
|
|
|
121
209
|
export async function setupAuth() {
|
|
122
210
|
auth = await initAuth();
|
|
123
211
|
return auth;
|
|
124
|
-
}
|
|
212
|
+
}
|
|
213
|
+
{{/if}}
|
|
@@ -4,16 +4,20 @@
|
|
|
4
4
|
{{#var defaultId = @default(cuid())}}
|
|
5
5
|
{{/if}}
|
|
6
6
|
model User {
|
|
7
|
-
id
|
|
8
|
-
name
|
|
9
|
-
email
|
|
10
|
-
emailVerified
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
7
|
+
id String @id {{defaultId}}
|
|
8
|
+
name String
|
|
9
|
+
email String
|
|
10
|
+
emailVerified Boolean @default(false)
|
|
11
|
+
role Role @default(USER)
|
|
12
|
+
status UserStatus @default(ACTIVE)
|
|
13
|
+
needPasswordChange Boolean @default(false)
|
|
14
|
+
isDeleted Boolean @default(false)
|
|
15
|
+
deletedAt DateTime?
|
|
16
|
+
image String?
|
|
17
|
+
createdAt DateTime @default(now())
|
|
18
|
+
updatedAt DateTime @updatedAt
|
|
19
|
+
sessions Session[]
|
|
20
|
+
accounts Account[]
|
|
17
21
|
|
|
18
22
|
@@unique([email])
|
|
19
23
|
@@map("user")
|
|
@@ -67,6 +71,13 @@ model Verification {
|
|
|
67
71
|
}
|
|
68
72
|
|
|
69
73
|
enum Role {
|
|
70
|
-
|
|
74
|
+
SUPER_ADMIN
|
|
71
75
|
ADMIN
|
|
76
|
+
USER
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
enum UserStatus {
|
|
80
|
+
ACTIVE
|
|
81
|
+
BLOCKED
|
|
82
|
+
DELETED
|
|
72
83
|
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import nodemailer from "nodemailer";
|
|
2
|
+
{{#if framework == "express"}}
|
|
3
|
+
import ejs from "ejs";
|
|
4
|
+
import status from "http-status";
|
|
5
|
+
import path from "path";
|
|
6
|
+
import { envVars } from "../../config/env";
|
|
7
|
+
import { AppError } from "../errors/app-error";
|
|
8
|
+
{{/if}}
|
|
9
|
+
{{#if framework == "nextjs"}}
|
|
10
|
+
import nodemailer from "nodemailer";
|
|
11
|
+
import { renderEmailTemplate } from "../email/otp-template";
|
|
12
|
+
import { envVars } from "../env";
|
|
13
|
+
{{/if}}
|
|
14
|
+
|
|
15
|
+
const transporter = nodemailer.createTransport({
|
|
16
|
+
host: envVars.EMAIL_SENDER.SMTP_HOST,
|
|
17
|
+
secure: true,
|
|
18
|
+
auth: {
|
|
19
|
+
user: envVars.EMAIL_SENDER.SMTP_USER,
|
|
20
|
+
pass: envVars.EMAIL_SENDER.SMTP_PASS,
|
|
21
|
+
},
|
|
22
|
+
port: Number(envVars.EMAIL_SENDER.SMTP_PORT),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
interface SendEmailOptions {
|
|
26
|
+
to: string;
|
|
27
|
+
subject: string;
|
|
28
|
+
templateName: string;
|
|
29
|
+
templateData: Record<string, string | number | boolean | object>;
|
|
30
|
+
attachments?: {
|
|
31
|
+
filename: string;
|
|
32
|
+
content: Buffer | string;
|
|
33
|
+
contentType: string;
|
|
34
|
+
}[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const sendEmail = async ({
|
|
38
|
+
subject,
|
|
39
|
+
templateData,
|
|
40
|
+
templateName,
|
|
41
|
+
to,
|
|
42
|
+
attachments,
|
|
43
|
+
}: SendEmailOptions) => {
|
|
44
|
+
try {
|
|
45
|
+
{{#if framework == "express"}}
|
|
46
|
+
const templatePath = path.resolve(
|
|
47
|
+
process.cwd(),
|
|
48
|
+
`src/templates/${templateName}.ejs`,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const html = await ejs.renderFile(templatePath, templateData);
|
|
52
|
+
{{/if}}
|
|
53
|
+
{{#if framework == "nextjs"}}
|
|
54
|
+
const html = renderEmailTemplate(templateName, templateData);
|
|
55
|
+
{{/if}}
|
|
56
|
+
|
|
57
|
+
await transporter.sendMail({
|
|
58
|
+
from: envVars.EMAIL_SENDER.SMTP_FROM,
|
|
59
|
+
to: to,
|
|
60
|
+
subject: subject,
|
|
61
|
+
html: html,
|
|
62
|
+
attachments: attachments?.map((attachment) => ({
|
|
63
|
+
filename: attachment.filename,
|
|
64
|
+
content: attachment.content,
|
|
65
|
+
contentType: attachment.contentType,
|
|
66
|
+
})),
|
|
67
|
+
});
|
|
68
|
+
} catch {
|
|
69
|
+
throw new AppError(status.INTERNAL_SERVER_ERROR, "Failed to send email");
|
|
70
|
+
}
|
|
71
|
+
};
|