mirror of
https://github.com/docmost/docmost.git
synced 2025-11-23 19:11:08 +10:00
switch to nx monorepo
This commit is contained in:
18
apps/server/src/core/auth/services/auth.service.spec.ts
Normal file
18
apps/server/src/core/auth/services/auth.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
describe('AuthService', () => {
|
||||
let service: AuthService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [AuthService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<AuthService>(AuthService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
41
apps/server/src/core/auth/services/auth.service.ts
Normal file
41
apps/server/src/core/auth/services/auth.service.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
import { LoginDto } from '../dto/login.dto';
|
||||
import { User } from '../../user/entities/user.entity';
|
||||
import { CreateUserDto } from '../../user/dto/create-user.dto';
|
||||
import { UserService } from '../../user/user.service';
|
||||
import { TokenService } from './token.service';
|
||||
import { TokensDto } from '../dto/tokens.dto';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
constructor(
|
||||
private userService: UserService,
|
||||
private tokenService: TokenService,
|
||||
) {}
|
||||
|
||||
async login(loginDto: LoginDto) {
|
||||
const user: User = await this.userService.findByEmail(loginDto.email);
|
||||
const invalidCredentialsMessage = 'email or password does not match';
|
||||
|
||||
if (
|
||||
!user ||
|
||||
!(await this.userService.compareHash(loginDto.password, user.password))
|
||||
) {
|
||||
throw new UnauthorizedException(invalidCredentialsMessage);
|
||||
}
|
||||
|
||||
user.lastLoginAt = new Date();
|
||||
|
||||
const tokens: TokensDto = await this.tokenService.generateTokens(user);
|
||||
|
||||
return { tokens };
|
||||
}
|
||||
|
||||
async register(createUserDto: CreateUserDto) {
|
||||
const user: User = await this.userService.create(createUserDto);
|
||||
|
||||
const tokens: TokensDto = await this.tokenService.generateTokens(user);
|
||||
|
||||
return { tokens };
|
||||
}
|
||||
}
|
||||
18
apps/server/src/core/auth/services/token.service.spec.ts
Normal file
18
apps/server/src/core/auth/services/token.service.spec.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { TokenService } from './token.service';
|
||||
|
||||
describe('TokenService', () => {
|
||||
let service: TokenService;
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [TokenService],
|
||||
}).compile();
|
||||
|
||||
service = module.get<TokenService>(TokenService);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
});
|
||||
43
apps/server/src/core/auth/services/token.service.ts
Normal file
43
apps/server/src/core/auth/services/token.service.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { EnvironmentService } from '../../../environment/environment.service';
|
||||
import { User } from '../../user/entities/user.entity';
|
||||
import { FastifyRequest } from 'fastify';
|
||||
import { TokensDto } from '../dto/tokens.dto';
|
||||
|
||||
export type JwtPayload = { sub: string; email: string };
|
||||
|
||||
@Injectable()
|
||||
export class TokenService {
|
||||
constructor(
|
||||
private jwtService: JwtService,
|
||||
private environmentService: EnvironmentService,
|
||||
) {}
|
||||
async generateJwt(user: User): Promise<string> {
|
||||
const payload: JwtPayload = {
|
||||
sub: user.id,
|
||||
email: user.email,
|
||||
};
|
||||
return await this.jwtService.signAsync(payload);
|
||||
}
|
||||
|
||||
async generateTokens(user: User): Promise<TokensDto> {
|
||||
return {
|
||||
accessToken: await this.generateJwt(user),
|
||||
refreshToken: null,
|
||||
};
|
||||
}
|
||||
|
||||
async verifyJwt(token: string) {
|
||||
return await this.jwtService.verifyAsync(token, {
|
||||
secret: this.environmentService.getJwtSecret(),
|
||||
});
|
||||
}
|
||||
|
||||
async extractTokenFromHeader(
|
||||
request: FastifyRequest,
|
||||
): Promise<string | undefined> {
|
||||
const [type, token] = request.headers.authorization?.split(' ') ?? [];
|
||||
return type === 'Bearer' ? token : undefined;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user