create-oikko-mongo 1.1.0 → 1.1.3
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/bin/index.js +20 -2
- package/package.json +9 -1
- package/template/package.json +0 -1
- package/template/src/app/config/passport.ts +2 -2
- package/template/src/app/modules/auth/auth.controller.ts +2 -1
- package/template/src/app/modules/user/user.interface.ts +3 -0
- package/template/src/app/modules/user/user.model.ts +2 -2
- package/template/src/app/modules/user/user.route.ts +2 -2
package/bin/index.js
CHANGED
|
@@ -359,13 +359,31 @@ async function main() {
|
|
|
359
359
|
// 4. Dynamically Inject Roles into user.interface.ts
|
|
360
360
|
const userInterfacePath = path.join(targetDir, "src/app/modules/user/user.interface.ts");
|
|
361
361
|
if (await fs.pathExists(userInterfacePath)) {
|
|
362
|
+
const effectiveRoles = roles.length > 0 ? roles : defaultRoles;
|
|
363
|
+
const defaultRoleKey = effectiveRoles[0].key;
|
|
364
|
+
const matchedAdminRoles = effectiveRoles.filter((r) => r.key.includes("ADMIN"));
|
|
365
|
+
const adminRoleKeys =
|
|
366
|
+
matchedAdminRoles.length > 0
|
|
367
|
+
? matchedAdminRoles.map((r) => `Role.${r.key}`)
|
|
368
|
+
: [`Role.${effectiveRoles[effectiveRoles.length - 1].key}`];
|
|
369
|
+
|
|
362
370
|
let content = await fs.readFile(userInterfacePath, "utf-8");
|
|
363
371
|
|
|
364
|
-
const enumCode = `export enum Role {\n${
|
|
372
|
+
const enumCode = `export enum Role {\n${effectiveRoles
|
|
365
373
|
.map((r) => ` ${r.key} = "${r.val}"`)
|
|
366
374
|
.join(",\n")}\n}`;
|
|
367
375
|
|
|
368
|
-
content = content
|
|
376
|
+
content = content
|
|
377
|
+
.replace(/export enum Role\s*\{[\s\S]*?\}/, enumCode)
|
|
378
|
+
.replace(
|
|
379
|
+
/export const DEFAULT_ROLE:\s*Role\s*=\s*[^;]+;/,
|
|
380
|
+
`export const DEFAULT_ROLE: Role = Role.${defaultRoleKey};`
|
|
381
|
+
)
|
|
382
|
+
.replace(
|
|
383
|
+
/export const ADMIN_ROLES:\s*Role\[\]\s*=\s*\[[^\]]*\];/,
|
|
384
|
+
`export const ADMIN_ROLES: Role[] = [${adminRoleKeys.join(", ")}];`
|
|
385
|
+
);
|
|
386
|
+
|
|
369
387
|
await fs.writeFile(userInterfacePath, content, "utf-8");
|
|
370
388
|
}
|
|
371
389
|
|
package/package.json
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-oikko-mongo",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Production-ready MERN (MongoDB + Express + TypeScript) enterprise backend scaffolder by Oikko",
|
|
5
5
|
"main": "./bin/index.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"create-oikko-mongo": "bin/index.js"
|
|
8
8
|
},
|
|
9
9
|
"type": "module",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/hassan-nahid/create-oikko-mongo.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/hassan-nahid/create-oikko-mongo/issues"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/hassan-nahid/create-oikko-mongo#readme",
|
|
10
18
|
"keywords": [
|
|
11
19
|
"oikko",
|
|
12
20
|
"mern",
|
package/template/package.json
CHANGED
|
@@ -5,7 +5,7 @@ import { envVars } from "./env";
|
|
|
5
5
|
import { Strategy as LocalStrategy } from "passport-local";
|
|
6
6
|
import bcryptjs from "bcryptjs"
|
|
7
7
|
import { User } from "../modules/user/user.model";
|
|
8
|
-
import {
|
|
8
|
+
import { DEFAULT_ROLE, IsActive } from "../modules/user/user.interface";
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
@@ -81,7 +81,7 @@ passport.use(
|
|
|
81
81
|
isUserExist = await User.create({
|
|
82
82
|
email,
|
|
83
83
|
name: profile.displayName,
|
|
84
|
-
role:
|
|
84
|
+
role: DEFAULT_ROLE,
|
|
85
85
|
isVerified: true,
|
|
86
86
|
auths: [
|
|
87
87
|
{
|
|
@@ -11,6 +11,7 @@ import { setAuthCookie } from "../../utils/setCookie"
|
|
|
11
11
|
import { createUserTokens } from "../../utils/userTokens"
|
|
12
12
|
import { AuthServices } from "./auth.service"
|
|
13
13
|
import { catchAsync } from "../../utils/catchAsync"
|
|
14
|
+
import { ADMIN_ROLES } from "../user/user.interface"
|
|
14
15
|
|
|
15
16
|
const credentialsLogin = catchAsync(async (req: Request, res: Response, next: NextFunction) => {
|
|
16
17
|
// const loginInfo = await AuthServices.credentialsLogin(req.body)
|
|
@@ -151,7 +152,7 @@ const googleCallbackController = catchAsync(async (req: Request, res: Response,
|
|
|
151
152
|
// If no redirect specified, use default based on role
|
|
152
153
|
if (!redirectTo) {
|
|
153
154
|
const user = req.user as any;
|
|
154
|
-
redirectTo = user?.role
|
|
155
|
+
redirectTo = ADMIN_ROLES.includes(user?.role) ? 'admin/dashboard' : 'dashboard'
|
|
155
156
|
}
|
|
156
157
|
|
|
157
158
|
// Remove leading slash if present
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { model, Schema } from "mongoose";
|
|
2
|
-
import { IAuthProvider, IGender, IsActive, IUser, Role } from "./user.interface";
|
|
2
|
+
import { DEFAULT_ROLE, IAuthProvider, IGender, IsActive, IUser, Role } from "./user.interface";
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
const authProviderSchema = new Schema<IAuthProvider>({
|
|
@@ -28,7 +28,7 @@ const userSchema = new Schema<IUser>({
|
|
|
28
28
|
role: {
|
|
29
29
|
type: String,
|
|
30
30
|
enum: Object.values(Role),
|
|
31
|
-
default:
|
|
31
|
+
default: DEFAULT_ROLE
|
|
32
32
|
},
|
|
33
33
|
gender: {
|
|
34
34
|
type: String,
|
|
@@ -2,7 +2,7 @@ import { Router } from "express";
|
|
|
2
2
|
import { UserController } from "./user.controller";
|
|
3
3
|
import { createUserZodSchema } from "./user.validation";
|
|
4
4
|
import { validateRequest } from "../../middlewares/validateRequest";
|
|
5
|
-
import { Role } from "./user.interface";
|
|
5
|
+
import { ADMIN_ROLES, Role } from "./user.interface";
|
|
6
6
|
import { checkAuth } from "../../middlewares/checkAuth";
|
|
7
7
|
|
|
8
8
|
const router = Router();
|
|
@@ -11,7 +11,7 @@ const router = Router();
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
router.post("/register", validateRequest(createUserZodSchema), UserController.createUser);
|
|
14
|
-
router.get("/all-users", checkAuth(
|
|
14
|
+
router.get("/all-users", checkAuth(...ADMIN_ROLES), UserController.getAllUsers);
|
|
15
15
|
// Get current user profile
|
|
16
16
|
router.get("/me",
|
|
17
17
|
checkAuth(...Object.values(Role)),
|