create-oikko-mongo 1.0.0

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.
Files changed (53) hide show
  1. package/README.md +275 -0
  2. package/bin/index.js +302 -0
  3. package/package.json +36 -0
  4. package/template/.env.example +82 -0
  5. package/template/_gitignore +6 -0
  6. package/template/package.json +50 -0
  7. package/template/src/app/config/env.ts +190 -0
  8. package/template/src/app/config/passport.ts +112 -0
  9. package/template/src/app/config/r2.config.ts +15 -0
  10. package/template/src/app/config/redis.config.ts +21 -0
  11. package/template/src/app/errorHelpers/AppError.ts +16 -0
  12. package/template/src/app/helpers/handleCastError.ts +10 -0
  13. package/template/src/app/helpers/handleDuplicateError.ts +12 -0
  14. package/template/src/app/helpers/handleValidationError.ts +19 -0
  15. package/template/src/app/helpers/handleZodError.ts +26 -0
  16. package/template/src/app/interfaces/error.Types.ts +10 -0
  17. package/template/src/app/interfaces/index.d.ts +8 -0
  18. package/template/src/app/middlewares/checkAuth.ts +54 -0
  19. package/template/src/app/middlewares/globalErrorHandler.ts +78 -0
  20. package/template/src/app/middlewares/notFound.ts +18 -0
  21. package/template/src/app/middlewares/requestLogger.ts +20 -0
  22. package/template/src/app/middlewares/validateRequest.ts +21 -0
  23. package/template/src/app/modules/auth/auth.controller.ts +184 -0
  24. package/template/src/app/modules/auth/auth.route.ts +27 -0
  25. package/template/src/app/modules/auth/auth.service.ts +136 -0
  26. package/template/src/app/modules/auth/auth.validation.ts +53 -0
  27. package/template/src/app/modules/auth/jwt.ts +16 -0
  28. package/template/src/app/modules/otp/otp.controller.ts +32 -0
  29. package/template/src/app/modules/otp/otp.route.ts +12 -0
  30. package/template/src/app/modules/otp/otp.service.ts +120 -0
  31. package/template/src/app/modules/otp/otp.validation.ts +27 -0
  32. package/template/src/app/modules/user/user.constant.ts +1 -0
  33. package/template/src/app/modules/user/user.controller.ts +55 -0
  34. package/template/src/app/modules/user/user.interface.ts +41 -0
  35. package/template/src/app/modules/user/user.model.ts +44 -0
  36. package/template/src/app/modules/user/user.route.ts +20 -0
  37. package/template/src/app/modules/user/user.service.ts +79 -0
  38. package/template/src/app/modules/user/user.validation.ts +77 -0
  39. package/template/src/app/routes/index.ts +25 -0
  40. package/template/src/app/utils/catchAsync.ts +10 -0
  41. package/template/src/app/utils/constants.ts +1 -0
  42. package/template/src/app/utils/logger.ts +68 -0
  43. package/template/src/app/utils/queryBuilder.ts +83 -0
  44. package/template/src/app/utils/sendEmail.ts +62 -0
  45. package/template/src/app/utils/sendResponse.ts +26 -0
  46. package/template/src/app/utils/setCookie.ts +34 -0
  47. package/template/src/app/utils/templates/forgetPassword.ejs +76 -0
  48. package/template/src/app/utils/templates/otp.ejs +76 -0
  49. package/template/src/app/utils/templates/resetPassword.ejs +121 -0
  50. package/template/src/app/utils/userTokens.ts +52 -0
  51. package/template/src/app.ts +44 -0
  52. package/template/src/server.ts +66 -0
  53. package/template/tsconfig.json +14 -0
@@ -0,0 +1,76 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <title>Your OTP Code</title>
7
+ <style>
8
+ body {
9
+ background-color: #f6f9fc;
10
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
11
+ margin: 0;
12
+ padding: 0;
13
+ color: #333;
14
+ }
15
+
16
+ .container {
17
+ max-width: 600px;
18
+ margin: 40px auto;
19
+ background-color: #ffffff;
20
+ padding: 30px 40px;
21
+ border-radius: 10px;
22
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
23
+ }
24
+
25
+ h2 {
26
+ color: #2c3e50;
27
+ font-weight: 600;
28
+ }
29
+
30
+ .otp-box {
31
+ font-size: 28px;
32
+ font-weight: bold;
33
+ background: #f0f4f8;
34
+ padding: 15px 25px;
35
+ text-align: center;
36
+ border-radius: 8px;
37
+ margin: 20px 0;
38
+ letter-spacing: 2px;
39
+ color: #34495e;
40
+ border: 2px dashed #3498db;
41
+ }
42
+
43
+ p {
44
+ font-size: 16px;
45
+ line-height: 1.6;
46
+ }
47
+
48
+ .footer {
49
+ margin-top: 30px;
50
+ font-size: 13px;
51
+ text-align: center;
52
+ color: #999;
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <div class="container">
59
+ <h2>Hello <%= name %>,</h2>
60
+ <p>Use the OTP code below to complete your verification:</p>
61
+
62
+ <div class="otp-box">
63
+ <%= otp %>
64
+ </div>
65
+
66
+ <p>This code is valid for <strong>2 minutes</strong>. Please do not share it with anyone.</p>
67
+
68
+ <p>If you didn’t request this, you can ignore this message.</p>
69
+
70
+ <div class="footer">
71
+ &copy; <%= new Date().getFullYear() %> Example. All rights reserved.
72
+ </div>
73
+ </div>
74
+ </body>
75
+
76
+ </html>
@@ -0,0 +1,121 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <title>Reset Your Password</title>
7
+ <style>
8
+ body {
9
+ background-color: #f6f9fc;
10
+ font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
11
+ margin: 0;
12
+ padding: 0;
13
+ color: #333;
14
+ }
15
+
16
+ .container {
17
+ max-width: 600px;
18
+ margin: 40px auto;
19
+ background-color: #ffffff;
20
+ padding: 30px 40px;
21
+ border-radius: 10px;
22
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.06);
23
+ }
24
+
25
+ h2 {
26
+ color: #2c3e50;
27
+ font-weight: 600;
28
+ margin-bottom: 20px;
29
+ }
30
+
31
+ p {
32
+ font-size: 16px;
33
+ line-height: 1.6;
34
+ margin-bottom: 15px;
35
+ }
36
+
37
+ .reset-button {
38
+ display: inline-block;
39
+ background-color: #3498db;
40
+ color: #ffffff;
41
+ padding: 14px 35px;
42
+ text-decoration: none;
43
+ border-radius: 6px;
44
+ font-weight: 600;
45
+ margin: 20px 0;
46
+ font-size: 16px;
47
+ transition: background-color 0.3s;
48
+ }
49
+
50
+ .reset-button:hover {
51
+ background-color: #2980b9;
52
+ }
53
+
54
+ .button-container {
55
+ text-align: center;
56
+ margin: 30px 0;
57
+ }
58
+
59
+ .link-box {
60
+ background: #f0f4f8;
61
+ padding: 15px;
62
+ border-radius: 6px;
63
+ word-break: break-all;
64
+ margin: 20px 0;
65
+ border-left: 4px solid #3498db;
66
+ font-size: 14px;
67
+ color: #555;
68
+ }
69
+
70
+ .warning {
71
+ background-color: #fff3cd;
72
+ border-left: 4px solid #ffc107;
73
+ padding: 12px 15px;
74
+ margin: 20px 0;
75
+ border-radius: 4px;
76
+ font-size: 14px;
77
+ }
78
+
79
+ .footer {
80
+ margin-top: 30px;
81
+ padding-top: 20px;
82
+ border-top: 1px solid #eee;
83
+ font-size: 13px;
84
+ text-align: center;
85
+ color: #999;
86
+ }
87
+ </style>
88
+ </head>
89
+
90
+ <body>
91
+ <div class="container">
92
+ <h2>Hello <%= name %>,</h2>
93
+
94
+ <p>We received a request to reset your password for your Example account.</p>
95
+
96
+ <p>Click the button below to reset your password:</p>
97
+
98
+ <div class="button-container">
99
+ <a href="<%= resetUILink %>" class="reset-button">Reset Password</a>
100
+ </div>
101
+
102
+ <p>Or copy and paste this link into your browser:</p>
103
+ <div class="link-box">
104
+ <%= resetUILink %>
105
+ </div>
106
+
107
+ <div class="warning">
108
+ <strong>⏱️ Important:</strong> This link will expire in <strong>10 minutes</strong> for security reasons.
109
+ </div>
110
+
111
+ <p>If you didn't request a password reset, please ignore this email or contact support if you have concerns.</p>
112
+
113
+ <p>Thanks,<br />The Example Team</p>
114
+
115
+ <div class="footer">
116
+ &copy; <%= new Date().getFullYear() %> Example. All rights reserved.
117
+ </div>
118
+ </div>
119
+ </body>
120
+
121
+ </html>
@@ -0,0 +1,52 @@
1
+ import { JwtPayload } from "jsonwebtoken"
2
+ import AppError from "../errorHelpers/AppError"
3
+ import httpStatus from "http-status-codes"
4
+ import { IsActive, IUser } from "../modules/user/user.interface"
5
+ import { generateToken, verifyToken } from "../modules/auth/jwt"
6
+ import { User } from "../modules/user/user.model"
7
+ import { envVars } from "../config/env"
8
+
9
+
10
+ export const createUserTokens = (user: Partial<IUser>) => {
11
+ const jwtPayload = {
12
+ userId: user._id,
13
+ email: user.email,
14
+ role: user.role,
15
+ }
16
+
17
+ const accessToken = generateToken(jwtPayload, envVars.JWT_ACCESS_SECRET, envVars.JWT_ACCESS_EXPIRES)
18
+ const refreshToken = generateToken(jwtPayload, envVars.JWT_REFRESH_SECRET, envVars.JWT_REFRESH_EXPIRES)
19
+
20
+ return {
21
+ accessToken,
22
+ refreshToken
23
+ }
24
+ }
25
+
26
+ export const createNewAccessTokenWithRefreshToken = async (refreshToken: string) => {
27
+ const verifiedRefreshToken = verifyToken(refreshToken, envVars.JWT_REFRESH_SECRET) as JwtPayload
28
+
29
+
30
+
31
+ const isUserExist = await User.findOne({ email: verifiedRefreshToken.email })
32
+ if (!isUserExist) {
33
+ throw new AppError(httpStatus.NOT_FOUND, "User not found")
34
+ }
35
+ if (isUserExist.isActive === IsActive.BLOCKED || isUserExist.isActive === IsActive.INACTIVE) {
36
+ throw new AppError(httpStatus.FORBIDDEN, `Your account has been ${isUserExist.isActive.toLowerCase()}. Please contact support for assistance`)
37
+ }
38
+ if (isUserExist.isDeleted) {
39
+ throw new AppError(httpStatus.BAD_REQUEST, "User is deleted")
40
+ }
41
+
42
+ const jwtPayload = {
43
+ userId: isUserExist._id,
44
+ email: isUserExist.email,
45
+ role: isUserExist.role,
46
+ }
47
+
48
+
49
+ const accessToken = generateToken(jwtPayload, envVars.JWT_ACCESS_SECRET, envVars.JWT_ACCESS_EXPIRES)
50
+
51
+ return accessToken
52
+ }
@@ -0,0 +1,44 @@
1
+ import express, { type Request, type Response } from "express";
2
+ import cors from "cors";
3
+ import cookieParser from "cookie-parser";
4
+ import passport from "passport";
5
+ import "./app/config/passport";
6
+ import { router } from "./app/routes";
7
+ import expressSession from "express-session"
8
+
9
+ import { globalErrorHandler } from "./app/middlewares/globalErrorHandler";
10
+ import notFound from "./app/middlewares/notFound";
11
+ import { envVars } from "./app/config/env";
12
+
13
+ const app = express();
14
+
15
+ app.use(expressSession({
16
+ secret: envVars.EXPRESS_SESSION_SECRET,
17
+ resave: false,
18
+ saveUninitialized: false,
19
+ }))
20
+ app.use(express.json())
21
+ app.use(express.urlencoded({ extended: true }))
22
+ app.set("trust proxy", 1)
23
+ app.use(cors({
24
+ origin: envVars.FRONTEND_URL,
25
+ credentials: true
26
+ }))
27
+
28
+ app.use(cookieParser())
29
+ app.use(passport.initialize())
30
+ app.use(passport.session())
31
+
32
+ app.use("/api/v1", router);
33
+
34
+ app.get("/", (req: Request, res: Response) => {
35
+ res.status(200).json({
36
+ message: "Server is running"
37
+ })
38
+ })
39
+
40
+ app.use(globalErrorHandler)
41
+
42
+ app.use(notFound)
43
+
44
+ export default app;
@@ -0,0 +1,66 @@
1
+ import { Server } from "http";
2
+ import mongoose from "mongoose";
3
+ import app from "./app";
4
+ import { envVars } from "./app/config/env";
5
+ import { connectRedis } from './app/config/redis.config';
6
+ let server: Server;
7
+
8
+ const startServer = async () => {
9
+ try {
10
+ await mongoose.connect(envVars.DB_URL);
11
+
12
+ console.log("MongoDB connected successfully");
13
+
14
+ await connectRedis();
15
+
16
+ console.log("Redis connected successfully")
17
+
18
+ server = app.listen(envVars.PORT, () => {
19
+ console.log(`Server is running on port ${envVars.PORT}`);
20
+ })
21
+ } catch (error) {
22
+ console.error("Error connecting to MongoDB:", error);
23
+ }
24
+ }
25
+
26
+
27
+ startServer();
28
+
29
+ process.on("SIGTERM", () => {
30
+ console.log("SIGTERM signal received. Server Shutting Down...");
31
+ if (server) {
32
+ server.close(() => {
33
+ process.exit(1)
34
+ })
35
+ }
36
+ })
37
+ process.on("SIGINT", () => {
38
+ console.log("SIGINT signal received. Server Shutting Down...");
39
+ if (server) {
40
+ server.close(() => {
41
+ process.exit(1)
42
+ })
43
+ }
44
+ })
45
+
46
+ process.on("unhandledRejection", (err) => {
47
+ console.log("Unhandled Rejection Detected. Server Shutting Down...", err);
48
+
49
+ if (server) {
50
+ server.close(() => {
51
+ process.exit(1);
52
+ });
53
+ }
54
+ process.exit(1);
55
+ })
56
+
57
+ process.on("uncaughtException", (err) => {
58
+ console.log("Uncaught Exception Detected. Server Shutting Down...", err);
59
+ if (server) {
60
+ server.close(() => {
61
+ process.exit(1);
62
+ })
63
+ }
64
+ process.exit(1);
65
+ })
66
+
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "esModuleInterop": true,
7
+ "strict": true,
8
+ "skipLibCheck": true,
9
+ "outDir": "./dist",
10
+ "rootDir": "./src"
11
+ },
12
+ "include": ["src/**/*"],
13
+ "exclude": ["node_modules", "dist"]
14
+ }