forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.controller.ts
More file actions
41 lines (37 loc) · 1.39 KB
/
Copy pathauth.controller.ts
File metadata and controls
41 lines (37 loc) · 1.39 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
import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ApiExcludeEndpoint, ApiTags } from '@nestjs/swagger';
import type { Request, Response } from 'express';
import { AuthService } from './auth.service';
import { GithubAuthGuard } from './guards/github-auth.guard';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { AppConfig } from '../config/configuration';
import type { UpsertFromGithubInput } from '../users/users.service';
@ApiTags('auth')
@Controller('auth')
export class AuthController {
constructor(
private readonly authService: AuthService,
private readonly configService: ConfigService<AppConfig, true>,
) {}
@Get('github')
@UseGuards(GithubAuthGuard)
@ApiExcludeEndpoint()
githubLogin() {
// Redirect handled by passport-github2; this handler body never runs.
}
@Get('github/callback')
@UseGuards(GithubAuthGuard)
@ApiExcludeEndpoint()
async githubCallback(@Req() req: Request, @Res() res: Response) {
const profile = req.user as UpsertFromGithubInput;
const { accessToken } = await this.authService.loginWithGithub(profile);
const frontendUrl = this.configService.get('frontendUrl', { infer: true });
res.redirect(`${frontendUrl}/auth/callback?token=${accessToken}`);
}
@Get('me')
@UseGuards(JwtAuthGuard)
me(@Req() req: Request) {
return req.user;
}
}