Guest User

Untitled

a guest
Mar 19th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. import { CognitoUserPool, CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js';
  2.  
  3. const options = {
  4. apiVersion: '2012-08-10',
  5. region: 'us-east-1',
  6. UserPoolId : '', // Your user pool id here
  7. ClientId : '' // Your client id here
  8. }
  9.  
  10. const userPool = new CognitoUserPool(options);
  11.  
  12. export default class Authenication {
  13.  
  14. static signupUser(username,password){
  15.  
  16. return new Promise((resolve)=>{
  17.  
  18. userPool.signUp(username, password, null, null,(err, result)=>{
  19. if (err) {
  20. resolve(err);
  21. }
  22. resolve(result)
  23. })
  24. })
  25. }
  26.  
  27. static loginUser(username,password){
  28. const authenticationData = {
  29. Username:username,
  30. Password:password,
  31. };
  32. const authenticationDetails = new AuthenticationDetails(authenticationData);
  33.  
  34. const userData = {
  35. Username : username,
  36. Pool : userPool
  37. };
  38.  
  39. const cognitoUser = new CognitoUser(userData);
  40.  
  41.  
  42. return new Promise((resolve,reject)=>{
  43. cognitoUser.authenticateUser(authenticationDetails, {
  44. onSuccess: (result)=> {
  45. resolve(result)
  46. },
  47. onFailure: (err) => {
  48. reject(err);
  49. },
  50. });
  51. },
  52. )
  53. }
  54.  
  55. static getIdToken(){
  56.  
  57. return new Promise((resolve, reject) =>{
  58. const users = userPool.getCurrentUser()
  59.  
  60. if (!users) {
  61. resolve('')
  62. }
  63. userPool.getCurrentUser().getSession((err, session)=>{
  64. if (err) {
  65. reject(err)
  66. }
  67. resolve(session.getIdToken().getJwtToken())
  68. })}
  69. )
  70. }
  71.  
  72. static getUser = () => userPool.getCurrentUser();
  73.  
  74.  
  75. }
Add Comment
Please, Sign In to add comment