/
auth.controller.ts
56 lines (50 loc) · 1.48 KB
/
auth.controller.ts
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
import {
BadRequestException,
Controller,
Get,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Request, Response } from 'express';
import { AuthService } from './auth.service';
import { GoogleOAuthGuard } from './guards/google-oauth.guard';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
@Controller('auth')
export class AuthController {
constructor(
private authService: AuthService,
private configService: ConfigService
) {}
@Get('google/login')
@UseGuards(GoogleOAuthGuard)
googleSignin() {
// handled by the auth guard
}
@Get('google/callback')
@UseGuards(GoogleOAuthGuard)
googleSigninCallback(@Req() req: Request, @Res() res: Response) {
const result = this.authService.googleLogin(req);
if (req.query.state && typeof req.query.state === 'string') {
try {
const origin = new URL(req.query.state);
origin.searchParams.set('access_token', result.tokens.accessToken);
return res.redirect(origin.href);
} catch (err) {
throw new BadRequestException('Invalid state provided');
}
}
return res.redirect('https://altairgraphql.dev');
}
@Get('me')
@UseGuards(JwtAuthGuard)
getUserProfile(@Req() req: Request) {
return this.authService.googleLogin(req);
}
@Get('slt')
@UseGuards(JwtAuthGuard)
getShortlivedEventsToken(@Req() req: Request) {
return { slt: this.authService.getShortLivedEventsToken(req.user.id) };
}
}