Add /auth endpoint
This commit is contained in:
parent
92ac80f4fa
commit
fc7a87a7c7
|
@ -1,6 +1,30 @@
|
|||
import { Controller, Get } from '@nestjs/common';
|
||||
import { ApiInfo, AppService } from './app.service';
|
||||
import { ApiOperation } from '@nestjs/swagger';
|
||||
import { Controller, Get, UseGuards } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
import {
|
||||
ApiOperation,
|
||||
ApiProperty,
|
||||
ApiResponse,
|
||||
ApiUnauthorizedResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import { META } from './meta';
|
||||
import { AuthData } from './auth/authdata.decorator';
|
||||
import { AuthGuard, AuthenticationData } from './auth/auth.guard';
|
||||
|
||||
export class ApiInfo {
|
||||
@ApiProperty()
|
||||
version: string;
|
||||
}
|
||||
|
||||
export class AuthInfo {
|
||||
@ApiProperty()
|
||||
authenticated: boolean;
|
||||
|
||||
@ApiProperty()
|
||||
user: string;
|
||||
|
||||
@ApiProperty({ default: false })
|
||||
god: boolean;
|
||||
}
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
|
@ -9,6 +33,24 @@ export class AppController {
|
|||
@Get()
|
||||
@ApiOperation({ summary: 'Fetch API configuration' })
|
||||
getHello(): ApiInfo {
|
||||
return this.appService.getRoot();
|
||||
return {
|
||||
version: META.version,
|
||||
};
|
||||
}
|
||||
|
||||
@Get('/auth')
|
||||
@UseGuards(AuthGuard)
|
||||
@ApiOperation({ summary: 'Check authentication status' })
|
||||
@ApiResponse({ type: AuthInfo, status: 200 })
|
||||
@ApiUnauthorizedResponse({
|
||||
description: 'Request will fail if the user is not authenticated',
|
||||
})
|
||||
getAuth(@AuthData() auth: AuthenticationData): AuthInfo {
|
||||
return {
|
||||
// Since the AuthGuard stops the request otherwise, we can assume the user is authenticated
|
||||
authenticated: true,
|
||||
user: auth.user,
|
||||
god: auth.god || false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,4 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { META } from './meta';
|
||||
|
||||
export interface ApiInfo {
|
||||
version: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getRoot(): ApiInfo {
|
||||
return {
|
||||
version: META.version,
|
||||
};
|
||||
}
|
||||
}
|
||||
export class AppService {}
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
import {
|
||||
createParamDecorator,
|
||||
ExecutionContext,
|
||||
InternalServerErrorException,
|
||||
} from '@nestjs/common';
|
||||
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
|
||||
import { AuthenticationData } from './auth.guard';
|
||||
|
||||
export const AuthData = createParamDecorator(
|
||||
(data: unknown, ctx: ExecutionContext) => {
|
||||
const request = ctx.switchToHttp().getRequest();
|
||||
if (!request.auth_data)
|
||||
throw new InternalServerErrorException(
|
||||
'Authentication data not present in request object',
|
||||
);
|
||||
return request.auth_data as AuthenticationData;
|
||||
//if (!request.auth_data)
|
||||
// throw new InternalServerErrorException(
|
||||
// 'Authentication data not present in request object',
|
||||
// );
|
||||
return request.auth_data as AuthenticationData | undefined;
|
||||
},
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue