Skip to content

Commit

Permalink
implt auth done
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 committed Nov 13, 2023
1 parent 1debb9f commit fa7f200
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
26 changes: 16 additions & 10 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import {
Body,
Controller,
Get,
Post,
UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import { Body, Controller, Get, Post, Res, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

import { AuthService } from './auth.service';
import { AuthLoginUserDto, AuthRegisterUserDto } from './auth.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('auth')
export class AuthController {
Expand All @@ -20,8 +15,19 @@ export class AuthController {
}

@Post('/login')
async login(@Body() authLoginUserDto: AuthLoginUserDto) {
return await this.awsCognitoService.authenticateUser(authLoginUserDto);
async login(
@Body() authLoginUserDto: AuthLoginUserDto,
@Res() res: Response,
) {
const cognitoAuth = await this.awsCognitoService.authenticateUser(
authLoginUserDto,
);

res.setHeader(
'Set-Cookie',
`Authentication=${cognitoAuth['accessToken']}; HttpOnly; Path=/;`,
);
return res.send();
}

@Get('check')
Expand Down
7 changes: 6 additions & 1 deletion src/auth/passport/jwt.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { passportJwtSecret } from 'jwks-rsa';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { Request } from 'express';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
return request?.cookies?.Authentication;
},
]),
ignoreExpiration: false,
_audience: process.env.AWS_COGNITO_CLIENT_ID,
issuer: `https://cognito-idp.ap-northeast-2.amazonaws.com/${process.env.AWS_COGNITO_USER_POOL_ID}`,
Expand Down

0 comments on commit fa7f200

Please sign in to comment.