commit
This commit is contained in:
64
src/modules/auth/controller.ts
Normal file
64
src/modules/auth/controller.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Elysia, t } from "elysia";
|
||||
import { authPlugin } from "@/middleware/auth";
|
||||
import type { User } from "@/database/schema";
|
||||
import type { KeycloakTokenPayload } from "@/lib/keycloak";
|
||||
|
||||
export const auth = new Elysia({ prefix: "/auth", tags: ["auth"] })
|
||||
.use(authPlugin)
|
||||
// GET /api/auth/me - Get current user info
|
||||
.get(
|
||||
"/me",
|
||||
(context: any) => {
|
||||
const user = context.user as User;
|
||||
const tokenPayload = context.tokenPayload as KeycloakTokenPayload;
|
||||
|
||||
if (!user || !tokenPayload) {
|
||||
throw new Error("Unauthorized");
|
||||
}
|
||||
|
||||
return {
|
||||
success: true as const,
|
||||
data: {
|
||||
user: {
|
||||
id: user.id,
|
||||
keycloakId: user.keycloakId,
|
||||
email: user.email,
|
||||
name: user.name,
|
||||
createdAt: user.createdAt.toISOString(),
|
||||
},
|
||||
tokenInfo: {
|
||||
sub: tokenPayload.sub,
|
||||
email: tokenPayload.email,
|
||||
name: tokenPayload.name,
|
||||
exp: tokenPayload.exp,
|
||||
iat: tokenPayload.iat,
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
{
|
||||
response: t.Object({
|
||||
success: t.Literal(true),
|
||||
data: t.Object({
|
||||
user: t.Object({
|
||||
id: t.String(),
|
||||
keycloakId: t.String(),
|
||||
email: t.String(),
|
||||
name: t.String(),
|
||||
createdAt: t.String(),
|
||||
}),
|
||||
tokenInfo: t.Object({
|
||||
sub: t.String(),
|
||||
email: t.Optional(t.String()),
|
||||
name: t.Optional(t.String()),
|
||||
exp: t.Number(),
|
||||
iat: t.Number(),
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
detail: {
|
||||
description: "Get current authenticated user information",
|
||||
security: [{ Bearer: [] }],
|
||||
},
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user