Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. import { Config, CognitoIdentityCredentials } from 'aws-sdk'
  2. import { CognitoUser, CognitoUserPool, AuthenticationDetails, CognitoUserAttribute } from 'amazon-cognito-identity-js'
  3. import cogConfig from '../config/config'
  4.  
  5. export default class CognitoAuth {
  6. constructor () {
  7. this.userSession = null
  8. this.configure(cogConfig)
  9. }
  10.  
  11. isAuthenticated (cb) {
  12. let cognitoUser = this.getCurrentUser()
  13. if (cognitoUser != null) {
  14. cognitoUser.getSession((err, session) => {
  15. if (err) {
  16. return cb(err, false)
  17. }
  18. return cb(null, true)
  19. })
  20. } else {
  21. cb(null, false)
  22. }
  23. }
  24.  
  25. configure (config) {
  26. if (typeof config !== 'object' || Array.isArray(config)) {
  27. throw new Error('[CognitoAuth error] valid option object required')
  28. }
  29. this.userPool = new CognitoUserPool({
  30. UserPoolId: config.UserPoolId,
  31. ClientId: config.ClientId
  32. })
  33. Config.region = config.region
  34. Config.credentials = new CognitoIdentityCredentials({
  35. IdentityPoolId: config.IdentityPoolId
  36. })
  37. this.options = config
  38. }
  39.  
  40. signup(user, name, familyName, middleName, role, companyAccountId, cb) {
  41. let attributeList = [
  42. new CognitoUserAttribute({
  43. Name: 'name',
  44. Value: name
  45. }),
  46. new CognitoUserAttribute({
  47. Name: 'family_name',
  48. Value: familyName
  49. }),
  50. new CognitoUserAttribute({
  51. Name: 'middle_name',
  52. Value: middleName
  53. })
  54. ];
  55. this.userPool.signUp(user, 'css2523*', attributeList, null, cb)
  56. }
  57.  
  58. authenticate (username, pass, cb) {
  59.  
  60. let authenticationData = { Username: username, Password: pass }
  61. let authenticationDetails = new AuthenticationDetails(authenticationData)
  62. let userData = { Username: username, Pool: this.userPool }
  63. let cognitoUser = new CognitoUser(userData)
  64.  
  65. cognitoUser.authenticateUser(authenticationDetails, {
  66. onSuccess: function (result) {
  67. console.log('access token + ' + result.getAccessToken().getJwtToken())
  68. /*var logins = {}
  69. logins['cognito-idp.' + this.options.region + '.amazonaws.com/' + this.options.UserPoolId] = result.getIdToken().getJwtToken()
  70. console.log(logins)
  71.  
  72.  
  73. Config.credentials = new CognitoIdentityCredentials({
  74. IdentityPoolId: this.options.UserPoolId,
  75. Logins: logins
  76. })*/
  77. //console.log(Config.credentials)
  78. //this.onChange(true)
  79. cb(true, result)
  80. },
  81. onFailure: function (err) {
  82. cb(false, err);
  83. },
  84. newPasswordRequired: function (userAttributes, requiredAttributes) {
  85. console.log('New Password Is Required')
  86. }
  87. })
  88. }
  89.  
  90. getCurrentUser () {
  91. return this.userPool.getCurrentUser()
  92. }
  93.  
  94. confirmRegistration (username, code, cb) {
  95. let cognitoUser = new CognitoUser({
  96. Username: username,
  97. Pool: this.userPool
  98. })
  99. cognitoUser.confirmRegistration(code, true, cb)
  100. }
  101.  
  102. /**
  103. * Logout of your cognito session.
  104. */
  105. logout () {
  106. this.getCurrentUser().signOut()
  107. }
  108.  
  109. /**
  110. * Resolves the current token based on a user session. If there
  111. * is no session it returns null.
  112. * @param {*} cb callback
  113. */
  114. getIdToken (cb) {
  115. if (this.getCurrentUser() == null) {
  116. return cb(null, null)
  117. }
  118. this.getCurrentUser().getSession((err, session) => {
  119. if (err) return cb(err)
  120. if (session.isValid()) {
  121. return cb(null, session.getIdToken().getJwtToken())
  122. }
  123. cb(Error('Session is invalid'))
  124. })
  125. }
  126.  
  127. recoverPassword(user, pass, cb){
  128. let userData = { Username: user, Pool: this.userPool }
  129. let cognitoUser = new CognitoUser(userData)
  130. cognitoUser.forgotPassword({
  131. onSuccess: function (data) {
  132. // successfully initiated reset password request
  133. console.log('CodeDeliveryData from forgotPassword: ' + data);
  134. },
  135. onFailure: function(err) {
  136. alert(err.message || JSON.stringify(err));
  137. },
  138. //Optional automatic callback
  139. inputVerificationCode: function(data) {
  140. var verificationCode = prompt('Por favor introduce el código de verificación enviado: ' ,'');
  141. cognitoUser.confirmPassword(verificationCode, pass, {
  142. onSuccess() {
  143. cb(true);
  144. },
  145. onFailure(err) {
  146. cb(err);
  147. }
  148. });
  149. }
  150. });
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement