Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. /* global AWSCognito */
  2.  
  3. import actionTypes from './types';
  4.  
  5. AWSCognito.config.region = 'us-east-1';
  6. AWSCognito.config.credentials = {
  7. accessKeyId: "...",
  8. secretAccessKey: "...",
  9. };
  10.  
  11. const userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool({
  12. UserPoolId: '...',
  13. ClientId: '...',
  14. });
  15.  
  16. /**
  17. * @returns {Object} Action data
  18. */
  19. export function gotoLogin() {
  20. return {
  21. type: actionTypes.GOTO_LOGIN,
  22. };
  23. }
  24.  
  25. /**
  26. * @returns {Object} Action data
  27. */
  28. export function gotoRegistration() {
  29. return {
  30. type: actionTypes.GOTO_REGISTRATION,
  31. };
  32. }
  33.  
  34. /**
  35. * @returns {Object} Action data
  36. */
  37. export function gotoUsers() {
  38. return {
  39. type: actionTypes.GOTO_USERS,
  40. };
  41. }
  42.  
  43. /**
  44. * @param {string} email - user email address
  45. * @returns {Object} Action data
  46. */
  47. export function registerSuccess(email) {
  48. return {
  49. type: actionTypes.REGISTRATION_SUCCESS,
  50. email,
  51. };
  52. }
  53.  
  54. /**
  55. * @param {string} message - registration error message
  56. * @returns {Object} Action data
  57. */
  58. export function registerFailure(message) {
  59. return {
  60. type: actionTypes.REGISTRATION_FAILURE,
  61. message,
  62. };
  63. }
  64.  
  65. /**
  66. * @param {string} email - user email address
  67. * @param {string} password - user password
  68. * @returns {Object} Action data
  69. */
  70. export function registerUser(email, password) {
  71. return dispatch => {
  72. const attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute({
  73. Name: "email",
  74. Value: email,
  75. });
  76.  
  77. userPool.signUp(email, password, [attributeEmail], null, (err, result) => {
  78. if (err) {
  79. dispatch(registerFailure(err.message));
  80. }
  81.  
  82. dispatch(registerSuccess(result.user.username));
  83. });
  84. };
  85. }
  86.  
  87. /**
  88. * @param {string} message - confirmation error message
  89. * @returns {Object} Action data
  90. */
  91. export function confirmationError(message){
  92. return {
  93. type: actionTypes.CONFIRMATION_ERROR,
  94. message,
  95. };
  96. }
  97.  
  98. /**
  99. * @param {string} email - user email address
  100. * @returns {Object} Action data
  101. */
  102. export function confirmationSuccess(email){
  103. return {
  104. type: actionTypes.CONFIRMATION_SUCCESS,
  105. email,
  106. };
  107. }
  108.  
  109. /**
  110. * @param {string} email - user email address
  111. * @param {string} activationCode - user activationCode
  112. * @returns {Object} Action data
  113. */
  114. export function activateUser(email, activationCode) {
  115. return dispatch => {
  116. const userData = {
  117. Username: email,
  118. Pool: userPool,
  119. };
  120.  
  121. const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  122.  
  123. cognitoUser.confirmRegistration(activationCode, true, err => {
  124. if (err){
  125. dispatch(confirmationError(err));
  126. }
  127.  
  128. dispatch(confirmationSuccess(email));
  129. });
  130. };
  131. }
  132.  
  133.  
  134. /**
  135. * @param {string} email - user email address
  136. * @param {Object} token - authenticated JSON Web token
  137. * @returns {Object} Action data
  138. */
  139. export function authenticationSuccess(email, token) {
  140. return {
  141. type: actionTypes.AUTHENTICATION_SUCCESS,
  142. email,
  143. token,
  144. };
  145. }
  146.  
  147. /**
  148. * @param {string} email - user email address
  149. * @param {Object} error - authentication error
  150. * @returns {Object} Action data
  151. */
  152. export function authenticationFailure(email, error) {
  153. return {
  154. type: actionTypes.AUTHENTICATION_FAILURE,
  155. email,
  156. error,
  157. };
  158. }
  159.  
  160. /**
  161. * @param {string} email - user email address
  162. * @param {Object} error - authentication error
  163. * @returns {Object} Action data
  164. */
  165. export function logoutSuccessful() {
  166. return {
  167. type: actionTypes.LOGOUT_SUCCESSFUL,
  168. };
  169. }
  170.  
  171. /**
  172. * @param {string} email - user email
  173. * @param {string} password - user password
  174. * @returns {object} new state
  175. */
  176. export function authenticateUser(email, password) {
  177. return dispatch => {
  178. const userData = {
  179. Username: email,
  180. Pool: userPool,
  181. };
  182.  
  183. const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  184.  
  185. const authenticationData = {
  186. Username: email,
  187. Password: password,
  188. };
  189.  
  190. const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
  191.  
  192. cognitoUser.authenticateUser(authenticationDetails, {
  193. onSuccess: result => {
  194. dispatch(authenticationSuccess(cognitoUser, result.getAccessToken().getJwtToken()));
  195. },
  196.  
  197. onFailure: err => {
  198. dispatch(authenticationFailure(cognitoUser, err));
  199. },
  200. });
  201. };
  202. }
  203.  
  204. /**
  205. * @param {string} email - user email
  206. * @returns {object} new state
  207. */
  208. export function logoutUser(email) {
  209. return dispatch => {
  210. const userData = {
  211. Username: email,
  212. Pool: userPool,
  213. };
  214.  
  215. const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
  216. cognitoUser.signOut();
  217.  
  218. dispatch(logoutSuccessful());
  219. };
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement