Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```javascript
- import { AuthenticationDetails, CognitoUserPool, CognitoUser } from 'amazon-cognito-identity-js';
- export default class CognitoUserPoolService{
- constructor(config){
- if (!config || !config.aws)
- throw new Error("aws configuration is missing.");
- if (!config.aws.cognitoUserPoolId)
- throw new Error("cognito userId is required.");
- if (!config.aws.clientId)
- throw new Error("App cliendId is required.");
- this.config = config;
- }
- signIn(username,password){
- if (!username)
- throw new Error("username is required.");
- if (!password)
- throw new Error("password is required.");
- var authenticationData = {
- Username : username,
- Password : password
- };
- var authenticationDetails = new AuthenticationDetails(authenticationData);
- var poolData = {
- UserPoolId : this.config.aws.cognitoUserPoolId,
- ClientId : this.config.aws.clientId
- };
- var userPool = new CognitoUserPool(poolData);
- var userData = {
- Username : username,
- Pool : userPool
- };
- var cognitoUser = new CognitoUser(userData);
- cognitoUser.authenticateUser(authenticationDetails, {
- onSuccess: function (result) {
- console.log('access token',result.getAccessToken().getJwtToken());
- console.log('id token',result.getIdToken().getJwtToken());
- console.log('refresh token',result.getRefreshToken().getJwtToken());
- AWSConfigHelper.configure(result.getIdToken().getJwtToken());
- },
- onFailure: function(err) {
- console.log(err.message || JSON.stringify(err));
- },
- });
- }
- }
- ```
Add Comment
Please, Sign In to add comment