.env: add variable COOKIE_TOKEN_NAME
This commit is contained in:
parent
2932db30eb
commit
d63a6ed199
3
.env
3
.env
@ -21,4 +21,5 @@ LDAP_ATTRIBUTE=
|
|||||||
|
|
||||||
API_SECRET=
|
API_SECRET=
|
||||||
API_TOKEN_TTL=
|
API_TOKEN_TTL=
|
||||||
API_CACHE_TTL=
|
API_CACHE_TTL=
|
||||||
|
COOKIE_TOKEN_NAME=token
|
||||||
@ -2,7 +2,6 @@
|
|||||||
/* eslint-disable class-methods-use-this */
|
/* eslint-disable class-methods-use-this */
|
||||||
/* eslint-disable import/no-extraneous-dependencies */
|
/* eslint-disable import/no-extraneous-dependencies */
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { COOKIE_TOKEN_NAME } from './lib/constants';
|
|
||||||
import { Credentials } from './types/request';
|
import { Credentials } from './types/request';
|
||||||
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res } from '@nestjs/common';
|
import { Body, Controller, Get, HttpException, HttpStatus, Post, Req, Res } from '@nestjs/common';
|
||||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
@ -35,7 +34,7 @@ export class AuthController {
|
|||||||
try {
|
try {
|
||||||
const token = await this.authService.login(login, password);
|
const token = await this.authService.login(login, password);
|
||||||
|
|
||||||
return reply.setCookie(COOKIE_TOKEN_NAME, token, this.cookieOptions).status(200).send();
|
return reply.setCookie(env.COOKIE_TOKEN_NAME, token, this.cookieOptions).status(200).send();
|
||||||
} catch {
|
} catch {
|
||||||
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
|
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
|
||||||
}
|
}
|
||||||
@ -43,7 +42,7 @@ export class AuthController {
|
|||||||
|
|
||||||
@Get('/logout')
|
@Get('/logout')
|
||||||
async logout(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
async logout(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||||
const token = req.cookies[COOKIE_TOKEN_NAME];
|
const token = req.cookies[env.COOKIE_TOKEN_NAME];
|
||||||
if (token) await this.authService.logout(token);
|
if (token) await this.authService.logout(token);
|
||||||
|
|
||||||
this.clearCookies(req, reply);
|
this.clearCookies(req, reply);
|
||||||
@ -53,7 +52,7 @@ export class AuthController {
|
|||||||
|
|
||||||
@Get('/auth')
|
@Get('/auth')
|
||||||
async auth(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
async auth(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||||
const token = req.cookies[COOKIE_TOKEN_NAME];
|
const token = req.cookies[env.COOKIE_TOKEN_NAME];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.authService.checkToken(token);
|
this.authService.checkToken(token);
|
||||||
@ -63,7 +62,7 @@ export class AuthController {
|
|||||||
if (error.name === 'TokenExpiredError') {
|
if (error.name === 'TokenExpiredError') {
|
||||||
const newToken = this.authService.refreshToken(token);
|
const newToken = this.authService.refreshToken(token);
|
||||||
|
|
||||||
return reply.setCookie(COOKIE_TOKEN_NAME, newToken, this.cookieOptions).send();
|
return reply.setCookie(env.COOKIE_TOKEN_NAME, newToken, this.cookieOptions).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
return reply.status(HttpStatus.UNAUTHORIZED).send();
|
return reply.status(HttpStatus.UNAUTHORIZED).send();
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
export const COOKIE_TOKEN_NAME = 'token';
|
|
||||||
@ -1,8 +1,11 @@
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
const envSchema = z.object({
|
const envSchema = z.object({
|
||||||
API_PORT: z.number().optional().default(3001),
|
|
||||||
API_CACHE_TTL: z.string().transform((val) => Number.parseInt(val, 10)),
|
API_CACHE_TTL: z.string().transform((val) => Number.parseInt(val, 10)),
|
||||||
|
API_PORT: z.number().optional().default(3001),
|
||||||
|
API_SECRET: z.string(),
|
||||||
|
API_TOKEN_TTL: z.string().transform((val) => Number.parseInt(val, 10)),
|
||||||
|
COOKIE_TOKEN_NAME: z.string().default('token'),
|
||||||
LDAP_ATTRIBUTE: z.string(),
|
LDAP_ATTRIBUTE: z.string(),
|
||||||
LDAP_BASE: z.string(),
|
LDAP_BASE: z.string(),
|
||||||
LDAP_BIND_CREDENTIALS: z.string(),
|
LDAP_BIND_CREDENTIALS: z.string(),
|
||||||
@ -14,8 +17,6 @@ const envSchema = z.object({
|
|||||||
.string()
|
.string()
|
||||||
.transform((val) => Number.parseInt(val, 10))
|
.transform((val) => Number.parseInt(val, 10))
|
||||||
.default('6379'),
|
.default('6379'),
|
||||||
API_SECRET: z.string(),
|
|
||||||
API_TOKEN_TTL: z.string().transform((val) => Number.parseInt(val, 10)),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export default envSchema;
|
export default envSchema;
|
||||||
|
|||||||
@ -1,9 +1,10 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/explicit-member-accessibility */
|
||||||
/* eslint-disable class-methods-use-this */
|
/* eslint-disable class-methods-use-this */
|
||||||
/* eslint-disable import/no-extraneous-dependencies */
|
/* eslint-disable import/no-extraneous-dependencies */
|
||||||
import { COOKIE_TOKEN_NAME } from '../auth/lib/constants';
|
|
||||||
import { UsersService } from './users.service';
|
import { UsersService } from './users.service';
|
||||||
import { Controller, Get, Req, Res } from '@nestjs/common';
|
import { Controller, Get, Req, Res } from '@nestjs/common';
|
||||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||||
|
import { env } from 'src/config/env';
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
export class UsersController {
|
export class UsersController {
|
||||||
@ -11,7 +12,7 @@ export class UsersController {
|
|||||||
|
|
||||||
@Get('/get-user')
|
@Get('/get-user')
|
||||||
async getUser(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
async getUser(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||||
const token = req.cookies[COOKIE_TOKEN_NAME];
|
const token = req.cookies[env.COOKIE_TOKEN_NAME];
|
||||||
|
|
||||||
const user = await this.usersService.getUser(token);
|
const user = await this.usersService.getUser(token);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user