├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt └── src ├── App.css ├── App.js ├── App.test.js ├── Home.js ├── app └── store.js ├── features └── userSlice.js ├── firebase.js ├── img ├── facebook.png ├── github.png ├── google.png └── twitter.png ├── index.css ├── index.js ├── logo.svg ├── serviceWorker.js └── setupTests.js /.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 was bootstrapped with [Create React App](https://github.com/facebook/create-react-app), using the [Redux](https://redux.js.org/) and [Redux Toolkit](https://redux-toolkit.js.org/) template. 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `npm run build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `npm run eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "auth-combo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@reduxjs/toolkit": "^1.6.0", 7 | "@testing-library/jest-dom": "^4.2.4", 8 | "@testing-library/react": "^9.5.0", 9 | "@testing-library/user-event": "^7.2.1", 10 | "firebase": "^8.6.8", 11 | "react": "^17.0.2", 12 | "react-dom": "^17.0.2", 13 | "react-redux": "^7.2.4", 14 | "react-scripts": "4.0.3" 15 | }, 16 | "scripts": { 17 | "start": "react-scripts start", 18 | "build": "react-scripts build", 19 | "test": "react-scripts test", 20 | "eject": "react-scripts eject" 21 | }, 22 | "eslintConfig": { 23 | "extends": "react-app" 24 | }, 25 | "browserslist": { 26 | "production": [ 27 | ">0.2%", 28 | "not dead", 29 | "not op_mini all" 30 | ], 31 | "development": [ 32 | "last 1 chrome version", 33 | "last 1 firefox version", 34 | "last 1 safari version" 35 | ] 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React Redux App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/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 | -------------------------------------------------------------------------------- /src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | height: 100vh; 4 | justify-content: center; 5 | box-sizing: border-box; 6 | display: flex; 7 | align-items: center; 8 | } 9 | 10 | .container { 11 | display: flex; 12 | align-items: center; 13 | margin: 10px; 14 | padding: 50px; 15 | /* border: 2px solid #ddd; */ 16 | border-radius: 10px; 17 | background-color: #020120; 18 | color: #ddd; 19 | box-shadow: -8px 8px 8px -8px #020120; 20 | } 21 | 22 | .container-heading { 23 | display: flex; 24 | 25 | flex-direction: column; 26 | } 27 | 28 | .container-heading > h1 { 29 | font-weight: 500; 30 | font-size: 1.9rem; 31 | align-items: center; 32 | } 33 | 34 | .container-heading > h1 > img { 35 | width: 60px; 36 | object-fit: contain; 37 | margin: 10px; 38 | padding: 10px; 39 | } 40 | 41 | .react { 42 | animation: spin 3s infinite linear; 43 | } 44 | 45 | .react1 { 46 | animation: heartbeat 1s infinite linear; 47 | } 48 | 49 | .react2 { 50 | animation: animate infinite 2s linear; 51 | transform: translate3d(0, 0, 0); 52 | } 53 | 54 | .content { 55 | display: flex; 56 | align-items: center; 57 | flex-direction: column; 58 | } 59 | 60 | .content > h3 { 61 | font-size: 1.5rem; 62 | margin-top: 0px; 63 | font-weight: 400; 64 | text-decoration: underline; 65 | } 66 | 67 | .content > input { 68 | padding: 10px; 69 | width: 100%; 70 | font-size: 1rem; 71 | margin-bottom: 10px; 72 | outline: none; 73 | border: 1px solid rgb(65, 65, 65); 74 | background-color: transparent; 75 | border-radius: 5px; 76 | color: #fff; 77 | } 78 | 79 | .content > input:focus { 80 | border: 2px solid aqua; 81 | } 82 | 83 | .content > button { 84 | width: 40%; 85 | padding: 10px; 86 | font-size: 1.1rem; 87 | margin-top: 20px; 88 | background-color: transparent; 89 | color: #ddd; 90 | border: 1px solid rgb(65, 65, 65); 91 | border-radius: 5px; 92 | cursor: pointer; 93 | } 94 | 95 | .content > button:focus { 96 | border: 1px solid aqua; 97 | } 98 | 99 | .content > button:hover { 100 | border: 1px solid #ddd; 101 | color: aqua; 102 | } 103 | 104 | .content > p { 105 | color: rgb(146, 146, 146); 106 | } 107 | 108 | .content > p > span { 109 | color: #ddd; 110 | cursor: pointer; 111 | } 112 | .content > p > span:hover { 113 | color: aqua; 114 | text-decoration: underline; 115 | } 116 | 117 | .providers { 118 | display: flex; 119 | margin: 12px 0px; 120 | } 121 | 122 | .provider { 123 | margin: 10px; 124 | display: flex; 125 | padding: 10px; 126 | background-color: #ddd; 127 | border-radius: 50%; 128 | box-shadow: 3px 3px 3px 3px #000; 129 | cursor: pointer; 130 | transition: transform 0.5s ease; 131 | } 132 | 133 | .provider:hover { 134 | transform: scale(1.1); 135 | background-color: #fff; 136 | } 137 | 138 | @keyframes spin { 139 | 0% { 140 | transform: rotate(0deg); 141 | } 142 | 50% { 143 | transform: rotate(180deg); 144 | } 145 | 100% { 146 | transform: rotate(360deg); 147 | } 148 | } 149 | 150 | @keyframes heartbeat { 151 | 0% { 152 | transform: scale(1); 153 | } 154 | 155 | 20% { 156 | transform: scale(1.25) translateX(5%) translateY(5%); 157 | } 158 | 159 | 40% { 160 | transform: scale(1.5) translateX(9%) translateY(10%); 161 | } 162 | } 163 | 164 | @keyframes animate { 165 | 10%, 166 | 90% { 167 | transform: translate3d(-1px, 0, 0); 168 | } 169 | 170 | 20%, 171 | 80% { 172 | transform: translate3d(2px, 0, 0); 173 | } 174 | 175 | 30%, 176 | 50%, 177 | 70% { 178 | transform: translate3d(-4px, 0, 0); 179 | } 180 | 181 | 40%, 182 | 60% { 183 | transform: translate3d(4px, 0, 0); 184 | } 185 | } 186 | 187 | .info { 188 | display: flex; 189 | flex-direction: row; 190 | align-items: center; 191 | } 192 | 193 | .info > img { 194 | width: 80px; 195 | object-fit: contain; 196 | } 197 | 198 | .info > span { 199 | margin-left: 20px; 200 | text-decoration: underline; 201 | color: aqua; 202 | } 203 | -------------------------------------------------------------------------------- /src/App.js: -------------------------------------------------------------------------------- 1 | import React, { useEffect, useState } from "react"; 2 | import { useDispatch, useSelector } from "react-redux"; 3 | import "./App.css"; 4 | import { login, logout, selectUser } from "./features/userSlice"; 5 | import { auth, facebookProvider, googleProvider } from "./firebase"; 6 | import Home from "./Home"; 7 | import google from "./img/google.png"; 8 | import facebook from "./img/facebook.png"; 9 | import github from "./img/github.png"; 10 | import twitter from "./img/twitter.png"; 11 | 12 | function App() { 13 | const user = useSelector(selectUser); 14 | const dispatch = useDispatch(); 15 | const [isRegister, setIsRegister] = useState(false); 16 | const [email, setEmail] = useState(""); 17 | const [password, setPassword] = useState(""); 18 | const [isLoginState, setIsLoginState] = useState("login"); 19 | 20 | const handleSubmit = (e) => { 21 | e.preventDefault(); 22 | isRegister ? handleLogin() : handleRegister(); 23 | }; 24 | 25 | useEffect(() => { 26 | auth.onAuthStateChanged((authUser) => { 27 | if (authUser) { 28 | dispatch( 29 | login({ 30 | id: authUser.uid, 31 | name: authUser.displayName ? authUser.displayName : authUser.email, 32 | lastsignIn: authUser.metadata.lastSignInTime, 33 | verified: String(authUser.emailVerified), 34 | pic: authUser.photoURL 35 | ? authUser.photoURL 36 | : "https://lh3.googleusercontent.com/ogw/ADea4I5bHBJbpIvco4Yh1ARth7_gu4dl_QnpyDAU0NW8=s32-c-mo", 37 | }) 38 | ); 39 | } else { 40 | dispatch(logout()); 41 | } 42 | }); 43 | }, [dispatch]); 44 | 45 | const handleLogin = () => { 46 | if (email && password !== "") { 47 | auth 48 | .signInWithEmailAndPassword(email, password) 49 | .then((data) => alert("Logged in successfully!!!")) 50 | .catch((err) => alert(err)); 51 | } 52 | }; 53 | const handleRegister = () => { 54 | if (email && password !== "") { 55 | auth 56 | .createUserWithEmailAndPassword(email, password) 57 | .then((data) => alert("Registered Successfully")) 58 | .catch((err) => alert(err)); 59 | } 60 | }; 61 | 62 | const handleGoogle = () => { 63 | auth.signInWithPopup(googleProvider); 64 | }; 65 | const handleFacebook = () => { 66 | auth.signInWithPopup(facebookProvider); 67 | }; 68 | return ( 69 |
70 | {" "} 71 | {user ? ( 72 | 73 | ) : ( 74 | <> 75 |
76 |
77 |

78 | Ultimate Authentication using
79 | {" "} 84 | {" "} 89 | 94 |

95 |
96 |

{isRegister ? "Login" : "Register"}

97 | setEmail(e.target.value)} 100 | type="text" 101 | required={true} 102 | placeholder="Enter your email" 103 | /> 104 | setPassword(e.target.value)} 107 | type="password" 108 | required={true} 109 | placeholder="Enter your password" 110 | /> 111 | 114 | {isLoginState === "login" && ( 115 | <> 116 |
117 |
118 | google 119 |
120 |
121 | google 122 |
123 |
124 | google 125 |
126 |
127 | google 128 |
129 |
130 | 131 | )} 132 |

133 | {isRegister ? "New member? " : "Already registered? "} 134 | 135 | setIsRegister((show) => !show)}> 136 | {isRegister ? "Register" : "Login"} 137 | 138 |

139 |
140 |
141 |
142 | 143 | )} 144 |
145 | ); 146 | } 147 | 148 | export default App; 149 | -------------------------------------------------------------------------------- /src/App.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import { Provider } from 'react-redux'; 4 | import { store } from './app/store'; 5 | import App from './App'; 6 | 7 | test('renders learn react link', () => { 8 | const { getByText } = render( 9 | 10 | 11 | 12 | ); 13 | 14 | expect(getByText(/learn/i)).toBeInTheDocument(); 15 | }); 16 | -------------------------------------------------------------------------------- /src/Home.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import { useSelector } from "react-redux"; 3 | import { selectUser } from "./features/userSlice"; 4 | import { auth } from "./firebase"; 5 | 6 | function Home() { 7 | const user = useSelector(selectUser); 8 | const handleLogout = () => { 9 | if (window.confirm("Wanna break up with us :(")) { 10 | auth.signOut(); 11 | } 12 | }; 13 | return ( 14 |
15 |
16 |

Securely logged in

17 |
18 | 19 |
20 |
21 |

Id:

22 | {user.id} 23 |
24 |
25 |

Name:

{String(user.name).split("@")[0]} 26 |
27 |
28 |

Verified:

{user.verified} 29 |
30 | 31 | 32 |
33 |

Last login : {user.lastsignIn}

34 |
35 |
36 |
37 | ); 38 | } 39 | 40 | export default Home; 41 | -------------------------------------------------------------------------------- /src/app/store.js: -------------------------------------------------------------------------------- 1 | import { configureStore } from "@reduxjs/toolkit"; 2 | import userReducer from "../features/userSlice"; 3 | 4 | export const store = configureStore({ 5 | reducer: { 6 | user: userReducer, 7 | }, 8 | }); 9 | -------------------------------------------------------------------------------- /src/features/userSlice.js: -------------------------------------------------------------------------------- 1 | import { createSlice } from "@reduxjs/toolkit"; 2 | 3 | const initialState = { 4 | user: null, 5 | }; 6 | 7 | export const userSlice = createSlice({ 8 | name: "user", 9 | initialState, 10 | // The `reducers` field lets us define reducers and generate associated actions 11 | reducers: { 12 | login: (state, action) => { 13 | state.user = action.payload; 14 | }, 15 | logout: (state) => { 16 | state.user = null; 17 | }, 18 | }, 19 | }); 20 | 21 | export const { login, logout } = userSlice.actions; 22 | 23 | // The function below is called a selector and allows us to select a value from 24 | // the state. Selectors can also be defined inline where they're used instead of 25 | // in the slice file. For example: `useSelector((state: RootState) => state.counter.value)` 26 | export const selectUser = (state) => state.user.user; 27 | 28 | // We can also write thunks by hand, which may contain both sync and async logic. 29 | // Here's an example of conditionally dispatching actions based on current state. 30 | 31 | export default userSlice.reducer; 32 | -------------------------------------------------------------------------------- /src/firebase.js: -------------------------------------------------------------------------------- 1 | import firebase from "firebase"; 2 | 3 | // For Firebase JS SDK v7.20.0 and later, measurementId is optional 4 | const firebaseConfig = { 5 | apiKey: "AIzaSyC8UHMG2PROL8H3x-uU1bU1Mp2DQn0uiEw", 6 | authDomain: "auth-746a5.firebaseapp.com", 7 | projectId: "auth-746a5", 8 | storageBucket: "auth-746a5.appspot.com", 9 | messagingSenderId: "1013640274798", 10 | appId: "1:1013640274798:web:d362bde766c1e3e7d4d77f", 11 | measurementId: "G-5639MW0HFN", 12 | }; 13 | 14 | const firebaseapp = firebase.initializeApp(firebaseConfig); 15 | const auth = firebase.auth(); 16 | const googleProvider = new firebase.auth.GoogleAuthProvider(); 17 | const facebookProvider = new firebase.auth.FacebookAuthProvider(); 18 | 19 | export { auth, googleProvider, facebookProvider }; 20 | -------------------------------------------------------------------------------- /src/img/facebook.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/src/img/facebook.png -------------------------------------------------------------------------------- /src/img/github.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/src/img/github.png -------------------------------------------------------------------------------- /src/img/google.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/src/img/google.png -------------------------------------------------------------------------------- /src/img/twitter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akkySrivastava/firebase-auth-tutorial/5e796ddc79ae0722199cdfb52842236f86e3ecbf/src/img/twitter.png -------------------------------------------------------------------------------- /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 React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import { store } from './app/store'; 6 | import { Provider } from 'react-redux'; 7 | import * as serviceWorker from './serviceWorker'; 8 | 9 | ReactDOM.render( 10 | 11 | 12 | 13 | 14 | , 15 | document.getElementById('root') 16 | ); 17 | 18 | // If you want your app to work offline and load faster, you can change 19 | // unregister() to register() below. Note this comes with some pitfalls. 20 | // Learn more about service workers: https://bit.ly/CRA-PWA 21 | serviceWorker.unregister(); 22 | -------------------------------------------------------------------------------- /src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/serviceWorker.js: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | export function register(config) { 24 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 25 | // The URL constructor is available in all browsers that support SW. 26 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href); 27 | if (publicUrl.origin !== window.location.origin) { 28 | // Our service worker won't work if PUBLIC_URL is on a different origin 29 | // from what our page is served on. This might happen if a CDN is used to 30 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 31 | return; 32 | } 33 | 34 | window.addEventListener('load', () => { 35 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 36 | 37 | if (isLocalhost) { 38 | // This is running on localhost. Let's check if a service worker still exists or not. 39 | checkValidServiceWorker(swUrl, config); 40 | 41 | // Add some additional logging to localhost, pointing developers to the 42 | // service worker/PWA documentation. 43 | navigator.serviceWorker.ready.then(() => { 44 | console.log( 45 | 'This web app is being served cache-first by a service ' + 46 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 47 | ); 48 | }); 49 | } else { 50 | // Is not localhost. Just register service worker 51 | registerValidSW(swUrl, config); 52 | } 53 | }); 54 | } 55 | } 56 | 57 | function registerValidSW(swUrl, config) { 58 | navigator.serviceWorker 59 | .register(swUrl) 60 | .then((registration) => { 61 | registration.onupdatefound = () => { 62 | const installingWorker = registration.installing; 63 | if (installingWorker == null) { 64 | return; 65 | } 66 | installingWorker.onstatechange = () => { 67 | if (installingWorker.state === 'installed') { 68 | if (navigator.serviceWorker.controller) { 69 | // At this point, the updated precached content has been fetched, 70 | // but the previous service worker will still serve the older 71 | // content until all client tabs are closed. 72 | console.log( 73 | 'New content is available and will be used when all ' + 74 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 75 | ); 76 | 77 | // Execute callback 78 | if (config && config.onUpdate) { 79 | config.onUpdate(registration); 80 | } 81 | } else { 82 | // At this point, everything has been precached. 83 | // It's the perfect time to display a 84 | // "Content is cached for offline use." message. 85 | console.log('Content is cached for offline use.'); 86 | 87 | // Execute callback 88 | if (config && config.onSuccess) { 89 | config.onSuccess(registration); 90 | } 91 | } 92 | } 93 | }; 94 | }; 95 | }) 96 | .catch((error) => { 97 | console.error('Error during service worker registration:', error); 98 | }); 99 | } 100 | 101 | function checkValidServiceWorker(swUrl, config) { 102 | // Check if the service worker can be found. If it can't reload the page. 103 | fetch(swUrl, { 104 | headers: { 'Service-Worker': 'script' }, 105 | }) 106 | .then((response) => { 107 | // Ensure service worker exists, and that we really are getting a JS file. 108 | const contentType = response.headers.get('content-type'); 109 | if ( 110 | response.status === 404 || 111 | (contentType != null && contentType.indexOf('javascript') === -1) 112 | ) { 113 | // No service worker found. Probably a different app. Reload the page. 114 | navigator.serviceWorker.ready.then((registration) => { 115 | registration.unregister().then(() => { 116 | window.location.reload(); 117 | }); 118 | }); 119 | } else { 120 | // Service worker found. Proceed as normal. 121 | registerValidSW(swUrl, config); 122 | } 123 | }) 124 | .catch(() => { 125 | console.log( 126 | 'No internet connection found. App is running in offline mode.' 127 | ); 128 | }); 129 | } 130 | 131 | export function unregister() { 132 | if ('serviceWorker' in navigator) { 133 | navigator.serviceWorker.ready.then((registration) => { 134 | registration.unregister(); 135 | }); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/setupTests.js: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | --------------------------------------------------------------------------------