2020-05-02 20:04:33 +02:00
|
|
|
import { Controller, Get, Res, UseFilters, UseGuards } from "@nestjs/common";
|
|
|
|
|
import { ConfigService } from "@nestjs/config";
|
2020-02-01 16:11:48 +01:00
|
|
|
import { AuthGuard } from "@nestjs/passport";
|
|
|
|
|
import { Response } from "express";
|
|
|
|
|
import { User } from "../users/user.entity";
|
|
|
|
|
import { AuthService } from "./auth.service";
|
2020-05-02 20:04:33 +02:00
|
|
|
import { ReqUser } from "./decorators/req-user.decorator";
|
|
|
|
|
import { SpotifyAuthFilter } from "./spotify.filter";
|
2020-02-01 16:11:48 +01:00
|
|
|
|
|
|
|
|
@Controller("api/v1/auth")
|
|
|
|
|
export class AuthController {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly authService: AuthService,
|
|
|
|
|
private readonly config: ConfigService
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
@Get("spotify")
|
|
|
|
|
@UseGuards(AuthGuard("spotify"))
|
|
|
|
|
spotifyRedirect() {
|
|
|
|
|
// User is redirected by AuthGuard
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Get("spotify/callback")
|
2020-05-02 20:04:33 +02:00
|
|
|
@UseFilters(SpotifyAuthFilter)
|
2020-02-01 16:11:48 +01:00
|
|
|
@UseGuards(AuthGuard("spotify"))
|
|
|
|
|
async spotifyCallback(@ReqUser() user: User, @Res() res: Response) {
|
|
|
|
|
const { accessToken } = await this.authService.createToken(user);
|
|
|
|
|
|
|
|
|
|
// Transmit accessToken to Frontend
|
|
|
|
|
res.cookie("listory_access_token", accessToken, {
|
|
|
|
|
// SPA will directly read cookie, save it to local storage and delete it
|
|
|
|
|
// 15 Minutes should be enough
|
|
|
|
|
maxAge: 15 * 60 * 1000,
|
|
|
|
|
|
|
|
|
|
// Must be readable by SPA
|
2020-05-02 17:17:20 +02:00
|
|
|
httpOnly: false,
|
2020-02-01 16:11:48 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Redirect User to SPA
|
2020-05-02 20:04:33 +02:00
|
|
|
res.redirect("/login/success?type=spotify");
|
2020-02-01 16:11:48 +01:00
|
|
|
}
|
|
|
|
|
}
|