|
@@ -1,25 +1,26 @@
|
|
|
-import { Body, Controller, Get, Post, Req } from '@nestjs/common';
|
|
|
|
|
|
|
+import { Args, Context, Mutation, Query, Resolver } from '@nestjs/graphql';
|
|
|
|
|
|
|
|
import { Role } from '../../common/types/role';
|
|
import { Role } from '../../common/types/role';
|
|
|
import { User } from '../../entity/user/user.entity';
|
|
import { User } from '../../entity/user/user.entity';
|
|
|
import { AuthService } from '../../service/auth.service';
|
|
import { AuthService } from '../../service/auth.service';
|
|
|
|
|
+import { ChannelService } from '../../service/channel.service';
|
|
|
import { RolesGuard } from '../roles-guard';
|
|
import { RolesGuard } from '../roles-guard';
|
|
|
|
|
|
|
|
-@Controller('auth')
|
|
|
|
|
-export class AuthController {
|
|
|
|
|
- constructor(private authService: AuthService) {}
|
|
|
|
|
|
|
+@Resolver('Auth')
|
|
|
|
|
+export class AuthResolver {
|
|
|
|
|
+ constructor(private authService: AuthService, private channelService: ChannelService) {}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Attempts a login given the username and password of a user. If successful, returns
|
|
* Attempts a login given the username and password of a user. If successful, returns
|
|
|
* the user data and a token to be used by Bearer auth.
|
|
* the user data and a token to be used by Bearer auth.
|
|
|
*/
|
|
*/
|
|
|
- @Post('login')
|
|
|
|
|
- async login(@Body() loginDto: { username: string; password: string }) {
|
|
|
|
|
- const { user, token } = await this.authService.createToken(loginDto.username, loginDto.password);
|
|
|
|
|
|
|
+ @Mutation()
|
|
|
|
|
+ async login(@Args() args: { username: string; password: string }) {
|
|
|
|
|
+ const { user, token } = await this.authService.createToken(args.username, args.password);
|
|
|
|
|
|
|
|
if (token) {
|
|
if (token) {
|
|
|
return {
|
|
return {
|
|
|
- token,
|
|
|
|
|
|
|
+ authToken: token,
|
|
|
user: this.publiclyAccessibleUser(user),
|
|
user: this.publiclyAccessibleUser(user),
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
@@ -29,20 +30,25 @@ export class AuthController {
|
|
|
* Returns information about the current authenticated user.
|
|
* Returns information about the current authenticated user.
|
|
|
*/
|
|
*/
|
|
|
@RolesGuard([Role.Authenticated])
|
|
@RolesGuard([Role.Authenticated])
|
|
|
- @Get('me')
|
|
|
|
|
- async me(@Req() request) {
|
|
|
|
|
|
|
+ @Query()
|
|
|
|
|
+ me(@Context('req') request: any) {
|
|
|
const user = request.user as User;
|
|
const user = request.user as User;
|
|
|
- return this.publiclyAccessibleUser(user);
|
|
|
|
|
|
|
+ return user ? this.publiclyAccessibleUser(user) : null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Exposes a subset of the User properties which we want to expose to the public API.
|
|
* Exposes a subset of the User properties which we want to expose to the public API.
|
|
|
*/
|
|
*/
|
|
|
- private publiclyAccessibleUser(user: User): Pick<User, 'id' | 'identifier' | 'roles'> {
|
|
|
|
|
|
|
+ private publiclyAccessibleUser(
|
|
|
|
|
+ user: User,
|
|
|
|
|
+ ): Pick<User, 'id' | 'identifier' | 'roles'> & { channelTokens: string[] } {
|
|
|
return {
|
|
return {
|
|
|
id: user.id,
|
|
id: user.id,
|
|
|
identifier: user.identifier,
|
|
identifier: user.identifier,
|
|
|
roles: user.roles,
|
|
roles: user.roles,
|
|
|
|
|
+ channelTokens: user.roles.includes(Role.SuperAdmin)
|
|
|
|
|
+ ? [this.channelService.getDefaultChannel().token]
|
|
|
|
|
+ : [],
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|