Guest User

Untitled

a guest
Jun 27th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. ```javascript
  2. import { AuthenticationDetails, CognitoUserPool, CognitoUser } from 'amazon-cognito-identity-js';
  3.  
  4. export default class CognitoUserPoolService{
  5. constructor(config){
  6. if (!config || !config.aws)
  7. throw new Error("aws configuration is missing.");
  8. if (!config.aws.cognitoUserPoolId)
  9. throw new Error("cognito userId is required.");
  10. if (!config.aws.clientId)
  11. throw new Error("App cliendId is required.");
  12.  
  13. this.config = config;
  14. }
  15.  
  16. signIn(username,password){
  17. if (!username)
  18. throw new Error("username is required.");
  19. if (!password)
  20. throw new Error("password is required.");
  21.  
  22. var authenticationData = {
  23. Username : username,
  24. Password : password
  25. };
  26. var authenticationDetails = new AuthenticationDetails(authenticationData);
  27.  
  28. var poolData = {
  29. UserPoolId : this.config.aws.cognitoUserPoolId,
  30. ClientId : this.config.aws.clientId
  31. };
  32. var userPool = new CognitoUserPool(poolData);
  33. var userData = {
  34. Username : username,
  35. Pool : userPool
  36. };
  37. var cognitoUser = new CognitoUser(userData);
  38. cognitoUser.authenticateUser(authenticationDetails, {
  39. onSuccess: function (result) {
  40. console.log('access token',result.getAccessToken().getJwtToken());
  41. console.log('id token',result.getIdToken().getJwtToken());
  42. console.log('refresh token',result.getRefreshToken().getJwtToken());
  43. AWSConfigHelper.configure(result.getIdToken().getJwtToken());
  44. },
  45. onFailure: function(err) {
  46. console.log(err.message || JSON.stringify(err));
  47. },
  48.  
  49. });
  50. }
  51. }
  52. ```
Add Comment
Please, Sign In to add comment