Skip to content

Commit

Permalink
Added root App component with silent auth
Browse files Browse the repository at this point in the history
  • Loading branch information
rimutaka committed Aug 18, 2024
1 parent aaa9c5b commit 44d2c0f
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 51 deletions.
6 changes: 3 additions & 3 deletions build/asset-manifest.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"files": {
"main.css": "./static/css/main.71786728.css",
"main.js": "./static/js/main.dd254ed8.js",
"main.js": "./static/js/main.e34b8f99.js",
"static/media/isbn_mod_bg.wasm": "./static/media/isbn_mod_bg.415d4eff989481533d43.wasm",
"static/media/buy.svg": "./static/media/buy.a0ebbd4b83f7c8afd5d9.svg",
"static/media/icomoon.woff": "./static/media/icomoon.25908258b2a9c6e6da52.woff",
"index.html": "./index.html",
"static/media/about.svg": "./static/media/about.472d9c94914ce88e8d8f.svg",
"static/media/borrow.svg": "./static/media/borrow.f8356d8f6c1fc40fa23b.svg",
"main.71786728.css.map": "./static/css/main.71786728.css.map",
"main.dd254ed8.js.map": "./static/js/main.dd254ed8.js.map"
"main.e34b8f99.js.map": "./static/js/main.e34b8f99.js.map"
},
"entrypoints": [
"static/css/main.71786728.css",
"static/js/main.dd254ed8.js"
"static/js/main.e34b8f99.js"
]
}
2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang="en"><head prefix="og: http://ogp.me/ns#"><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="referrer" content="no-referrer"/><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>📖📚📚</title><meta name="description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:title" content="Scan ISBN to record or share a book"/><meta property="og:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:site_name" content="Bookworm Food"><meta property="og:type" content="website"><meta property="og:url" content="https://bookwormfood.com"><meta property="og:image" itemprop="image" content="http://bookwormfood.com/img/og-image-400.png"/><meta property="og:image:secure_url" itemprop="image" content="https://bookwormfood.com/img/og-image-400.png"><meta property="og:image:width" content="400"><meta property="og:image:height" content="400"><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Scan ISBN to record or share a book"/><meta name="twitter:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."/><meta name="twitter:image" content="https://bookwormfood.com/img/og-image-400.png"/><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@400;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=News+Cycle:wght@400;700&display=swap" rel="stylesheet"><script defer="defer" src="/static/js/main.dd254ed8.js"></script><link href="/static/css/main.71786728.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>
<!doctype html><html lang="en"><head prefix="og: http://ogp.me/ns#"><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"><meta name="referrer" content="no-referrer"/><meta name="theme-color" content="#ffffff"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>📖📚📚</title><meta name="description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:title" content="Scan ISBN to record or share a book"/><meta property="og:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."><meta property="og:site_name" content="Bookworm Food"><meta property="og:type" content="website"><meta property="og:url" content="https://bookwormfood.com"><meta property="og:image" itemprop="image" content="http://bookwormfood.com/img/og-image-400.png"/><meta property="og:image:secure_url" itemprop="image" content="https://bookwormfood.com/img/og-image-400.png"><meta property="og:image:width" content="400"><meta property="og:image:height" content="400"><meta name="twitter:card" content="summary"/><meta name="twitter:title" content="Scan ISBN to record or share a book"/><meta name="twitter:description" content="A pocket assistant for keen readers: find more information about the book or the author online, borrow it from your local library, buy, sell or share."/><meta name="twitter:image" content="https://bookwormfood.com/img/og-image-400.png"/><link rel="apple-touch-icon" sizes="180x180" href="/img/apple-touch-icon.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/favicon-16x16.png"><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.googleapis.com/css2?family=Amatic+SC:wght@400;700&display=swap" rel="stylesheet"><link href="https://fonts.googleapis.com/css2?family=News+Cycle:wght@400;700&display=swap" rel="stylesheet"><script defer="defer" src="/static/js/main.e34b8f99.js"></script><link href="/static/css/main.71786728.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="app"></div></body></html>
1 change: 0 additions & 1 deletion build/static/js/main.dd254ed8.js.map

This file was deleted.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions build/static/js/main.e34b8f99.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if ("function" === typeof importScripts) {
self.addEventListener("install", event => {
self.skipWaiting();
});
workbox.precaching.precacheAndRoute([{"revision":"c6130d0fcd5b638ee6ac39a6b5b780e1","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"37ff8dc0d50cd7705fc65fc84837c2aa","url":"img/og-image-400.png"},{"revision":"54c6768df4ab6361172cc04205aa081e","url":"index.html"},{"revision":"04e972588e780145b7edd5c11b0902e8","url":"static/css/main.71786728.css"},{"revision":"52ab47a0a4a2db71baae247ba7e6e9b2","url":"static/js/main.dd254ed8.js"},{"revision":"3311ddb0ad85b9240262a753f5a667b7","url":"static/media/about.472d9c94914ce88e8d8f.svg"},{"revision":"8d00e8b89883db0a8a11d9f91b427d10","url":"static/media/borrow.f8356d8f6c1fc40fa23b.svg"},{"revision":"fb577b765b6c263191ff525af5f4f175","url":"static/media/buy.a0ebbd4b83f7c8afd5d9.svg"},{"revision":"b4f1d77229af9fed1ec3978b8f9a6ce8","url":"static/media/isbn_mod_bg.415d4eff989481533d43.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.precaching.precacheAndRoute([{"revision":"5132027dcca037e8920ffbcd74bf5b43","url":"asset-manifest.json"},{"revision":"4020571efe44dc33d271798e6a18e0c1","url":"favicon.ico"},{"revision":"f98923403f5c78d689d56b18f947c520","url":"img/apple-touch-icon.png"},{"revision":"a62aa63bb4d0a3dd08820787bd7e118f","url":"img/favicon-16x16.png"},{"revision":"a5229a03fcfe584a3031846fe3c19ccf","url":"img/favicon-32x32.png"},{"revision":"37ff8dc0d50cd7705fc65fc84837c2aa","url":"img/og-image-400.png"},{"revision":"5a021c9518ecaa3109f107494edb3866","url":"index.html"},{"revision":"04e972588e780145b7edd5c11b0902e8","url":"static/css/main.71786728.css"},{"revision":"30a2a0b9983888f57b4d59361c64607b","url":"static/js/main.e34b8f99.js"},{"revision":"3311ddb0ad85b9240262a753f5a667b7","url":"static/media/about.472d9c94914ce88e8d8f.svg"},{"revision":"8d00e8b89883db0a8a11d9f91b427d10","url":"static/media/borrow.f8356d8f6c1fc40fa23b.svg"},{"revision":"fb577b765b6c263191ff525af5f4f175","url":"static/media/buy.a0ebbd4b83f7c8afd5d9.svg"},{"revision":"b4f1d77229af9fed1ec3978b8f9a6ce8","url":"static/media/isbn_mod_bg.415d4eff989481533d43.wasm"},{"revision":"24f2b115d3964c9f977462cdd38b066a","url":"wasm/koder.js"},{"revision":"6f11e7db4fe9aca82cac7150bfc33769","url":"wasm/zbar.js"},{"revision":"e8789bf03df9c2c85e9c59ab0a0cd0c6","url":"wasm/zbar.wasm"},{"revision":"bb1c649a95ffa80369254cc3e51b9a41","url":"wasmWorker.js"}]);
workbox.routing.registerRoute(
new RegExp("https://fonts.(?:.googlepis|gstatic).com/(.*)"),
new workbox.strategies.CacheFirst({
Expand Down
55 changes: 55 additions & 0 deletions src/components/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useEffect } from "react";
import { useLocation, Outlet } from "react-router-dom";
import { useAuth0 } from '@auth0/auth0-react'
import { LAST_AUTH_TIMESTAMP } from "./bookDetails.js";

export default function App() {

const location = useLocation();
const { isAuthenticated, isLoading, loginWithRedirect, getAccessTokenSilently } = useAuth0();

useEffect(() => {

console.log(`App load/auth: ${isLoading}/${isAuthenticated}`);

// save auth details in the localStorage
if (!isLoading) {
if (isAuthenticated) {
localStorage.setItem(LAST_AUTH_TIMESTAMP, Date.now());
console.log("Auth status updated in LS");
}
else {
console.log("Not authenticated");

(async () => {

// log in the user if was logged in before
const lastAuth = localStorage.getItem(LAST_AUTH_TIMESTAMP);
console.log(`Last auth/auth'd: ${lastAuth}/${isAuthenticated}`);
if (lastAuth && !isAuthenticated) {

console.log("User was logged in before, logging in again");
try {
const accessToken = await getAccessTokenSilently();
console.log(`Access token: ${accessToken}`);
} catch (e) {
console.log(`Error getting access token: ${e}`);
await loginWithRedirect({
appState: {
returnTo: window.location.pathname,
},
})
}
}
})();
}
}

}, [isLoading, isAuthenticated]);

return (
<div className="main">
<Outlet />
</div>
);
};
16 changes: 3 additions & 13 deletions src/components/bookDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default function BookDetails() {

const navigate = useNavigate();
const location = useLocation();
const { getIdTokenClaims, isAuthenticated, loginWithRedirect } = useAuth0();
const { getIdTokenClaims, isAuthenticated } = useAuth0();

// console.log(location);
// the ISBN can be located anywhere in the URL,
Expand All @@ -86,18 +86,6 @@ export default function BookDetails() {
// fetch book data if the ISBN code is found in the URL
if (isbn) {
(async () => {

// log in the user if was logged in before
const lastAuth = localStorage.getItem(LAST_AUTH_TIMESTAMP);
console.log(`Last auth/auth'd: ${lastAuth}/${isAuthenticated}`);
if (lastAuth && !isAuthenticated) {
await loginWithRedirect({
appState: {
returnTo: window.location.pathname,
},
});
}

// get the ID token to send to the server, if the user is logged in
// the server only needs the email address from the claims
let idTokenClaims = null;
Expand All @@ -109,6 +97,8 @@ export default function BookDetails() {
} else {
console.log(`Token: ${JSON.stringify(idTokenClaims)}`);
}
} else {
console.log("User is not authenticated");
}

await initWasmModule(); // run the wasm initializer before calling wasm methods
Expand Down
33 changes: 4 additions & 29 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as React from "react";
import * as ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import { Routes, Route, Outlet, Link, useLocation } from "react-router-dom";
import { useAuth0 } from "@auth0/auth0-react";
import { LAST_AUTH_TIMESTAMP } from "./components/bookDetails.js";
import { Routes, Route, Link } from "react-router-dom";

import * as serviceWorker from './serviceWorker';
import App from "./components/app";
import Scan from "./components/scan";
import BookDetails from "./components/bookDetails";
import Welcome from "./components/welcome";
Expand All @@ -15,14 +14,14 @@ import { Auth0ProviderWithNavigate } from "./components/auth0-provider-with-navi

import ".//css/index.css";

console.log("app started")
// console.log("app started")

ReactDOM.createRoot(document.getElementById("app")).render(

<BrowserRouter>
<Auth0ProviderWithNavigate>
<Routes>
<Route path="/" element={<Layout />}>
<Route path="/" element={<App />}>
<Route index element={<Welcome />} />
<Route path="scan" element={<Scan scanRate={250} />} />
<Route path="about" element={<About />} />
Expand All @@ -38,30 +37,6 @@ ReactDOM.createRoot(document.getElementById("app")).render(

serviceWorker.register();

function Layout() {

const location = useLocation();
const { isAuthenticated, isLoading } = useAuth0();

React.useEffect(() => {

console.log(`Layout load/auth: ${isLoading}/${isAuthenticated}`);

// save auth details in the localStorage
if (!isLoading && isAuthenticated) {
localStorage.setItem(LAST_AUTH_TIMESTAMP, Date.now());
console.log("Auth status updated");
}

}, [isAuthenticated]);

return (
<div className="main">
<Outlet />
</div>
);
}

function About() {
return (
<div>
Expand Down

0 comments on commit 44d2c0f

Please sign in to comment.