diff --git a/components/Newsfeed/Newsfeed.tsx b/components/Newsfeed/Newsfeed.tsx index cc124c307..667d01e8b 100644 --- a/components/Newsfeed/Newsfeed.tsx +++ b/components/Newsfeed/Newsfeed.tsx @@ -7,6 +7,7 @@ import { Col, Row, Spinner } from "../bootstrap" import { Profile, useProfile, usePublicProfile } from "../db" import { NotificationProps, Notifications } from "./NotificationProps" import notificationQuery from "./notification-query" + import { BillCol, Header, @@ -16,6 +17,8 @@ import { import ProfileSettingsModal from "components/EditProfilePage/ProfileSettingsModal" import { NewsfeedCard } from "components/NewsfeedCard/NewsfeedCard" import { ProfileButtons } from "components/ProfilePage/ProfileButtons" +import { useAppDispatch } from "components/hooks" +import { authStepChanged } from "components/auth/redux" export default function Newsfeed() { const { t } = useTranslation("common") @@ -31,6 +34,26 @@ export default function Newsfeed() { const [allResults, setAllResults] = useState([]) const [filteredResults, setFilteredResults] = useState([]) + const dispatch = useAppDispatch() + + const [initialLoadComplete, setInitialLoadComplete] = useState(false) + + useEffect(() => { + if (!loading) { + setInitialLoadComplete(true) + } + }, [loading]) + + useEffect(() => { + if (!initialLoadComplete) return + + if (!profile) { + dispatch(authStepChanged("protectedpage")) + } else { + dispatch(authStepChanged(null)) + } + }, [dispatch, profile, initialLoadComplete]) + useEffect(() => { const results = allResults.filter(result => { if (isShowingOrgs && result.type == `testimony`) return true @@ -251,7 +274,7 @@ export default function Newsfeed() { ) : ( - + )} )} diff --git a/components/auth/AuthModal.tsx b/components/auth/AuthModal.tsx index 079f07538..7b066afcf 100644 --- a/components/auth/AuthModal.tsx +++ b/components/auth/AuthModal.tsx @@ -7,41 +7,51 @@ import VerifyEmailModal from "./VerifyEmailModal" import ProfileTypeModal from "./ProfileTypeModal" import { AuthFlowStep, authStepChanged, useAuth } from "./redux" import { useAppDispatch } from "components/hooks" +import { useRouter } from "next/router" export default function AuthModal() { const dispatch = useAppDispatch() - const { authFlowStep: currentModal } = useAuth() + const router = useRouter() + const { authFlowStep: currentModal, isFromProtectedPage } = useAuth() const setCurrentModal = (step: AuthFlowStep) => dispatch(authStepChanged(step)) - const close = () => dispatch(authStepChanged(null)) - + const close = () => { + dispatch(authStepChanged(null)) + } + const closeModal = () => { + dispatch(authStepChanged(null)) + if (isFromProtectedPage) { + router.push("/") + } + } return ( <> setCurrentModal("signIn")} onSignUpClick={() => setCurrentModal("chooseProfileType")} /> setCurrentModal("userSignUp")} onOrgUserClick={() => setCurrentModal("orgSignUp")} /> setCurrentModal("forgotPassword")} /> setCurrentModal("verifyEmail")} /> setCurrentModal("verifyEmail")} /> diff --git a/components/auth/SignInModal.tsx b/components/auth/SignInModal.tsx index ba661fa94..9cfa695d7 100644 --- a/components/auth/SignInModal.tsx +++ b/components/auth/SignInModal.tsx @@ -16,11 +16,13 @@ import { useTranslation } from "next-i18next" export default function SignInModal({ show, onHide, + onLogin, onForgotPasswordClick }: { show?: boolean onForgotPasswordClick: () => void onHide: () => void + onLogin: () => void }) { const { register, @@ -41,7 +43,7 @@ export default function SignInModal({ }, [show, reset]) const onSubmit = handleSubmit(credentials => { - signIn.execute(credentials).then(onHide) + signIn.execute(credentials).then(onLogin) }) const { t } = useTranslation("auth") diff --git a/components/auth/redux.ts b/components/auth/redux.ts index fc3c97ce3..6821a37fd 100644 --- a/components/auth/redux.ts +++ b/components/auth/redux.ts @@ -12,6 +12,7 @@ export type AuthFlowStep = | "forgotPassword" | "verifyEmail" | "chooseProfileType" + | "protectedpage" | null export interface State { @@ -23,12 +24,14 @@ export interface State { /** True iff user is signed in */ authenticated: boolean authFlowStep: AuthFlowStep + isFromProtectedPage: boolean } const initialState: State = { authenticated: false, user: undefined, - authFlowStep: null + authFlowStep: null, + isFromProtectedPage: false } export const { @@ -48,6 +51,11 @@ export const { }, authStepChanged(state, action: PayloadAction) { state.authFlowStep = action.payload + if (action.payload === "protectedpage") { + state.isFromProtectedPage = true + } else if (action.payload === "start") { + state.isFromProtectedPage = false + } } } }) diff --git a/components/auth/service.tsx b/components/auth/service.tsx index f21730599..3edcabc28 100644 --- a/components/auth/service.tsx +++ b/components/auth/service.tsx @@ -92,6 +92,6 @@ export function requireAuth( * Redirects user after logging out. */ export async function signOutAndRedirectToHome() { + await Router.push("/") await auth.signOut() - Router.push("/") } diff --git a/public/locales/en/auth.json b/public/locales/en/auth.json index 8a5b6f0a9..9b0a4699e 100644 --- a/public/locales/en/auth.json +++ b/public/locales/en/auth.json @@ -55,6 +55,4 @@ "verifyEmail": "Verify your email address", "verifyLinkSent": "Please verify your email for your account by clicking the verification link we sent to your email. You will be required to verify your email before submitting testimony.", "setUpProfile": "Set Up Your Profile" - - } \ No newline at end of file