feat: rewrite auth (#364)

* feat: wip rewrite auth

* feat: restructure folders and fix stories and tests

* feat: remove auth provider and fix tests
This commit is contained in:
Jérémy M
2023-06-23 17:49:50 +02:00
committed by GitHub
parent 1c7980b270
commit c6708b2c1f
54 changed files with 1268 additions and 584 deletions

View File

@ -1,30 +0,0 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PasswordAuthController } from './password-auth.controller';
import { AuthService } from '../services/auth.service';
import { TokenService } from '../services/token.service';
describe('PasswordAuthController', () => {
let controller: PasswordAuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [PasswordAuthController],
providers: [
{
provide: AuthService,
useValue: {},
},
{
provide: TokenService,
useValue: {},
},
],
}).compile();
controller = module.get<PasswordAuthController>(PasswordAuthController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@ -1,23 +0,0 @@
import { Body, Controller, Post } from '@nestjs/common';
import { ChallengeInput } from '../dto/challenge.input';
import { AuthService } from '../services/auth.service';
import { LoginTokenEntity } from '../dto/login-token.entity';
import { TokenService } from '../services/token.service';
@Controller('auth/password')
export class PasswordAuthController {
constructor(
private readonly authService: AuthService,
private readonly tokenService: TokenService,
) {}
@Post()
async challenge(
@Body() challengeInput: ChallengeInput,
): Promise<LoginTokenEntity> {
const user = await this.authService.challenge(challengeInput);
const loginToken = await this.tokenService.generateLoginToken(user.email);
return { loginToken };
}
}

View File

@ -1,21 +0,0 @@
import { BadRequestException, Body, Controller, Post } from '@nestjs/common';
import { RefreshTokenInput } from '../dto/refresh-token.input';
import { TokenService } from '../services/token.service';
@Controller('auth/token')
export class TokenController {
constructor(private tokenService: TokenService) {}
@Post()
async generateAccessToken(@Body() body: RefreshTokenInput) {
if (!body.refreshToken) {
throw new BadRequestException('Refresh token is mendatory');
}
const tokens = await this.tokenService.generateTokensFromRefreshToken(
body.refreshToken,
);
return { tokens: tokens };
}
}

View File

@ -1,14 +1,14 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthController } from './auth.controller';
import { VerifyAuthController } from './verify-auth.controller';
import { AuthService } from '../services/auth.service';
import { TokenService } from '../services/token.service';
describe('AuthController', () => {
let controller: AuthController;
describe('VerifyAuthController', () => {
let controller: VerifyAuthController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [AuthController],
controllers: [VerifyAuthController],
providers: [
{
provide: AuthService,
@ -21,7 +21,7 @@ describe('AuthController', () => {
],
}).compile();
controller = module.get<AuthController>(AuthController);
controller = module.get<VerifyAuthController>(VerifyAuthController);
});
it('should be defined', () => {

View File

@ -1,18 +1,18 @@
import { Body, Controller, Post } from '@nestjs/common';
import { AuthService } from '../services/auth.service';
import { VerifyInput } from '../dto/verify.input';
import { VerifyEntity } from '../dto/verify.entity';
import { Verify } from '../dto/verify.entity';
import { TokenService } from '../services/token.service';
@Controller('auth')
export class AuthController {
@Controller('auth/verify')
export class VerifyAuthController {
constructor(
private readonly authService: AuthService,
private readonly tokenService: TokenService,
) {}
@Post('verify')
async verify(@Body() verifyInput: VerifyInput): Promise<VerifyEntity> {
@Post()
async verify(@Body() verifyInput: VerifyInput): Promise<Verify> {
const email = await this.tokenService.verifyLoginToken(
verifyInput.loginToken,
);