From 08fd27cb26fb38da1e95a68a823ac86f71948bff Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Fri, 22 Jul 2022 00:03:16 -0700 Subject: [PATCH] Make main login/logout buttons reload server side props (#677) * Set cookies in auth handler before looking up user * Make sidebar logout button trigger SSR reload * Make sidebar login button trigger SSR reload --- web/components/auth-context.tsx | 4 ++-- web/components/create-question-button.tsx | 9 ++++++++- web/components/nav/sidebar.tsx | 13 ++++++++++--- web/lib/firebase/users.ts | 2 +- web/pages/index.tsx | 14 -------------- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/web/components/auth-context.tsx b/web/components/auth-context.tsx index fcc3de39..653368b6 100644 --- a/web/components/auth-context.tsx +++ b/web/components/auth-context.tsx @@ -41,6 +41,7 @@ export function AuthProvider({ children }: any) { useEffect(() => { return onIdTokenChanged(auth, async (fbUser) => { if (fbUser) { + setAuthCookies(await fbUser.getIdToken(), fbUser.refreshToken) let user = await getUser(fbUser.uid) if (!user) { const deviceToken = ensureDeviceToken() @@ -51,12 +52,11 @@ export function AuthProvider({ children }: any) { // Note: Cap on localStorage size is ~5mb localStorage.setItem(CACHED_USER_KEY, JSON.stringify(user)) setCachedReferralInfoForUser(user) - setAuthCookies(await fbUser.getIdToken(), fbUser.refreshToken) } else { // User logged out; reset to null + deleteAuthCookies() setAuthUser(null) localStorage.removeItem(CACHED_USER_KEY) - deleteAuthCookies() } }) }, [setAuthUser]) diff --git a/web/components/create-question-button.tsx b/web/components/create-question-button.tsx index 277816fa..1b8ac11e 100644 --- a/web/components/create-question-button.tsx +++ b/web/components/create-question-button.tsx @@ -1,4 +1,5 @@ import Link from 'next/link' +import { useRouter } from 'next/router' import clsx from 'clsx' import { firebaseLogin, User } from 'web/lib/firebase/users' import React from 'react' @@ -16,6 +17,7 @@ export const CreateQuestionButton = (props: { 'from-indigo-500 to-blue-500 hover:from-indigo-700 hover:to-blue-700' const { user, overrideText, className, query } = props + const router = useRouter() return (
{user ? ( @@ -26,7 +28,12 @@ export const CreateQuestionButton = (props: { ) : (