65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
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: [] }],
|
|
},
|
|
},
|
|
);
|