Guest User

Untitled

a guest
Jan 4th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. app.get('/', (req, res) => {
  2. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  3. let cognitoUser = userPool.getCurrentUser();
  4.  
  5. if(cognitoUser === null) {
  6. res.render('index')
  7. }
  8. else {
  9. res.redirect('/dashboard')
  10. }
  11.  
  12.  
  13. });
  14.  
  15. app.get('/dashboard', (req, res) => {
  16. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  17. let cognitoUser = userPool.getCurrentUser();
  18.  
  19. if (cognitoUser != null) {
  20. cognitoUser.getSession(function (err, session) {
  21. if (err) {
  22. req.flash('error_msg', err.message)
  23. res.redirect('/');
  24. }
  25. if (session.isValid()) {
  26. cognitoUser.getUserAttributes(function (err, result) {
  27. if (err) {
  28. req.flash('error_msg', err.message)
  29. res.redirect('/');
  30. }
  31. let token = session['idToken']['jwtToken'];
  32. ValidateToken(token);
  33. res.render('dashboard', {name: result[2].getValue(), email: result[3].getValue()})
  34.  
  35. });
  36. }
  37.  
  38. });
  39.  
  40. }
  41. else {
  42. req.flash('error_msg', 'You are not logged in...')
  43. res.redirect('/');
  44. }
  45.  
  46.  
  47. })
  48.  
  49. app.post('/login', (req, res) => {
  50. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  51. let username = req.body.email;
  52. let password = req.body.password;
  53.  
  54. let authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails({
  55. Username : username,
  56. Password : password,
  57. });
  58.  
  59. let userData = {
  60. Username : username,
  61. Pool : userPool
  62. };
  63.  
  64. let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
  65.  
  66. cognitoUser.authenticateUser(authenticationDetails, {
  67. onSuccess: function (result) {
  68. // console.log('access token + ' + result.getAccessToken().getJwtToken());
  69. // console.log('id token + ' + result.getIdToken().getJwtToken());
  70. // console.log('refresh token + ' + result.getRefreshToken().getToken());
  71. req.flash('success_msg', 'Logged in');
  72. res.redirect('/dashboard');
  73. },
  74. onFailure: function(err) {
  75. req.flash('success_msg', err.message);
  76. res.redirect('/dashboard');
  77. console.log(err);
  78. },
  79.  
  80. });
  81.  
  82. })
  83.  
  84. app.get('/register', (req, res) => {
  85. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  86. let cognitoUser = userPool.getCurrentUser();
  87. if(cognitoUser === null) {
  88. res.render('register')
  89. }
  90. else {
  91. res.redirect('/dashboard')
  92. }
  93.  
  94.  
  95. })
  96.  
  97.  
  98. app.post('/register', (req, res) => {
  99. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  100. let username = req.body.email;
  101. let password = req.body.password;
  102. let personalname = req.body.name;
  103.  
  104. let attributeList = [];
  105.  
  106. let dataEmail = {
  107. Name : 'email',
  108. Value : username, //get from form field
  109. };
  110.  
  111. let dataPersonalName = {
  112. Name : 'name',
  113. Value : personalname, //get from form field
  114. };
  115.  
  116. let attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
  117. let attributePersonalName = new AmazonCognitoIdentity.CognitoUserAttribute(dataPersonalName);
  118.  
  119.  
  120. attributeList.push(attributeEmail);
  121. attributeList.push(attributePersonalName);
  122.  
  123. userPool.signUp(username, password, attributeList, null, function(err, result){
  124. if (err) {
  125. req.flash('error_msg', err.message);
  126. res.redirect('/register');
  127. return;
  128. }
  129. res.render('verify', {user: result.user.getUsername()})
  130. });
  131.  
  132.  
  133.  
  134. })
  135.  
  136. app.post('/activation-code', (req, res) => {
  137. let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
  138. let email = req.body.username;
  139. let activation_code = req.body.activation_code;
  140.  
  141. let userData = {
  142. Username: email,
  143. Pool: userPool
  144. };
  145.  
  146. let cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
  147.  
  148. // signing up
  149. cognitoUser.confirmRegistration(activation_code, false, (error, result) => {
  150. if (error) {
  151. console.log(error)
  152. req.flash('error_msg', error.message)
  153. res.render('verify', {user: email})
  154. }
  155. else {
  156. req.flash('success_msg', 'You have successfully verified your email. You can now log in.')
  157. res.redirect('/')
  158. }
  159. });
  160. })
Add Comment
Please, Sign In to add comment