Last active
June 8, 2020 09:08
-
-
Save jacobdo2/c30594b71fe5e9173558ee9707ac9b4c to your computer and use it in GitHub Desktop.
AuthService
This file contains hidden or 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
import { AuthConfig } from './auth.config'; | |
import { Inject, Injectable } from '@nestjs/common'; | |
import { | |
AuthenticationDetails, | |
CognitoUser, | |
CognitoUserPool, | |
} from 'amazon-cognito-identity-js'; | |
@Injectable() | |
export class AuthService { | |
private userPool: CognitoUserPool; | |
constructor( | |
@Inject('AuthConfig') | |
private readonly authConfig: AuthConfig, | |
) { | |
this.userPool = new CognitoUserPool({ | |
UserPoolId: this.authConfig.userPoolId, | |
ClientId: this.authConfig.clientId, | |
}); | |
} | |
authenticateUser(user: { name: string, password: string}) { | |
const { name, password } = user; | |
const authenticationDetails = new AuthenticationDetails({ | |
Username: name, | |
Password: password, | |
}); | |
const userData = { | |
Username: name, | |
Pool: this.userPool, | |
}; | |
const newUser = new CognitoUser(userData); | |
return new Promise((resolve, reject) => { | |
return newUser.authenticateUser(authenticationDetails, { | |
onSuccess: result => { | |
resolve(result); | |
}, | |
onFailure: err => { | |
reject(err); | |
}, | |
}); | |
}); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment