Skip to content

Commit

Permalink
invalidate session via id route
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 5a1b050 commit ef1924b
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/v2/lib/managers/auth/user-session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,16 @@ export class AuthSessionManager {

return true
}

public async invalidateSessionById(id: string) {
const { user } = await this.validateAndGetSession()

if (!user) {
return null
}

await this.lucia.invalidateSession(id)

return true
}
}
2 changes: 1 addition & 1 deletion src/v2/routes/asset/delete-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const deleteAssetByIdSchema = z.object({
name: "id",
in: "path",
description: "The ID of the asset to delete.",
example: "1",
example: "asset_id",
required: true,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/like-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const likeAssetByIdSchema = z.object({
id: z.string().openapi({
param: {
description: "The id of the asset to like.",
example: "1",
example: "asset_id",
in: "path",
name: "id",
required: true,
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/modify-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const modifyAssetPathSchema = z.object({
id: z.string().openapi({
param: {
description: "The id of the asset to modify.",
example: "1",
example: "asset_id",
in: "path",
required: true,
},
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/asset/unlike-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const unlikeAssetByIdSchema = z.object({
id: z.string().openapi({
param: {
description: "The id of the asset to unlike.",
example: "1",
example: "asset_id",
in: "path",
name: "id",
required: true,
Expand Down
3 changes: 2 additions & 1 deletion src/v2/routes/asset/upload-asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const uploadAssetSchema = z.object({
.min(1)
.max(1)
.openapi({
description: "If the asset contains suggestive content 0 or 1.",
description:
"If the asset contains suggestive content. 1 = Yes, 0 = No.",
example: "1",
})
.transform((value) => parseInt(value))
Expand Down
2 changes: 2 additions & 0 deletions src/v2/routes/auth/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { UserLoginRoute } from "./account-login"
import { UserAllCurrentSessionsRoute } from "./get-all-sessions"
import { LogoutCurrentSessionRoute } from "./logout-current-session"
import { ValidateSessionRoute } from "./validate-current-session"
import { InvalidateSessionRoute } from "./invalidate-session"

const handler = new OpenAPIHono<{ Bindings: Bindings; Variables: Variables }>()

UserCreateAccountRoute(handler)
UserLoginRoute(handler)

ValidateSessionRoute(handler)
InvalidateSessionRoute(handler)
UserAllCurrentSessionsRoute(handler)
LogoutCurrentSessionRoute(handler)

Expand Down
94 changes: 94 additions & 0 deletions src/v2/routes/auth/invalidate-session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { AppHandler } from "../handler"
import { AuthSessionManager } from "@/v2/lib/managers/auth/user-session-manager"
// import { deleteCookie } from "hono/cookie"
import { createRoute } from "@hono/zod-openapi"
import { GenericResponses } from "@/v2/lib/response-schemas"
import { z } from "@hono/zod-openapi"

const invalidateSessionSchema = z.object({
id: z.string().openapi({
param: {
description: "The id of the session to invalidate.",
example: "session_id",
in: "path",
name: "id",
required: true,
},
}),
})

const invalidateSessionResponseSchema = z.object({
success: z.literal(true),
})

const invalidateSessionRoute = createRoute({
path: "/invalidate/{id}",
method: "get",
description: "Invalidate a session by its ID.",
tags: ["Auth"],
request: {
params: invalidateSessionSchema,
},
responses: {
200: {
description: "Logout successful.",
content: {
"application/json": {
schema: invalidateSessionResponseSchema,
},
},
},
...GenericResponses,
},
})

export const InvalidateSessionRoute = (handler: AppHandler) => {
handler.openapi(invalidateSessionRoute, async (ctx) => {
const sessionId = ctx.req.valid("param").id

const authSessionManager = new AuthSessionManager(ctx)

const { user, session } = await authSessionManager.validateSession()

if (!user) {
return ctx.json(
{
success: false,
message: "Unauthorized",
},
401
)
}

if (sessionId == session.id) {
return ctx.json(
{
success: false,
message: "Cannot invalidate the current session.",
},
400
)
}

const sessions = await authSessionManager.getAllSessions()

if (!sessions.find((s) => s.id === sessionId)) {
return ctx.json(
{
success: false,
message: "Session not found.",
},
400
)
}

await authSessionManager.invalidateSessionById(sessionId)

return ctx.json(
{
success: true,
},
200
)
})
}
2 changes: 1 addition & 1 deletion src/v2/routes/requests/delete-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const deleteRequestByIdSchema = z.object({
name: "id",
in: "path",
description: "The ID of the request to delete. Supporter required.",
example: "1",
example: "request_id",
required: true,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/requests/remove-request-upvote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const removeRequestUpvoteByIdSchema = z.object({
name: "id",
in: "path",
description: "The ID of the request to remove the upvote for.",
example: "1",
example: "request_id",
required: true,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/requests/upvote-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const upvoteRequestByIdSchema = z.object({
name: "id",
in: "path",
description: "The ID of the request to upvote.",
example: "1",
example: "request_id",
required: true,
},
}),
Expand Down
2 changes: 1 addition & 1 deletion src/v2/routes/requests/view-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const viewRequestByIdSchema = z.object({
name: "id",
in: "path",
description: "The ID of the request to view.",
example: "1",
example: "request_id",
required: true,
},
}),
Expand Down

0 comments on commit ef1924b

Please sign in to comment.