feat(user stuff): added in all the user stuff
This commit is contained in:
42
app/src/internal/admin/controller/users/newUser.ts
Normal file
42
app/src/internal/admin/controller/users/newUser.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import type { User } from "better-auth";
|
||||
import { DrizzleQueryError } from "drizzle-orm";
|
||||
import { auth } from "../../../../pkg/auth/auth.js";
|
||||
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
|
||||
export type NewUser = {
|
||||
email: string;
|
||||
password: string;
|
||||
username: string;
|
||||
statusCode: number;
|
||||
message: string;
|
||||
};
|
||||
|
||||
export const createNewUser = async (userData: NewUser) => {
|
||||
const { data, error } = await tryCatch(
|
||||
auth.api.createUser({
|
||||
body: {
|
||||
email: userData.email, // required
|
||||
password: userData.password, // required
|
||||
name: userData.username, // required
|
||||
role: "user",
|
||||
data: { username: userData.username },
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
if (error instanceof DrizzleQueryError) {
|
||||
// @ts-ignore
|
||||
if (error?.cause.message.includes("unique constraint")) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
message: `${userData.username} already exists`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
@@ -1,11 +1,9 @@
|
||||
import type { Express, Request, Response } from "express";
|
||||
import { requireAuth } from "../../pkg/middleware/authMiddleware.js";
|
||||
import { mainServerSync } from "./controller/servers/matchServers.js";
|
||||
//admin routes
|
||||
import users from "./routes/getUserRoles.js";
|
||||
import grantRoles from "./routes/grantRole.js";
|
||||
import revokeRoles from "./routes/revokeRole.js";
|
||||
import servers from "./routes/servers/serverRoutes.js";
|
||||
//admin routes
|
||||
import users from "./routes/users/userRoutes.js";
|
||||
|
||||
export const setupAdminRoutes = (app: Express, basePath: string) => {
|
||||
app.use(
|
||||
@@ -15,22 +13,10 @@ export const setupAdminRoutes = (app: Express, basePath: string) => {
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin/users",
|
||||
requireAuth("user", ["systemAdmin"]), // will pass bc system admin but this is just telling us we need this
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
users,
|
||||
);
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
grantRoles,
|
||||
);
|
||||
|
||||
app.use(
|
||||
basePath + "/api/admin",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
revokeRoles,
|
||||
);
|
||||
|
||||
// run the sync only on startup
|
||||
setTimeout(() => {
|
||||
mainServerSync();
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
import { Router } from "express";
|
||||
import type { Request, Response } from "express";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { user } from "../../../pkg/db/schema/auth-schema.js";
|
||||
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
// should get all users
|
||||
const { data: users, error: userError } = await tryCatch(
|
||||
db.select().from(user)
|
||||
);
|
||||
|
||||
if (userError) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to get users",
|
||||
error: userError,
|
||||
});
|
||||
}
|
||||
// should get all roles
|
||||
|
||||
const { data: userRole, error: userRoleError } = await tryCatch(
|
||||
db.select().from(userRoles)
|
||||
);
|
||||
|
||||
if (userRoleError) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to get userRoless",
|
||||
error: userRoleError,
|
||||
});
|
||||
}
|
||||
|
||||
// add the roles and return
|
||||
|
||||
const usersWithRoles = users.map((user) => {
|
||||
const roles = userRole
|
||||
.filter((ur) => ur.userId === user.id)
|
||||
.map((ur) => ({ module: ur.module, role: ur.role }));
|
||||
|
||||
return { ...user, roles };
|
||||
});
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({ success: true, message: "User data", data: usersWithRoles });
|
||||
});
|
||||
|
||||
export default router;
|
||||
25
app/src/internal/admin/routes/users/changeUserPassword.ts
Normal file
25
app/src/internal/admin/routes/users/changeUserPassword.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { type Request, type Response, Router } from "express";
|
||||
import { auth } from "../../../../pkg/auth/auth.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.patch("/:userId", async (req: Request, res: Response) => {
|
||||
const userId = req.params.userId;
|
||||
const cookieHeader = req.headers.cookie ?? "";
|
||||
const authorization = req.headers.authorization ?? "";
|
||||
|
||||
const data = await auth.api.setUserPassword({
|
||||
body: {
|
||||
newPassword: req.body.password, // required
|
||||
userId: userId, // required
|
||||
},
|
||||
// This endpoint requires session cookies.
|
||||
headers: {
|
||||
cookie: cookieHeader,
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Password was just changed." });
|
||||
});
|
||||
export default router;
|
||||
20
app/src/internal/admin/routes/users/createUser.ts
Normal file
20
app/src/internal/admin/routes/users/createUser.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { type Request, type Response, Router } from "express";
|
||||
import { createNewUser, type NewUser } from "../../controller/users/newUser.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
const body = req.body;
|
||||
const user = (await createNewUser(body)) as NewUser;
|
||||
|
||||
if (user?.statusCode === 400) {
|
||||
return res.status(user?.statusCode).json({
|
||||
message: user?.message,
|
||||
});
|
||||
}
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({ message: `${body.username}, was just created` });
|
||||
});
|
||||
export default router;
|
||||
24
app/src/internal/admin/routes/users/deleteUser.ts
Normal file
24
app/src/internal/admin/routes/users/deleteUser.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { type Request, type Response, Router } from "express";
|
||||
import { auth } from "../../../../pkg/auth/auth.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.delete("/:userId", async (req: Request, res: Response) => {
|
||||
const userId = req.params.userId;
|
||||
const cookieHeader = req.headers.cookie ?? "";
|
||||
const authorization = req.headers.authorization ?? "";
|
||||
|
||||
const data = await auth.api.removeUser({
|
||||
body: {
|
||||
userId: userId, // required
|
||||
},
|
||||
// This endpoint requires session cookies.
|
||||
headers: {
|
||||
cookie: cookieHeader,
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "User was just deleted." });
|
||||
});
|
||||
export default router;
|
||||
25
app/src/internal/admin/routes/users/getActiveSessions.ts
Normal file
25
app/src/internal/admin/routes/users/getActiveSessions.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { type Request, type Response, Router } from "express";
|
||||
import { auth } from "../../../../pkg/auth/auth.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.patch("/:userId", async (req: Request, res: Response) => {
|
||||
const userId = req.params.userId;
|
||||
const cookieHeader = req.headers.cookie ?? "";
|
||||
const authorization = req.headers.authorization ?? "";
|
||||
|
||||
const data = await auth.api.setUserPassword({
|
||||
body: {
|
||||
newPassword: req.body.password, // required
|
||||
userId: userId, // required
|
||||
},
|
||||
// This endpoint requires session cookies.
|
||||
headers: {
|
||||
cookie: cookieHeader,
|
||||
authorization,
|
||||
},
|
||||
});
|
||||
|
||||
return res.status(200).json({ message: "Password was just changed." });
|
||||
});
|
||||
export default router;
|
||||
52
app/src/internal/admin/routes/users/getUserRoles.ts
Normal file
52
app/src/internal/admin/routes/users/getUserRoles.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
import { user } from "../../../../pkg/db/schema/auth-schema.js";
|
||||
import { userRoles } from "../../../../pkg/db/schema/user_roles.js";
|
||||
import { tryCatch } from "../../../../pkg/utils/tryCatch.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/", async (req: Request, res: Response) => {
|
||||
// should get all users
|
||||
const { data: users, error: userError } = await tryCatch(
|
||||
db.select().from(user),
|
||||
);
|
||||
|
||||
if (userError) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to get users",
|
||||
error: userError,
|
||||
});
|
||||
}
|
||||
// should get all roles
|
||||
|
||||
const { data: userRole, error: userRoleError } = await tryCatch(
|
||||
db.select().from(userRoles),
|
||||
);
|
||||
|
||||
if (userRoleError) {
|
||||
return res.status(500).json({
|
||||
success: false,
|
||||
message: "Failed to get userRoless",
|
||||
error: userRoleError,
|
||||
});
|
||||
}
|
||||
|
||||
// add the roles and return
|
||||
|
||||
const usersWithRoles = users.map((user) => {
|
||||
const roles = userRole
|
||||
.filter((ur) => ur.userId === user.id)
|
||||
.map((ur) => ({ module: ur.module, role: ur.role }));
|
||||
|
||||
return { ...user, roles };
|
||||
});
|
||||
|
||||
return res
|
||||
.status(200)
|
||||
.json({ success: true, message: "User data", data: usersWithRoles });
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,10 +1,9 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import z from "zod";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../pkg/logger/logger.js";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||
|
||||
const roleSchema = z.object({
|
||||
module: z.enum([
|
||||
@@ -2,10 +2,9 @@ import { and, eq } from "drizzle-orm";
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import z from "zod";
|
||||
import { db } from "../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../pkg/logger/logger.js";
|
||||
import { tryCatch } from "../../../pkg/utils/tryCatch.js";
|
||||
import { db } from "../../../../pkg/db/db.js";
|
||||
import { userRoles } from "../../../../pkg/db/schema/user_roles.js";
|
||||
import { createLogger } from "../../../../pkg/logger/logger.js";
|
||||
|
||||
const roleSchema = z.object({
|
||||
module: z.enum([
|
||||
51
app/src/internal/admin/routes/users/userRoutes.ts
Normal file
51
app/src/internal/admin/routes/users/userRoutes.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { fromNodeHeaders } from "better-auth/node";
|
||||
import type { Request, Response } from "express";
|
||||
import { Router } from "express";
|
||||
import { auth } from "../../../../pkg/auth/auth.js";
|
||||
import { requireAuth } from "../../../../pkg/middleware/authMiddleware.js";
|
||||
import changePassword from "./changeUserPassword.js";
|
||||
import createUser from "./createUser.js";
|
||||
import deleteUser from "./deleteUser.js";
|
||||
import users from "./getUserRoles.js";
|
||||
import grantRoles from "./grantRole.js";
|
||||
import revokeRoles from "./revokeRole.js";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.use(
|
||||
"/",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
grantRoles,
|
||||
);
|
||||
|
||||
router.use(
|
||||
"/new",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
createUser,
|
||||
);
|
||||
|
||||
router.use(
|
||||
"/",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
revokeRoles,
|
||||
);
|
||||
|
||||
router.use(
|
||||
"/",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
users,
|
||||
);
|
||||
|
||||
router.use(
|
||||
"/changePassword",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
changePassword,
|
||||
);
|
||||
|
||||
router.use(
|
||||
"/delete",
|
||||
requireAuth("user", ["systemAdmin", "admin"]), // will pass bc system admin but this is just telling us we need this
|
||||
deleteUser,
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -1,89 +1,93 @@
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { db } from "../db/db.js";
|
||||
import { username, admin, apiKey, jwt } from "better-auth/plugins";
|
||||
import { betterAuth } from "better-auth";
|
||||
import * as rawSchema from "../db/schema/auth-schema.js";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { admin, apiKey, jwt, username } from "better-auth/plugins";
|
||||
import type { User } from "better-auth/types";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/db.js";
|
||||
import * as rawSchema from "../db/schema/auth-schema.js";
|
||||
import { sendEmail } from "../utils/mail/sendMail.js";
|
||||
|
||||
export const schema = {
|
||||
user: rawSchema.user,
|
||||
session: rawSchema.session,
|
||||
account: rawSchema.account,
|
||||
verification: rawSchema.verification,
|
||||
jwks: rawSchema.jwks,
|
||||
apiKey: rawSchema.apikey, // 🔑 rename to apiKey
|
||||
user: rawSchema.user,
|
||||
session: rawSchema.session,
|
||||
account: rawSchema.account,
|
||||
verification: rawSchema.verification,
|
||||
jwks: rawSchema.jwks,
|
||||
apiKey: rawSchema.apikey, // 🔑 rename to apiKey
|
||||
};
|
||||
const RESET_EXPIRY_SECONDS = 3600; // 1 hour
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg",
|
||||
schema,
|
||||
}),
|
||||
trustedOrigins: [
|
||||
"*.alpla.net",
|
||||
"http://localhost:5173",
|
||||
"http://localhost:5500",
|
||||
"http://localhost:4200",
|
||||
"http://localhost:4000",
|
||||
],
|
||||
appName: "lst",
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
minPasswordLength: 8, // optional config
|
||||
resetPasswordTokenExpirySeconds: RESET_EXPIRY_SECONDS, // time in seconds
|
||||
sendResetPassword: async ({ user, token }) => {
|
||||
const frontendUrl = `${process.env.BETTER_AUTH_URL}/lst/app/user/resetpassword?token=${token}`;
|
||||
const expiryMinutes = Math.floor(RESET_EXPIRY_SECONDS / 60);
|
||||
const expiryText =
|
||||
expiryMinutes >= 60
|
||||
? `${expiryMinutes / 60} hour${
|
||||
expiryMinutes === 60 ? "" : "s"
|
||||
}`
|
||||
: `${expiryMinutes} minutes`;
|
||||
const emailData = {
|
||||
email: user.email,
|
||||
subject: "LST- Forgot password request",
|
||||
template: "forgotPassword",
|
||||
context: {
|
||||
username: user.name,
|
||||
email: user.email,
|
||||
url: frontendUrl,
|
||||
expiry: expiryText,
|
||||
},
|
||||
};
|
||||
await sendEmail(emailData);
|
||||
},
|
||||
// onPasswordReset: async ({ user }, request) => {
|
||||
// // your logic here
|
||||
// console.log(`Password for user ${user.email} has been reset.`);
|
||||
// },
|
||||
},
|
||||
plugins: [
|
||||
//jwt({ jwt: { expirationTime: "1h" } }),
|
||||
apiKey(),
|
||||
admin(),
|
||||
username(),
|
||||
],
|
||||
session: {
|
||||
expiresIn: 60 * 60,
|
||||
updateAge: 60 * 5,
|
||||
freshAge: 60 * 2,
|
||||
cookieCache: {
|
||||
enabled: true,
|
||||
maxAge: 5 * 60, // Cache duration in seconds
|
||||
},
|
||||
},
|
||||
events: {
|
||||
async onSignInSuccess({ user }: { user: User }) {
|
||||
await db
|
||||
.update(schema.user)
|
||||
.set({ lastLogin: new Date() })
|
||||
.where(eq(schema.user.id, user.id));
|
||||
},
|
||||
},
|
||||
database: drizzleAdapter(db, {
|
||||
provider: "pg",
|
||||
schema,
|
||||
}),
|
||||
trustedOrigins: [
|
||||
"*.alpla.net",
|
||||
"http://localhost:5173",
|
||||
"http://localhost:5500",
|
||||
"http://localhost:4200",
|
||||
"http://localhost:4000",
|
||||
],
|
||||
appName: "lst",
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
minPasswordLength: 8, // optional config
|
||||
resetPasswordTokenExpirySeconds: RESET_EXPIRY_SECONDS, // time in seconds
|
||||
sendResetPassword: async ({ user, token }) => {
|
||||
const frontendUrl = `${process.env.BETTER_AUTH_URL}/lst/app/user/resetpassword?token=${token}`;
|
||||
const expiryMinutes = Math.floor(RESET_EXPIRY_SECONDS / 60);
|
||||
const expiryText =
|
||||
expiryMinutes >= 60
|
||||
? `${expiryMinutes / 60} hour${expiryMinutes === 60 ? "" : "s"}`
|
||||
: `${expiryMinutes} minutes`;
|
||||
const emailData = {
|
||||
email: user.email,
|
||||
subject: "LST- Forgot password request",
|
||||
template: "forgotPassword",
|
||||
context: {
|
||||
username: user.name,
|
||||
email: user.email,
|
||||
url: frontendUrl,
|
||||
expiry: expiryText,
|
||||
},
|
||||
};
|
||||
await sendEmail(emailData);
|
||||
},
|
||||
// onPasswordReset: async ({ user }, request) => {
|
||||
// // your logic here
|
||||
// console.log(`Password for user ${user.email} has been reset.`);
|
||||
// },
|
||||
},
|
||||
plugins: [
|
||||
//jwt({ jwt: { expirationTime: "1h" } }),
|
||||
apiKey(),
|
||||
admin(),
|
||||
username(),
|
||||
],
|
||||
session: {
|
||||
expiresIn: 60 * 60,
|
||||
updateAge: 60 * 5,
|
||||
freshAge: 60 * 2,
|
||||
cookieCache: {
|
||||
enabled: true,
|
||||
maxAge: 5 * 60,
|
||||
},
|
||||
},
|
||||
cookie: {
|
||||
path: "/lst/app",
|
||||
sameSite: "lax",
|
||||
secure: false,
|
||||
httpOnly: true,
|
||||
},
|
||||
events: {
|
||||
async onSignInSuccess({ user }: { user: User }) {
|
||||
await db
|
||||
.update(schema.user)
|
||||
.set({ lastLogin: new Date() })
|
||||
.where(eq(schema.user.id, user.id));
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export type Auth = typeof auth;
|
||||
|
||||
Reference in New Issue
Block a user