auth.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import Router from 'next/router';
  2. import axios from 'axios';
  3. import cookie from 'js-cookie';
  4. import decodeJwt from 'jwt-decode';
  5. import {
  6. SET_DOMAIN,
  7. SHOW_PAGE_LOADING,
  8. HIDE_PAGE_LOADING,
  9. AUTH_USER,
  10. UNAUTH_USER,
  11. SENT_VERIFICATION,
  12. AUTH_ERROR,
  13. LOGIN_LOADING,
  14. SIGNUP_LOADING,
  15. AUTH_RENEW,
  16. } from './actionTypes';
  17. const setDomain = payload => ({ type: SET_DOMAIN, payload });
  18. export const showPageLoading = () => ({ type: SHOW_PAGE_LOADING });
  19. export const hidePageLoading = () => ({ type: HIDE_PAGE_LOADING });
  20. export const authUser = payload => ({ type: AUTH_USER, payload });
  21. export const unauthUser = () => ({ type: UNAUTH_USER });
  22. export const sentVerification = payload => ({
  23. type: SENT_VERIFICATION,
  24. payload,
  25. });
  26. export const showAuthError = payload => ({ type: AUTH_ERROR, payload });
  27. export const showLoginLoading = () => ({ type: LOGIN_LOADING });
  28. export const showSignupLoading = () => ({ type: SIGNUP_LOADING });
  29. export const authRenew = () => ({ type: AUTH_RENEW });
  30. export const signupUser = payload => async dispatch => {
  31. dispatch(showSignupLoading());
  32. try {
  33. const {
  34. data: { email },
  35. } = await axios.post('/api/auth/signup', payload);
  36. dispatch(sentVerification(email));
  37. } catch ({ response }) {
  38. dispatch(showAuthError(response.data.error));
  39. }
  40. };
  41. export const loginUser = payload => async dispatch => {
  42. dispatch(showLoginLoading());
  43. try {
  44. const {
  45. data: { token },
  46. } = await axios.post('/api/auth/login', payload);
  47. cookie.set('token', token, { expires: 7 });
  48. dispatch(authRenew());
  49. dispatch(authUser(decodeJwt(token)));
  50. dispatch(setDomain({ customDomain: decodeJwt(token).domain }));
  51. dispatch(showPageLoading());
  52. Router.push('/');
  53. } catch ({ response }) {
  54. dispatch(showAuthError(response.data.error));
  55. }
  56. };
  57. export const logoutUser = () => dispatch => {
  58. dispatch(showPageLoading());
  59. cookie.remove('token');
  60. dispatch(unauthUser());
  61. Router.push('/login');
  62. };
  63. export const renewAuthUser = () => async (dispatch, getState) => {
  64. if (getState().auth.renew) {
  65. return;
  66. }
  67. const options = {
  68. method: 'POST',
  69. headers: { Authorization: cookie.get('token') },
  70. url: '/api/auth/renew',
  71. };
  72. try {
  73. const {
  74. data: { token },
  75. } = await axios(options);
  76. cookie.set('token', token, { expires: 7 });
  77. dispatch(authRenew());
  78. dispatch(authUser(decodeJwt(token)));
  79. dispatch(setDomain({ customDomain: decodeJwt(token).domain }));
  80. } catch (error) {
  81. cookie.remove('token');
  82. dispatch(unauthUser());
  83. }
  84. };