Advertisement
TheRightGuy

This is how it looks like

Nov 24th, 2022
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Store.js
  2. import { applyMiddleware } from 'redux';
  3. import { configureStore } from '@reduxjs/toolkit'
  4. import { composeWithDevTools } from 'redux-devtools-extension';
  5. import thunk from 'redux-thunk';
  6. import rootReducer from './reducers';
  7.  
  8. const initialState = {};
  9.  
  10. const middleware = [thunk];
  11.  
  12. const store = configureStore({
  13.     reducer: {
  14.         authReducer: rootReducer,
  15.         starterState : initialState,
  16.         devTools: composeWithDevTools(applyMiddleware(...middleware)),
  17.     }
  18. });
  19.  
  20. export default store;
  21.  
  22. index.js (rootReducer)
  23.  
  24. import { combineReducers } from 'redux';
  25. import auth from './auth';
  26.  
  27. export default combineReducers({
  28.     auth
  29. });
  30.  
  31. auth.js
  32.  
  33. import {
  34.     LOGIN_SUCCESS,
  35.     LOGIN_FAIL,
  36.     USER_LOADED_SUCCESS,
  37.     USER_LOADED_FAIL,
  38.     AUTHENTICATED_SUCCESS,
  39.     AUTHENTICATED_FAIL,
  40.     PASSWORD_RESET_SUCCESS,
  41.     PASSWORD_RESET_FAIL,
  42.     PASSWORD_RESET_CONFIRM_SUCCESS,
  43.     PASSWORD_RESET_CONFIRM_FAIL,
  44.     SIGNUP_SUCCESS,
  45.     SIGNUP_FAIL,
  46.     ACTIVATION_SUCCESS,
  47.     ACTIVATION_FAIL,
  48.     LOGOUT
  49. } from '../actions/types';
  50.  
  51. const initialState = {
  52.     access: localStorage.getItem('access'),
  53.     refresh: localStorage.getItem('refresh'),
  54.     isAuthenticated: null,
  55.     user: null
  56. };
  57.  
  58. export default function (state = initialState, action) {
  59.     const { type, payload } = action;
  60.  
  61.     switch (type) {
  62.         case AUTHENTICATED_SUCCESS:
  63.             return {
  64.                 ...state,
  65.                 isAuthenticated: true
  66.             }
  67.         case LOGIN_SUCCESS:
  68.             localStorage.setItem('access', payload.access);
  69.             localStorage.setItem('refresh', payload.refresh);
  70.             return {
  71.                 ...state,
  72.                 isAuthenticated: true,
  73.                 access: payload.access,
  74.                 refresh: payload.refresh
  75.             }
  76.         case SIGNUP_SUCCESS:
  77.             return {
  78.                 ...state,
  79.                 isAuthenticated: false
  80.             }
  81.         case USER_LOADED_SUCCESS:
  82.             return {
  83.                 ...state,
  84.                 user: payload
  85.             }
  86.         case AUTHENTICATED_FAIL:
  87.             return {
  88.                 ...state,
  89.                 isAuthenticated: false
  90.             }
  91.         case USER_LOADED_FAIL:
  92.             return {
  93.                 ...state,
  94.                 user: null
  95.             }
  96.         case LOGIN_FAIL:
  97.         case SIGNUP_FAIL:
  98.         case LOGOUT:
  99.             localStorage.removeItem('access');
  100.             localStorage.removeItem('refresh');
  101.             return {
  102.                 ...state,
  103.                 access: null,
  104.                 refresh: null,
  105.                 isAuthenticated: false,
  106.                 user: null
  107.             }
  108.         case PASSWORD_RESET_SUCCESS:
  109.         case PASSWORD_RESET_FAIL:
  110.         case PASSWORD_RESET_CONFIRM_SUCCESS:
  111.         case PASSWORD_RESET_CONFIRM_FAIL:
  112.         case ACTIVATION_SUCCESS:
  113.         case ACTIVATION_FAIL:
  114.             return {
  115.                 ...state
  116.             }
  117.         default:
  118.             return state
  119.     }
  120. };
  121.  
  122. Navbar,js
  123.  
  124. import React, { Fragment, useState } from 'react';
  125. import { Link, Navigate } from 'react-router-dom';
  126. import { connect } from 'react-redux';
  127. import { logout } from '../actions/auth';
  128.  
  129. const Navbar = ({ logout, isAuthenticated }) => {
  130.     const [redirect, setRedirect] = useState(false);
  131.  
  132.     const logout_user = () => {
  133.         logout();
  134.         setRedirect(true);
  135.     };
  136.  
  137.     const guestLinks = () => (
  138.         <Fragment>
  139.             ...        
  140.         </Fragment>
  141.     );
  142.  
  143.     const authLinks = () => (
  144.         ...
  145.     );
  146.  
  147.     return (
  148.         <Fragment>
  149.             <...
  150.                         {isAuthenticated ? authLinks() : guestLinks()}
  151. ...
  152.         </Fragment>
  153.     );
  154. };
  155.  
  156. const mapStateToProps = state => ({
  157.     isAuthenticated: state.auth.isAuthenticated
  158. });
  159.  
  160. export default connect(mapStateToProps, { logout })(Navbar);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement