From f3ec3b0b88a43e2a48db51b81f9b5778ade11248 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 8 Oct 2023 18:45:33 +0300 Subject: Fix initial auth from cookie --- vnext/src/App.js | 84 ++++++++++++++++-------------------------- vnext/src/api/index.js | 6 +-- vnext/src/index.js | 49 ++++++++++++++---------- vnext/src/ui/Feeds.js | 1 - vnext/src/ui/Login.js | 4 +- vnext/src/ui/Message.js | 8 ++-- vnext/src/ui/VisitorContext.js | 17 ++++----- 7 files changed, 78 insertions(+), 91 deletions(-) (limited to 'vnext') diff --git a/vnext/src/App.js b/vnext/src/App.js index e723fe9c..c7849418 100644 --- a/vnext/src/App.js +++ b/vnext/src/App.js @@ -30,7 +30,7 @@ import { Toaster } from 'react-hot-toast' export default function App({ footer }) { let contentRef = useRef(null) - const [cookie, setCookie] = useCookies(['hash']) + const [, setCookie] = useCookies(['hash']) const [allTrends, setAllTrends] = useState([]) @@ -54,32 +54,13 @@ export default function App({ footer }) { }).catch(console.error) }, [setVisitor]) - const [hash, setHash] = useState(cookie.hash) - const [eventSource, setEventSource] = /** @param EventSource? */ useState({}) - /** - * @param {import("./api").SecureUser} visitor - */ - let auth = useCallback((visitor) => { - setVisitor(prevState => { - if (visitor.hash != prevState.hash) { - setHash(visitor.hash) - } - return visitor - }) - }, [setVisitor]) - useEffect(() => { let es - const anonymousUser = { - uid: 0 - } - if (hash) { - me().then(visitor => auth(visitor)) - .catch(() => setVisitor(anonymousUser)) + if (visitor) { if ('EventSource' in window) { - const eventParams = new URLSearchParams({ hash: hash }) + const eventParams = new URLSearchParams({ hash: visitor.hash }) let url = new URL(`https://juick.com/api/events?${eventParams.toString()}`) console.log(url.toString()) es = new EventSource(url.toString()) @@ -94,8 +75,6 @@ export default function App({ footer }) { es.addEventListener('msg', updateStatus) setEventSource(es) } - } else { - setVisitor(anonymousUser) } return (() => { if (es && es.removeEventListener) { @@ -103,7 +82,7 @@ export default function App({ footer }) { es.removeEventListener('msg', updateStatus) } }) - }, [auth, hash, setVisitor, updateStatus]) + }, [visitor, updateStatus]) useEffect(() => { const getTrends = async () => { @@ -121,32 +100,33 @@ export default function App({ footer }) {