Files
imen_pishghaman/apps/api/dist/auth/auth.controller.js
T

133 lines
4.9 KiB
JavaScript

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
import { Body, Controller, Get, HttpCode, Module, Post, Req, Res } from "@nestjs/common";
import { JwtModule } from "@nestjs/jwt";
import { Throttle } from "@nestjs/throttler";
import { changePasswordSchema, loginSchema, } from "@imen/shared";
import { env, isProd } from "../config/env.js";
import { AdminAuth, CurrentUser } from "../common/auth.js";
import { ZodPipe } from "../common/zod.pipe.js";
import { AuthService } from "./auth.service.js";
const COOKIE = "imen_rt";
const COOKIE_PATH = "/api/admin/auth";
const ua = (req) => req.headers["user-agent"] ?? "";
const readCookie = (req) => req.cookies?.[COOKIE];
let AuthController = class AuthController {
auth;
constructor(auth) {
this.auth = auth;
}
async login(body, req, res) {
return this.respond(res, await this.auth.login(body, ua(req)));
}
async refresh(req, res) {
try {
return this.respond(res, await this.auth.refresh(readCookie(req), ua(req)));
}
catch (e) {
res.clearCookie(COOKIE, { path: COOKIE_PATH });
throw e;
}
}
async logout(req, res) {
await this.auth.logout(readCookie(req));
res.clearCookie(COOKIE, { path: COOKIE_PATH });
}
me(user) {
return this.auth.me(user.id);
}
async changePassword(body, user) {
await this.auth.changePassword(user.id, body);
}
respond(res, t) {
res.cookie(COOKIE, t.refreshToken, {
httpOnly: true,
secure: isProd && env.COOKIE_SECURE,
sameSite: "strict",
path: COOKIE_PATH,
expires: t.refreshExpiresAt,
});
return { accessToken: t.accessToken, user: t.user };
}
};
__decorate([
Post("login"),
HttpCode(200),
Throttle({ default: { limit: 8, ttl: 60_000 } }),
__param(0, Body(new ZodPipe(loginSchema))),
__param(1, Req()),
__param(2, Res({ passthrough: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "login", null);
__decorate([
Post("refresh"),
HttpCode(200),
Throttle({ default: { limit: 30, ttl: 60_000 } }),
__param(0, Req()),
__param(1, Res({ passthrough: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "refresh", null);
__decorate([
Post("logout"),
HttpCode(204),
__param(0, Req()),
__param(1, Res({ passthrough: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "logout", null);
__decorate([
Get("me"),
AdminAuth(),
__param(0, CurrentUser()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object]),
__metadata("design:returntype", void 0)
], AuthController.prototype, "me", null);
__decorate([
Post("change-password"),
HttpCode(204),
AdminAuth(),
__param(0, Body(new ZodPipe(changePasswordSchema))),
__param(1, CurrentUser()),
__metadata("design:type", Function),
__metadata("design:paramtypes", [Object, Object]),
__metadata("design:returntype", Promise)
], AuthController.prototype, "changePassword", null);
AuthController = __decorate([
Controller("admin/auth"),
__metadata("design:paramtypes", [AuthService])
], AuthController);
export { AuthController };
let AuthModule = class AuthModule {
};
AuthModule = __decorate([
Module({
imports: [
JwtModule.register({
global: true,
secret: env.JWT_SECRET,
signOptions: { expiresIn: env.JWT_ACCESS_TTL },
}),
],
controllers: [AuthController],
providers: [AuthService],
exports: [AuthService],
})
], AuthModule);
export { AuthModule };
//# sourceMappingURL=auth.controller.js.map