Created
October 24, 2022 12:38
-
-
Save akkonrad/4959919bed3c4c503b649fd697385b3a to your computer and use it in GitHub Desktop.
auth/auth.service.ts
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
import { Injectable } from '@nestjs/common'; | |
import { | |
AuthenticationDetails, | |
CognitoUser, | |
CognitoUserPool, | |
CognitoUserSession, | |
} from 'amazon-cognito-identity-js'; | |
import { AuthConfig } from './auth.config'; | |
@Injectable() | |
export class AuthService { | |
private readonly userPool: CognitoUserPool; | |
constructor(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: CognitoUserSession) => { | |
resolve(result); | |
}, | |
onFailure: (err) => { | |
reject(err); | |
}, | |
}); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment