[
  {
    "path": ".gitignore",
    "content": "# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.\n\n# dependencies\n/node_modules\n/.pnp\n.pnp.js\n\n# testing\n/coverage\n\n# production\n/build\n\n# misc\n.DS_Store\n.env.local\n.env.development.local\n.env.test.local\n.env.production.local\n\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n"
  },
  {
    "path": "LICENSE",
    "content": "MIT License\n\nCopyright (c) 2021 Sivanesh Shanmugam\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "FYI: This uses the old approach as shown in the live demo. But still functional. If you prefer for the new Google Auth then this might not be the best place for you. \n\n# React Google authentication demo\n[Live Demo](https://sivanesh-s.github.io/react-google-authentication/) \n\nRefer this [blog](https://dev.to/sivaneshs/add-google-login-to-your-react-apps-in-10-mins-4del) for documentation.\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"my-react-google-login\",\n  \"version\": \"0.1.0\",\n  \"private\": true,\n  \"dependencies\": {\n    \"@testing-library/jest-dom\": \"^4.2.4\",\n    \"@testing-library/react\": \"^9.3.2\",\n    \"@testing-library/user-event\": \"^7.1.2\",\n    \"react\": \"^16.13.1\",\n    \"react-dom\": \"^16.13.1\",\n    \"react-google-login\": \"^5.1.20\",\n    \"react-scripts\": \"3.4.1\"\n  },\n  \"scripts\": {\n    \"start\": \"react-scripts start\",\n    \"build\": \"react-scripts build\",\n    \"test\": \"react-scripts test\",\n    \"eject\": \"react-scripts eject\",\n    \"predeploy\": \"npm run build\",\n    \"deploy\": \"gh-pages -d build\"\n  },\n  \"eslintConfig\": {\n    \"extends\": \"react-app\"\n  },\n  \"browserslist\": {\n    \"production\": [\n      \">0.2%\",\n      \"not dead\",\n      \"not op_mini all\"\n    ],\n    \"development\": [\n      \"last 1 chrome version\",\n      \"last 1 firefox version\",\n      \"last 1 safari version\"\n    ]\n  },\n  \"homepage\": \"https://sivanesh-s.github.io/react-google-authentication\",\n  \"devDependencies\": {\n    \"gh-pages\": \"^3.1.0\"\n  }\n}\n"
  },
  {
    "path": "public/index.html",
    "content": "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <link rel=\"icon\" href=\"%PUBLIC_URL%/favicon.ico\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <meta name=\"theme-color\" content=\"#000000\" />\n    <meta\n      name=\"description\"\n      content=\"Web site created using create-react-app\"\n    />\n    <link rel=\"apple-touch-icon\" href=\"%PUBLIC_URL%/logo192.png\" />\n    <!--\n      manifest.json provides metadata used when your web app is installed on a\n      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/\n    -->\n    <link rel=\"manifest\" href=\"%PUBLIC_URL%/manifest.json\" />\n    <!--\n      Notice the use of %PUBLIC_URL% in the tags above.\n      It will be replaced with the URL of the `public` folder during the build.\n      Only files inside the `public` folder can be referenced from the HTML.\n\n      Unlike \"/favicon.ico\" or \"favicon.ico\", \"%PUBLIC_URL%/favicon.ico\" will\n      work correctly both with client-side routing and a non-root public URL.\n      Learn how to configure a non-root public URL by running `npm run build`.\n    -->\n    <title>React App</title>\n  </head>\n  <body>\n    <noscript>You need to enable JavaScript to run this app.</noscript>\n    <div id=\"root\"></div>\n    <!--\n      This HTML file is a template.\n      If you open it directly in the browser, you will see an empty page.\n\n      You can add webfonts, meta tags, or analytics to this file.\n      The build step will place the bundled scripts into the <body> tag.\n\n      To begin the development, run `npm start` or `yarn start`.\n      To create a production bundle, use `npm run build` or `yarn build`.\n    -->\n  </body>\n</html>\n"
  },
  {
    "path": "public/manifest.json",
    "content": "{\n  \"short_name\": \"React App\",\n  \"name\": \"Create React App Sample\",\n  \"icons\": [\n    {\n      \"src\": \"favicon.ico\",\n      \"sizes\": \"64x64 32x32 24x24 16x16\",\n      \"type\": \"image/x-icon\"\n    },\n    {\n      \"src\": \"logo192.png\",\n      \"type\": \"image/png\",\n      \"sizes\": \"192x192\"\n    },\n    {\n      \"src\": \"logo512.png\",\n      \"type\": \"image/png\",\n      \"sizes\": \"512x512\"\n    }\n  ],\n  \"start_url\": \".\",\n  \"display\": \"standalone\",\n  \"theme_color\": \"#000000\",\n  \"background_color\": \"#ffffff\"\n}\n"
  },
  {
    "path": "public/robots.txt",
    "content": "# https://www.robotstxt.org/robotstxt.html\nUser-agent: *\nDisallow:\n"
  },
  {
    "path": "src/App.css",
    "content": ".App {\n  text-align: center;\n  margin-top: 20px;\n}\n\n/* Hooks button */\n.button {\n  cursor: pointer;\n  display: block;\n  font-size: 1.3em;\n  box-sizing: content-box;\n  margin: 20px auto 0px;\n  width: 70%;\n  padding: 15px 20px;\n  border-radius: 24px;\n  border-color: transparent;\n  background-color: white;\n  /* box-shadow: 0px 16px 60px rgba(78, 79, 114, 0.1); */\n  box-shadow: 0px 16px 60px rgba(78, 79, 114, 0.08);\n  position: relative;\n}\n\n.buttonText {\n  color: #4285f4;\n  font-weight: bolder;\n}\n\n.icon {\n  height: 25px;\n  width: 25px;\n  margin-right: 0px;\n  position: absolute;\n  left: 30px;\n  align-items: center;\n}\n"
  },
  {
    "path": "src/App.js",
    "content": "import React from 'react';\nimport './App.css';\nimport Login from './components/Login';\nimport Logout from './components/Logout';\nimport LoginHooks from './components/LoginHooks';\nimport LogoutHooks from './components/LogoutHooks';\n\nfunction App() {\n  return (\n    <div className=\"App\">\n      <h2>The Components way</h2>\n      <Login />\n      <br />\n      <Logout />\n      <h2>The Hooks way</h2>\n      <LoginHooks />\n      <LogoutHooks />\n      <br />\n      If it does helped you feel free to star{' '}\n      <a href=\"https://github.com/Sivanesh-S/react-google-authentication\">\n        Github Repo\n      </a>{' '}\n      😉\n    </div>\n  );\n}\n\nexport default App;\n"
  },
  {
    "path": "src/components/Login.js",
    "content": "import React from 'react';\n\nimport { GoogleLogin } from 'react-google-login';\n// refresh token\nimport { refreshTokenSetup } from '../utils/refreshToken';\n\nconst clientId =\n  '707788443358-u05p46nssla3l8tmn58tpo9r5sommgks.apps.googleusercontent.com';\n\nfunction Login() {\n  const onSuccess = (res) => {\n    console.log('Login Success: currentUser:', res.profileObj);\n    alert(\n      `Logged in successfully welcome ${res.profileObj.name} 😍. \\n See console for full profile object.`\n    );\n    refreshTokenSetup(res);\n  };\n\n  const onFailure = (res) => {\n    console.log('Login failed: res:', res);\n    alert(\n      `Failed to login. 😢 Please ping this to repo owner twitter.com/sivanesh_fiz`\n    );\n  };\n\n  return (\n    <div>\n      <GoogleLogin\n        clientId={clientId}\n        buttonText=\"Login\"\n        onSuccess={onSuccess}\n        onFailure={onFailure}\n        cookiePolicy={'single_host_origin'}\n        style={{ marginTop: '100px' }}\n        isSignedIn={true}\n      />\n    </div>\n  );\n}\n\nexport default Login;\n"
  },
  {
    "path": "src/components/LoginHooks.js",
    "content": "import React from 'react';\nimport { useGoogleLogin } from 'react-google-login';\n\n// refresh token\nimport { refreshTokenSetup } from '../utils/refreshToken';\n\nconst clientId =\n  '707788443358-u05p46nssla3l8tmn58tpo9r5sommgks.apps.googleusercontent.com';\n\nfunction LoginHooks() {\n  const onSuccess = (res) => {\n    console.log('Login Success: currentUser:', res.profileObj);\n    alert(\n      `Logged in successfully welcome ${res.profileObj.name} 😍. \\n See console for full profile object.`\n    );\n    refreshTokenSetup(res);\n  };\n\n  const onFailure = (res) => {\n    console.log('Login failed: res:', res);\n    alert(\n      `Failed to login. 😢 Please ping this to repo owner twitter.com/sivanesh_fiz`\n    );\n  };\n\n  const { signIn } = useGoogleLogin({\n    onSuccess,\n    onFailure,\n    clientId,\n    isSignedIn: true,\n    accessType: 'offline',\n    // responseType: 'code',\n    // prompt: 'consent',\n  });\n\n  return (\n    <button onClick={signIn} className=\"button\">\n      <img src=\"icons/google.svg\" alt=\"google login\" className=\"icon\"></img>\n\n      <span className=\"buttonText\">Sign in with Google</span>\n    </button>\n  );\n}\n\nexport default LoginHooks;\n"
  },
  {
    "path": "src/components/Logout.js",
    "content": "import React from 'react';\nimport { GoogleLogout } from 'react-google-login';\n\nconst clientId =\n  '707788443358-u05p46nssla3l8tmn58tpo9r5sommgks.apps.googleusercontent.com';\n\nfunction Logout() {\n  const onSuccess = () => {\n    console.log('Logout made successfully');\n    alert('Logout made successfully ✌');\n  };\n\n  return (\n    <div>\n      <GoogleLogout\n        clientId={clientId}\n        buttonText=\"Logout\"\n        onLogoutSuccess={onSuccess}\n      ></GoogleLogout>\n    </div>\n  );\n}\n\nexport default Logout;\n"
  },
  {
    "path": "src/components/LogoutHooks.js",
    "content": "import React from 'react';\nimport { useGoogleLogout } from 'react-google-login';\n\nconst clientId =\n  '707788443358-u05p46nssla3l8tmn58tpo9r5sommgks.apps.googleusercontent.com';\n\nfunction LogoutHooks() {\n  const onLogoutSuccess = (res) => {\n    console.log('Logged out Success');\n    alert('Logged out Successfully ✌');\n  };\n\n  const onFailure = () => {\n    console.log('Handle failure cases');\n  };\n\n  const { signOut } = useGoogleLogout({\n    clientId,\n    onLogoutSuccess,\n    onFailure,\n  });\n\n  return (\n    <button onClick={signOut} className=\"button\">\n      <img src=\"icons/google.svg\" alt=\"google login\" className=\"icon\"></img>\n\n      <span className=\"buttonText\">Sign out</span>\n    </button>\n  );\n}\n\nexport default LogoutHooks;\n"
  },
  {
    "path": "src/index.css",
    "content": "body {\n  margin: 0;\n  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',\n    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',\n    sans-serif;\n  -webkit-font-smoothing: antialiased;\n  -moz-osx-font-smoothing: grayscale;\n}\n\ncode {\n  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',\n    monospace;\n}\n"
  },
  {
    "path": "src/index.js",
    "content": "import React from 'react';\nimport ReactDOM from 'react-dom';\nimport './index.css';\nimport App from './App';\nimport * as serviceWorker from './serviceWorker';\n\nReactDOM.render(\n  <React.StrictMode>\n    <App />\n  </React.StrictMode>,\n  document.getElementById('root')\n);\n\n// If you want your app to work offline and load faster, you can change\n// unregister() to register() below. Note this comes with some pitfalls.\n// Learn more about service workers: https://bit.ly/CRA-PWA\nserviceWorker.unregister();\n"
  },
  {
    "path": "src/serviceWorker.js",
    "content": "// This optional code is used to register a service worker.\n// register() is not called by default.\n\n// This lets the app load faster on subsequent visits in production, and gives\n// it offline capabilities. However, it also means that developers (and users)\n// will only see deployed updates on subsequent visits to a page, after all the\n// existing tabs open on the page have been closed, since previously cached\n// resources are updated in the background.\n\n// To learn more about the benefits of this model and instructions on how to\n// opt-in, read https://bit.ly/CRA-PWA\n\nconst isLocalhost = Boolean(\n  window.location.hostname === 'localhost' ||\n    // [::1] is the IPv6 localhost address.\n    window.location.hostname === '[::1]' ||\n    // 127.0.0.0/8 are considered localhost for IPv4.\n    window.location.hostname.match(\n      /^127(?:\\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/\n    )\n);\n\nexport function register(config) {\n  if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) {\n    // The URL constructor is available in all browsers that support SW.\n    const publicUrl = new URL(process.env.PUBLIC_URL, window.location.href);\n    if (publicUrl.origin !== window.location.origin) {\n      // Our service worker won't work if PUBLIC_URL is on a different origin\n      // from what our page is served on. This might happen if a CDN is used to\n      // serve assets; see https://github.com/facebook/create-react-app/issues/2374\n      return;\n    }\n\n    window.addEventListener('load', () => {\n      const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`;\n\n      if (isLocalhost) {\n        // This is running on localhost. Let's check if a service worker still exists or not.\n        checkValidServiceWorker(swUrl, config);\n\n        // Add some additional logging to localhost, pointing developers to the\n        // service worker/PWA documentation.\n        navigator.serviceWorker.ready.then(() => {\n          console.log(\n            'This web app is being served cache-first by a service ' +\n              'worker. To learn more, visit https://bit.ly/CRA-PWA'\n          );\n        });\n      } else {\n        // Is not localhost. Just register service worker\n        registerValidSW(swUrl, config);\n      }\n    });\n  }\n}\n\nfunction registerValidSW(swUrl, config) {\n  navigator.serviceWorker\n    .register(swUrl)\n    .then(registration => {\n      registration.onupdatefound = () => {\n        const installingWorker = registration.installing;\n        if (installingWorker == null) {\n          return;\n        }\n        installingWorker.onstatechange = () => {\n          if (installingWorker.state === 'installed') {\n            if (navigator.serviceWorker.controller) {\n              // At this point, the updated precached content has been fetched,\n              // but the previous service worker will still serve the older\n              // content until all client tabs are closed.\n              console.log(\n                'New content is available and will be used when all ' +\n                  'tabs for this page are closed. See https://bit.ly/CRA-PWA.'\n              );\n\n              // Execute callback\n              if (config && config.onUpdate) {\n                config.onUpdate(registration);\n              }\n            } else {\n              // At this point, everything has been precached.\n              // It's the perfect time to display a\n              // \"Content is cached for offline use.\" message.\n              console.log('Content is cached for offline use.');\n\n              // Execute callback\n              if (config && config.onSuccess) {\n                config.onSuccess(registration);\n              }\n            }\n          }\n        };\n      };\n    })\n    .catch(error => {\n      console.error('Error during service worker registration:', error);\n    });\n}\n\nfunction checkValidServiceWorker(swUrl, config) {\n  // Check if the service worker can be found. If it can't reload the page.\n  fetch(swUrl, {\n    headers: { 'Service-Worker': 'script' },\n  })\n    .then(response => {\n      // Ensure service worker exists, and that we really are getting a JS file.\n      const contentType = response.headers.get('content-type');\n      if (\n        response.status === 404 ||\n        (contentType != null && contentType.indexOf('javascript') === -1)\n      ) {\n        // No service worker found. Probably a different app. Reload the page.\n        navigator.serviceWorker.ready.then(registration => {\n          registration.unregister().then(() => {\n            window.location.reload();\n          });\n        });\n      } else {\n        // Service worker found. Proceed as normal.\n        registerValidSW(swUrl, config);\n      }\n    })\n    .catch(() => {\n      console.log(\n        'No internet connection found. App is running in offline mode.'\n      );\n    });\n}\n\nexport function unregister() {\n  if ('serviceWorker' in navigator) {\n    navigator.serviceWorker.ready\n      .then(registration => {\n        registration.unregister();\n      })\n      .catch(error => {\n        console.error(error.message);\n      });\n  }\n}\n"
  },
  {
    "path": "src/setupTests.js",
    "content": "// jest-dom adds custom jest matchers for asserting on DOM nodes.\n// allows you to do things like:\n// expect(element).toHaveTextContent(/react/i)\n// learn more: https://github.com/testing-library/jest-dom\nimport '@testing-library/jest-dom/extend-expect';\n"
  },
  {
    "path": "src/utils/refreshToken.js",
    "content": "export const refreshTokenSetup = (res) => {\n  // Timing to renew access token\n  let refreshTiming = (res.tokenObj.expires_in || 3600 - 5 * 60) * 1000;\n\n  const refreshToken = async () => {\n    const newAuthRes = await res.reloadAuthResponse();\n    refreshTiming = (newAuthRes.expires_in || 3600 - 5 * 60) * 1000;\n    console.log('newAuthRes:', newAuthRes);\n    // saveUserToken(newAuthRes.access_token);  <-- save new token\n    localStorage.setItem('authToken', newAuthRes.id_token);\n\n    // Setup the other timer after the first one\n    setTimeout(refreshToken, refreshTiming);\n  };\n\n  // Setup first refresh timer\n  setTimeout(refreshToken, refreshTiming);\n};\n"
  }
]