Skip to content

Commit 89cb593

Browse files
committed
patch: correct handlers
1 parent 7d5199d commit 89cb593

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

apps/api/src/controllers/auth.ts

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AuthorizationCode } from "simple-oauth2";
99
import { getOAuthProvider, getOidcConfig } from "../lib/auth";
1010
import { track } from "../lib/hog";
1111
import { forgotPassword } from "../lib/nodemailer/auth/forgot-password";
12+
import { requirePermission } from "../lib/roles";
1213
import { checkSession } from "../lib/session";
1314
import { getOAuthClient } from "../lib/utils/oauth_client";
1415
import { getOidcClient } from "../lib/utils/oidc_client";
@@ -325,16 +326,16 @@ export function authRoutes(fastify: FastifyInstance) {
325326
var secret = Buffer.from(process.env.SECRET!, "base64");
326327
const token = jwt.sign(
327328
{
328-
data: {
329+
data: {
329330
id: user!.id,
330331
// Add a unique identifier for this session
331-
sessionId: crypto.randomBytes(32).toString('hex')
332-
}
332+
sessionId: crypto.randomBytes(32).toString("hex"),
333+
},
333334
},
334335
secret,
335-
{
336+
{
336337
expiresIn: "8h",
337-
algorithm: 'HS256'
338+
algorithm: "HS256",
338339
}
339340
);
340341

@@ -344,7 +345,7 @@ export function authRoutes(fastify: FastifyInstance) {
344345
userId: user!.id,
345346
sessionToken: token,
346347
expires: new Date(Date.now() + 8 * 60 * 60 * 1000), // 8 hours
347-
userAgent: request.headers['user-agent'] || '',
348+
userAgent: request.headers["user-agent"] || "",
348349
ipAddress: request.ip,
349350
},
350351
});
@@ -697,6 +698,9 @@ export function authRoutes(fastify: FastifyInstance) {
697698
// Delete a user
698699
fastify.delete(
699700
"/api/v1/auth/user/:id",
701+
{
702+
preHandler: requirePermission(["user::delete"]),
703+
},
700704
async (request: FastifyRequest, reply: FastifyReply) => {
701705
const { id } = request.params as { id: string };
702706

@@ -793,6 +797,9 @@ export function authRoutes(fastify: FastifyInstance) {
793797
// Reset password by admin
794798
fastify.post(
795799
"/api/v1/auth/admin/reset-password",
800+
{
801+
preHandler: requirePermission(["user::manage"]),
802+
},
796803
async (request: FastifyRequest, reply: FastifyReply) => {
797804
let { password, user } = request.body as {
798805
password: string;
@@ -834,6 +841,9 @@ export function authRoutes(fastify: FastifyInstance) {
834841
// Update a users profile/config
835842
fastify.put(
836843
"/api/v1/auth/profile",
844+
{
845+
preHandler: requirePermission(["user::update"]),
846+
},
837847
async (request: FastifyRequest, reply: FastifyReply) => {
838848
const session = await checkSession(request);
839849

@@ -861,6 +871,9 @@ export function authRoutes(fastify: FastifyInstance) {
861871
// Update a users Email notification settings
862872
fastify.put(
863873
"/api/v1/auth/profile/notifcations/emails",
874+
{
875+
preHandler: requirePermission(["user::update"]),
876+
},
864877
async (request: FastifyRequest, reply: FastifyReply) => {
865878
const session = await checkSession(request);
866879

@@ -904,12 +917,14 @@ export function authRoutes(fastify: FastifyInstance) {
904917
// Update a users role
905918
fastify.put(
906919
"/api/v1/auth/user/role",
920+
{
921+
preHandler: requirePermission(["user::manage"]),
922+
},
907923
async (request: FastifyRequest, reply: FastifyReply) => {
908924
const session = await checkSession(request);
909925

910926
if (session?.isAdmin) {
911927
const { id, role } = request.body as { id: string; role: boolean };
912-
// check for atleast one admin on role downgrade
913928
if (role === false) {
914929
const admins = await prisma.user.findMany({
915930
where: { isAdmin: true },
@@ -959,7 +974,8 @@ export function authRoutes(fastify: FastifyInstance) {
959974
);
960975

961976
// Add a new endpoint to list and manage active sessions
962-
fastify.get("/api/v1/auth/sessions",
977+
fastify.get(
978+
"/api/v1/auth/sessions",
963979
async (request: FastifyRequest, reply: FastifyReply) => {
964980
const currentUser = await checkSession(request);
965981
if (!currentUser) {
@@ -973,16 +989,17 @@ export function authRoutes(fastify: FastifyInstance) {
973989
userAgent: true,
974990
ipAddress: true,
975991
createdAt: true,
976-
expires: true
977-
}
992+
expires: true,
993+
},
978994
});
979995

980996
reply.send({ sessions });
981997
}
982998
);
983999

9841000
// Add ability to revoke specific sessions
985-
fastify.delete("/api/v1/auth/sessions/:sessionId",
1001+
fastify.delete(
1002+
"/api/v1/auth/sessions/:sessionId",
9861003
async (request: FastifyRequest, reply: FastifyReply) => {
9871004
const currentUser = await checkSession(request);
9881005
if (!currentUser) {
@@ -993,18 +1010,18 @@ export function authRoutes(fastify: FastifyInstance) {
9931010

9941011
// Only allow users to delete their own sessions
9951012
const session = await prisma.session.findFirst({
996-
where: {
1013+
where: {
9971014
id: sessionId,
998-
userId: currentUser.id
999-
}
1015+
userId: currentUser.id,
1016+
},
10001017
});
10011018

10021019
if (!session) {
10031020
return reply.code(404).send({ message: "Session not found" });
10041021
}
10051022

10061023
await prisma.session.delete({
1007-
where: { id: sessionId }
1024+
where: { id: sessionId },
10081025
});
10091026

10101027
reply.send({ success: true });

0 commit comments

Comments
 (0)