Skip to content

Commit

Permalink
implt auth api
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueHorn07 committed Nov 9, 2023
1 parent cd68553 commit 254a312
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ DATABASE_DATABASE=poapper

# Github
GITHUB_TOKEN=xxxx

# AWS Cognito
AWS_COGNITO_USER_POOL_ID=xxxx
AWS_COGNITO_CLIENT_ID=xxxx
26 changes: 24 additions & 2 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions src/auth/auth.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
73 changes: 72 additions & 1 deletion src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -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);
},
});
});
}
}

0 comments on commit 254a312

Please sign in to comment.