-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd68553
commit 254a312
Showing
4 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}); | ||
}); | ||
} | ||
} |