├── .github └── workflows │ └── auto-squash-deps.yml ├── .gitignore ├── README.md ├── db.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.css ├── App.js ├── components │ ├── LogIn.js │ └── Register.js ├── index.css ├── index.js └── store │ ├── auth │ ├── authActionTypes.js │ ├── authActions.js │ ├── authReducer.js │ └── authSagas.js │ ├── index.js │ ├── rootReducer.js │ └── rootSaga.js └── yarn.lock /.github/workflows/auto-squash-deps.yml: -------------------------------------------------------------------------------- 1 | name: automerge on pull request 2 | on: [pull_request] 3 | 4 | jobs: 5 | automerge: 6 | name: automerge dependabot 7 | runs-on: ubuntu-latest 8 | if: github.actor == 'dependabot[bot]' 9 | steps: 10 | - name: merge 11 | uses: actions/github-script@0.2.0 12 | with: 13 | script: | 14 | github.pullRequests.createReview({ 15 | owner: context.payload.repository.owner.login, 16 | repo: context.payload.repository.name, 17 | pull_number: context.payload.pull_request.number, 18 | event: 'APPROVE' 19 | }) 20 | github.pullRequests.merge({ 21 | owner: context.payload.repository.owner.login, 22 | repo: context.payload.repository.name, 23 | pull_number: context.payload.pull_request.number, 24 | merge_method: 'squash' 25 | }) 26 | github-token: ${{github.token}} 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This project is a simple example using redux and redux-saga to handle authentication in a react app. 2 | 3 | You can find a complete explanation of it on my blog @ [https://praz.dev/react-auth-redux-saga](https://praz.dev/react-auth-redux-saga) 4 | -------------------------------------------------------------------------------- /db.json: -------------------------------------------------------------------------------- 1 | { 2 | "users": [ 3 | { 4 | "email": "test@test.com", 5 | "password": "$2a$10$rVFXp08eQpgLxDI1e1rojudJvmllRR8bbOugg.G2zp531Fq5Gt4XS", 6 | "id": 1 7 | } 8 | ] 9 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "redux-sagas-auth", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.11.9", 7 | "@testing-library/react": "^11.2.5", 8 | "@testing-library/user-event": "^12.8.1", 9 | "axios": "^0.21.1", 10 | "react": "^17.0.1", 11 | "react-dom": "^17.0.1", 12 | "react-redux": "^7.2.2", 13 | "react-scripts": "4.0.3", 14 | "redux": "^4.0.5", 15 | "redux-logger": "^3.0.6", 16 | "redux-saga": "^1.1.3" 17 | }, 18 | "scripts": { 19 | "server": "json-server-auth db.json --port 3001", 20 | "start": "react-scripts start", 21 | "build": "react-scripts build", 22 | "test": "react-scripts test", 23 | "eject": "react-scripts eject" 24 | }, 25 | "proxy": "http://localhost:3001", 26 | "eslintConfig": { 27 | "extends": "react-app" 28 | }, 29 | "browserslist": { 30 | "production": [ 31 | ">0.2%", 32 | "not dead", 33 | "not op_mini all" 34 | ], 35 | "development": [ 36 | "last 1 chrome version", 37 | "last 1 firefox version", 38 | "last 1 safari version" 39 | ] 40 | }, 41 | "devDependencies": { 42 | "json-server": "^0.16.3", 43 | "json-server-auth": "^2.0.2" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Seven-33/react-auth-redux-saga/7b2a6feb02fd53cad03df4910027726a6db13bd2/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Seven-33/react-auth-redux-saga/7b2a6feb02fd53cad03df4910027726a6db13bd2/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Seven-33/react-auth-redux-saga/7b2a6feb02fd53cad03df4910027726a6db13bd2/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | } 4 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import './App.css'; 2 | 3 | import React from 'react'; 4 | import { useDispatch, useSelector } from 'react-redux'; 5 | 6 | import LogIn from './components/LogIn'; 7 | import Register from './components/Register'; 8 | import { logOut } from './store/auth/authActions'; 9 | 10 | function App() { 11 | const auth = useSelector((state) => state.auth); 12 | const dispatch = useDispatch(); 13 | 14 | return ( 15 |
16 | {auth.currentUser ? ( 17 | <> 18 |
Connected user with token {auth.currentUser.token}
19 | 20 | 21 | ) : ( 22 | <> 23 | 24 | 25 | {auth.error ? {auth.error?.response.data} : null} 26 | 27 | )} 28 |
29 | ); 30 | } 31 | 32 | export default App; 33 | -------------------------------------------------------------------------------- /src/components/LogIn.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { useDispatch } from 'react-redux'; 3 | 4 | import { logInStart } from '../store/auth/authActions'; 5 | 6 | const LogIn = () => { 7 | const [credentials, setCredentials] = useState({ email: "", password: "" }); 8 | 9 | const dispatch = useDispatch(); 10 | 11 | const handleChange = (e) => 12 | setCredentials({ ...credentials, [e.target.name]: e.target.value }); 13 | 14 | const handleSubmit = (e) => { 15 | e.preventDefault(); 16 | dispatch(logInStart(credentials)); 17 | }; 18 | 19 | return ( 20 |
21 |

LOGIN

22 | 31 | 40 | 41 |
42 | ); 43 | }; 44 | 45 | export default LogIn; 46 | -------------------------------------------------------------------------------- /src/components/Register.js: -------------------------------------------------------------------------------- 1 | import React, { useState } from 'react'; 2 | import { useDispatch } from 'react-redux'; 3 | 4 | import { registerStart } from '../store/auth/authActions'; 5 | 6 | const Register = () => { 7 | const [credentials, setCredentials] = useState({ email: "", password: "" }); 8 | 9 | const dispatch = useDispatch(); 10 | 11 | const handleChange = (e) => 12 | setCredentials({ ...credentials, [e.target.name]: e.target.value }); 13 | 14 | const handleSubmit = (e) => { 15 | e.preventDefault(); 16 | dispatch(registerStart(credentials)); 17 | }; 18 | 19 | return ( 20 |
21 |

REGISTER

22 | 31 | 40 | 41 |
42 | ); 43 | }; 44 | 45 | export default Register; 46 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import './index.css'; 2 | 3 | import React from 'react'; 4 | import ReactDOM from 'react-dom'; 5 | import { Provider } from 'react-redux'; 6 | 7 | import App from './App'; 8 | import store from './store'; 9 | 10 | ReactDOM.render( 11 | 12 | 13 | 14 | 15 | , 16 | document.getElementById("root") 17 | ); 18 | -------------------------------------------------------------------------------- /src/store/auth/authActionTypes.js: -------------------------------------------------------------------------------- 1 | const userActionTypes = { 2 | LOG_IN_START: "LOG_IN_START", 3 | LOG_IN_SUCCESS: "LOG_IN_SUCCESS", 4 | LOG_IN_FAILURE: "LOG_IN_FAILURE", 5 | REGISTER_START: "REGISTER_START", 6 | REGISTER_SUCCESS: "REGISTER_SUCCESS", 7 | REGISTER_FAILURE: "REGISTER_FAILURE", 8 | LOG_OUT: "LOG_OUT", 9 | }; 10 | 11 | export default userActionTypes; 12 | -------------------------------------------------------------------------------- /src/store/auth/authActions.js: -------------------------------------------------------------------------------- 1 | import types from './authActionTypes'; 2 | 3 | export const logInStart = (credentials) => ({ 4 | type: types.LOG_IN_START, 5 | payload: credentials, 6 | }); 7 | 8 | export const logInSuccess = (user) => ({ 9 | type: types.LOG_IN_SUCCESS, 10 | payload: user, 11 | }); 12 | 13 | export const logInFailure = (error) => ({ 14 | type: types.LOG_IN_FAILURE, 15 | payload: error, 16 | }); 17 | 18 | export const registerStart = (credentials) => ({ 19 | type: types.REGISTER_START, 20 | payload: credentials, 21 | }); 22 | 23 | export const registerSuccess = (user) => ({ 24 | type: types.REGISTER_SUCCESS, 25 | payload: user, 26 | }); 27 | 28 | export const registerFailure = (error) => ({ 29 | type: types.REGISTER_FAILURE, 30 | payload: error, 31 | }); 32 | 33 | export const logOut = () => ({ 34 | type: types.LOG_OUT, 35 | }); 36 | -------------------------------------------------------------------------------- /src/store/auth/authReducer.js: -------------------------------------------------------------------------------- 1 | import types from './authActionTypes'; 2 | 3 | const INITIAL_STATE = { 4 | currentUser: null, 5 | error: null, 6 | }; 7 | 8 | const authReducer = (state = INITIAL_STATE, action) => { 9 | switch (action.type) { 10 | case types.LOG_IN_SUCCESS: 11 | return { 12 | ...state, 13 | currentUser: action.payload, 14 | error: null, 15 | }; 16 | case types.LOG_IN_FAILURE: 17 | case types.REGISTER_FAILURE: 18 | return { 19 | ...state, 20 | error: action.payload, 21 | }; 22 | case types.LOG_OUT: 23 | return INITIAL_STATE; 24 | default: 25 | return state; 26 | } 27 | }; 28 | 29 | export default authReducer; 30 | -------------------------------------------------------------------------------- /src/store/auth/authSagas.js: -------------------------------------------------------------------------------- 1 | import axios from 'axios'; 2 | import { all, call, put, takeLatest } from 'redux-saga/effects'; 3 | 4 | import { logInFailure, logInSuccess, registerFailure, registerSuccess } from './authActions'; 5 | import types from './authActionTypes'; 6 | 7 | const logIn = async (email, password) => { 8 | const response = await axios.post("/login", { 9 | email, 10 | password, 11 | }); 12 | return { token: response.data.accessToken }; 13 | }; 14 | 15 | const register = async (email, password) => { 16 | await axios.post("/register", { 17 | email, 18 | password, 19 | }); 20 | }; 21 | 22 | export function* logInWithCredentials({ payload: { email, password } }) { 23 | try { 24 | const user = yield logIn(email, password); 25 | yield put(logInSuccess(user)); 26 | } catch (error) { 27 | yield put(logInFailure(error)); 28 | } 29 | } 30 | 31 | export function* registerWithCredentials({ payload: { email, password } }) { 32 | try { 33 | yield register(email, password); 34 | yield put(registerSuccess({ email, password })); 35 | } catch (error) { 36 | yield put(registerFailure(error)); 37 | } 38 | } 39 | 40 | export function* logInAfterRegister({ payload: { email, password } }) { 41 | yield logInWithCredentials({ payload: { email, password } }); 42 | } 43 | 44 | export function* onLogInStart() { 45 | yield takeLatest(types.LOG_IN_START, logInWithCredentials); 46 | } 47 | 48 | export function* onRegisterStart() { 49 | yield takeLatest(types.REGISTER_START, registerWithCredentials); 50 | } 51 | 52 | export function* onRegisterSuccess() { 53 | yield takeLatest(types.REGISTER_SUCCESS, logInAfterRegister); 54 | } 55 | 56 | export function* authSagas() { 57 | yield all([ 58 | call(onLogInStart), 59 | call(onRegisterStart), 60 | call(onRegisterSuccess), 61 | ]); 62 | } 63 | -------------------------------------------------------------------------------- /src/store/index.js: -------------------------------------------------------------------------------- 1 | import { applyMiddleware, createStore } from 'redux'; 2 | import logger from 'redux-logger'; 3 | import createSagaMiddleware from 'redux-saga'; 4 | 5 | import rootReducer from './rootReducer'; 6 | import rootSaga from './rootSaga'; 7 | 8 | const sagaMiddleware = createSagaMiddleware(); 9 | 10 | const middlewares = [sagaMiddleware, logger]; 11 | 12 | const store = createStore(rootReducer, applyMiddleware(...middlewares)); 13 | 14 | sagaMiddleware.run(rootSaga); 15 | 16 | export default store; 17 | -------------------------------------------------------------------------------- /src/store/rootReducer.js: -------------------------------------------------------------------------------- 1 | import { combineReducers } from 'redux'; 2 | 3 | import auth from './auth/authReducer'; 4 | 5 | export default combineReducers({ auth }); 6 | -------------------------------------------------------------------------------- /src/store/rootSaga.js: -------------------------------------------------------------------------------- 1 | import { all, call } from 'redux-saga/effects'; 2 | 3 | import { authSagas } from './auth/authSagas'; 4 | 5 | export default function* rootSaga() { 6 | yield all([call(authSagas)]); 7 | } 8 | --------------------------------------------------------------------------------