auth.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 { data: { email } } = await axios.post('/api/auth/signup', payload);
  34. dispatch(sentVerification(email));
  35. } catch ({ response }) {
  36. dispatch(showAuthError(response.data.error));
  37. }
  38. };
  39. export const loginUser = payload => async dispatch => {
  40. dispatch(showLoginLoading());
  41. try {
  42. const { data: { token } } = await axios.post('/api/auth/login', payload);
  43. cookie.set('token', token, { expires: 7 });
  44. dispatch(authRenew());
  45. dispatch(authUser(decodeJwt(token)));
  46. dispatch(setDomain({ customDomain: decodeJwt(token).domain }));
  47. dispatch(showPageLoading());
  48. Router.push('/');
  49. } catch ({ response }) {
  50. dispatch(showAuthError(response.data.error));
  51. }
  52. };
  53. export const logoutUser = () => dispatch => {
  54. dispatch(showPageLoading());
  55. cookie.remove('token');
  56. dispatch(unauthUser());
  57. Router.push('/login');
  58. };
  59. export const renewAuthUser = () => async (dispatch, getState) => {
  60. if (getState().auth.renew) {
  61. return;
  62. }
  63. const options = {
  64. method: 'POST',
  65. headers: { Authorization: cookie.get('token') },
  66. url: '/api/auth/renew',
  67. };
  68. try {
  69. const { data: { token } } = await axios(options);
  70. cookie.set('token', token, { expires: 7 });
  71. dispatch(authRenew());
  72. dispatch(authUser(decodeJwt(token)));
  73. dispatch(setDomain({ customDomain: decodeJwt(token).domain }));
  74. } catch (error) {
  75. cookie.remove('token');
  76. dispatch(unauthUser());
  77. }
  78. };