-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathauth-github.controller.ts
More file actions
65 lines (62 loc) · 2.1 KB
/
Copy pathauth-github.controller.ts
File metadata and controls
65 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
AuthenticationJwtResponseDto,
AuthPublic,
AuthUser,
IssueTokenServiceInterface,
} from '@concepta/nestjs-authentication';
import {
AuthenticatedUserInterface,
AuthenticationResponseInterface,
} from '@concepta/nestjs-common';
import { Controller, Get, Inject, UseGuards } from '@nestjs/common';
import { ApiOkResponse, ApiTags } from '@nestjs/swagger';
import { AUTH_GITHUB_ISSUE_TOKEN_SERVICE_TOKEN } from './auth-github.constants';
import { AuthGithubGuard } from './auth-github.guard';
// TODO: improve documentation
/**
* Github controller
*
* Flow of how github works:
*
* - Client call auth/github/login to be redirected to github login page to require authentication
* - After authorized by github, the user will be redirected to the callback url defined in the config
* - The auth/github/callback url will be called with the code as a query parameter
* - The code will be used to get the access token and user profile from github
* - The user profile will be used to create a new user or return the existing user from federated module
* - The user will be authenticated and a token will be issued
* - The token will be returned to the client
* - The client can use the token to make requests to the protected resources
*
*/
@Controller('auth/github')
@UseGuards(AuthGithubGuard)
@AuthPublic()
@ApiTags('auth')
export class AuthGithubController {
constructor(
@Inject(AUTH_GITHUB_ISSUE_TOKEN_SERVICE_TOKEN)
private issueTokenService: IssueTokenServiceInterface,
) {}
/**
* Login
*/
@ApiOkResponse({
description: 'Users are redirected to request their GitHub identity.',
})
@Get('login')
login(): void {
// TODO: no code needed, Decorator will redirect to github
return;
}
// TODO: Check why post does not work for a callback
@ApiOkResponse({
type: AuthenticationJwtResponseDto,
description: 'DTO containing an access token and a refresh token.',
})
@Get('callback')
async get(
@AuthUser() user: AuthenticatedUserInterface,
): Promise<AuthenticationResponseInterface> {
return this.issueTokenService.responsePayload(user.id);
}
}