diff --git a/.env.example b/.env.example index a49aef2..22bd1cb 100644 --- a/.env.example +++ b/.env.example @@ -8,3 +8,7 @@ DATABASE_DATABASE=poapper # Github GITHUB_TOKEN=xxxx + +# AWS Cognito +AWS_COGNITO_USER_POOL_ID=xxxx +AWS_COGNITO_CLIENT_ID=xxxx diff --git a/src/auth/auth.controller.ts b/src/auth/auth.controller.ts index 268eeb2..8b79b87 100644 --- a/src/auth/auth.controller.ts +++ b/src/auth/auth.controller.ts @@ -1,4 +1,26 @@ -import { Controller } from '@nestjs/common'; +import { + Body, + Controller, + Post, + UsePipes, + ValidationPipe, +} from '@nestjs/common'; + +import { AuthService } from './auth.service'; +import { AuthLoginUserDto, AuthRegisterUserDto } from './auth.dto'; @Controller('auth') -export class AuthController {} +export class AuthController { + constructor(private awsCognitoService: AuthService) {} + + @Post('/register') + async register(@Body() authRegisterUserDto: AuthRegisterUserDto) { + return await this.awsCognitoService.registerUser(authRegisterUserDto); + } + + @Post('/login') + @UsePipes(ValidationPipe) + async login(@Body() authLoginUserDto: AuthLoginUserDto) { + return await this.awsCognitoService.authenticateUser(authLoginUserDto); + } +} diff --git a/src/auth/auth.dto.ts b/src/auth/auth.dto.ts new file mode 100644 index 0000000..dd77833 --- /dev/null +++ b/src/auth/auth.dto.ts @@ -0,0 +1,10 @@ +export class AuthLoginUserDto { + readonly email: string; + readonly password: string; +} + +export class AuthRegisterUserDto { + readonly name: string; + readonly email: string; + readonly password: string; +} diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index a41c649..7082c75 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -1,4 +1,75 @@ import { Injectable } from '@nestjs/common'; +import { + AuthenticationDetails, + CognitoUser, + CognitoUserAttribute, + CognitoUserPool, +} from 'amazon-cognito-identity-js'; + +import { AuthLoginUserDto, AuthRegisterUserDto } from './auth.dto'; @Injectable() -export class AuthService {} +export class AuthService { + private userPool: CognitoUserPool; + + constructor() { + this.userPool = new CognitoUserPool({ + UserPoolId: process.env.AWS_COGNITO_USER_POOL_ID, + ClientId: process.env.AWS_COGNITO_CLIENT_ID, + }); + } + + async registerUser(authRegisterUserDto: AuthRegisterUserDto) { + const { name, email, password } = authRegisterUserDto; + + return new Promise((resolve, reject) => { + this.userPool.signUp( + email, + password, + [ + new CognitoUserAttribute({ + Name: 'name', + Value: name, + }), + ], + null, + (err, result) => { + if (!result) { + reject(err); + } else { + resolve(result.user); + } + }, + ); + }); + } + + async authenticateUser(authLoginUserDto: AuthLoginUserDto) { + const { email, password } = authLoginUserDto; + const userData = { + Username: email, + Pool: this.userPool, + }; + + const authenticationDetails = new AuthenticationDetails({ + Username: email, + Password: password, + }); + + const userCognito = new CognitoUser(userData); + + return new Promise((resolve, reject) => { + userCognito.authenticateUser(authenticationDetails, { + onSuccess: (result) => { + resolve({ + accessToken: result.getAccessToken().getJwtToken(), + refreshToken: result.getRefreshToken().getToken(), + }); + }, + onFailure: (err) => { + reject(err); + }, + }); + }); + } +}