Advertisement
Guest User

Untitled

a guest
May 15th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // Dependencies ===============
  2.  
  3. import {Auth} from 'aws-amplify'
  4. const store = {namespaced: true}
  5.  
  6. // State ======================
  7.  
  8. store.state = {
  9. authorized: false,
  10. user: null,
  11. loginError: '',
  12. signupError: '',
  13. confirm: false,
  14. confirmError: '',
  15. }
  16.  
  17. // Mutations ==================
  18.  
  19. store.mutations = {
  20. user(state, user){
  21. state.authorized = !!user && user.attributes && user.attributes.email_verified
  22. state.user = user
  23. },
  24. confirm(state, showConfirm){
  25. state.confirm = !!showConfirm
  26. },
  27. }
  28.  
  29. // Actions ====================
  30.  
  31. store.actions = {
  32. async login({dispatch, state}, {email, password}){
  33. state.loginError = ''
  34. try{
  35. await Auth.signIn(email, password)
  36. }catch(err){
  37. console.log(`Login Error [${err}]`)
  38. if(err)
  39. state.loginError = err.message || err
  40. return
  41. }
  42. await dispatch('fetchUser')
  43. },
  44. async signup({commit, state}, {email, password}){
  45. state.signupError = ''
  46. try{
  47. await Auth.signUp({
  48. username: email,
  49. email: email,
  50. password: password,
  51. attributes: {
  52. email: email,
  53. },
  54. validationData: [],
  55. })
  56. //switch email confirmation form
  57. commit('confirm', true)
  58. }catch(err){
  59. console.log(`Signup Error [${err}]`)
  60. if(err)
  61. state.signupError = err.message || err
  62. commit('confirm', false)
  63. }
  64. },
  65. async confirm({commit, dispatch, state}, {email, code}){
  66. state.confirmError = ''
  67. try{
  68. await Auth.confirmSignUp(email, code, {
  69. forceAliasCreation: true,
  70. })
  71. }catch(err){
  72. console.log(`Confirm Error [${err}]`)
  73. if(err)
  74. state.confirmError = err.message || err
  75. return
  76. }
  77. commit('confirm', false)
  78. },
  79. async authState({commit, dispatch}, state){
  80. if(state === 'signedIn')
  81. await dispatch('fetchUser')
  82. else
  83. commit('user', null)
  84. },
  85. async fetchUser({commit, dispatch}){
  86. try{
  87. const user = await Auth.currentAuthenticatedUser()
  88. const expires = user.getSignInUserSession().getIdToken().payload.exp - Math.floor(new Date().getTime() / 1000)
  89. console.log(`Token expires in ${expires} seconds`)
  90. setTimeout(async () => {
  91. console.log('Renewing Token')
  92. await dispatch('fetchUser')
  93. }, expires * 1000)
  94. commit('user', user)
  95. }catch(err){
  96. commit('user', null)
  97. }
  98. },
  99. async logout({commit}){
  100. await Auth.signOut()
  101. commit('user', null)
  102. },
  103. }
  104.  
  105. // Getters ====================
  106.  
  107. store.getters = {
  108. }
  109.  
  110. // Export =====================
  111.  
  112. export default store
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement