feat: 增加登录鉴权和固定角色权限
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { env } from "../../config/env";
|
||||
import { ok } from "../../shared/response";
|
||||
import { authGuard } from "./auth.guard";
|
||||
import { loginBodySchema } from "./auth.schema";
|
||||
import { authService } from "./auth.service";
|
||||
|
||||
export async function authRoutes(app: FastifyInstance): Promise<void> {
|
||||
app.post("/auth/login", async (request) => {
|
||||
const body = loginBodySchema.parse(request.body);
|
||||
const { user, payload } = await authService.login(body);
|
||||
const token = app.jwt.sign(payload);
|
||||
|
||||
return ok({
|
||||
token,
|
||||
tokenType: "Bearer",
|
||||
expiresIn: env.JWT_EXPIRES_IN,
|
||||
user,
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/auth/me", { preHandler: authGuard }, async (request) => {
|
||||
const user = await authService.getCurrentUser(request.user);
|
||||
|
||||
return ok(user);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user