From 5ad9d2bc0fd0d713fa14563858154ceb10517d7f Mon Sep 17 00:00:00 2001 From: Davinci Date: Fri, 17 May 2024 15:28:35 +0200 Subject: [PATCH] fix: opening link to sign a file while not logged in is not redirecting correctly --- src/App.tsx | 12 +++++++----- src/pages/login/index.tsx | 25 ++++++++++++++++++++----- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 58f0c71..b8d0c47 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -33,6 +33,12 @@ const App = () => { } } + const handleRootRedirect = () => { + if (authState.loggedIn) return appPrivateRoutes.homePage + const callbackPathEncoded = btoa(window.location.href.split(`${window.location.origin}/#`)[1]) + return `${appPublicRoutes.login}?callbackPath=${callbackPathEncoded}` + } + return ( }> @@ -70,11 +76,7 @@ const App = () => { path="*" element={ } /> diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index a4a49b7..36f9747 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -2,7 +2,7 @@ import { Box, Button, TextField, Typography } from '@mui/material' import { getPublicKey, nip19 } from 'nostr-tools' import { useEffect, useState } from 'react' import { useDispatch } from 'react-redux' -import { useNavigate } from 'react-router-dom' +import { useNavigate, useSearchParams } from 'react-router-dom' import { toast } from 'react-toastify' import { LoadingSpinner } from '../../components/LoadingSpinner' import { @@ -22,6 +22,8 @@ import { npubToHex, queryNip05 } from '../../utils' import styles from './style.module.scss' export const Login = () => { + const [searchParams] = useSearchParams() + const dispatch: Dispatch = useDispatch() const navigate = useNavigate() @@ -53,6 +55,19 @@ export const Login = () => { } } + const navigateAfterLogin = (path: string) => { + const callbackPath = searchParams.get('callbackPath') + + if (callbackPath) { + // base64 decoded path + const path = atob(callbackPath) + navigate(path) + return + } + + navigate(path) + } + const loginWithExtension = async () => { setIsLoading(true) setLoadingSpinnerDesc('Capturing pubkey from nostr extension') @@ -66,7 +81,7 @@ export const Login = () => { const redirectPath = await authController.authAndGetMetadataAndRelaysMap(pubkey) - navigate(redirectPath) + navigateAfterLogin(redirectPath) }) .catch((err) => { toast.error('Error capturing public key from nostr extension: ' + err) @@ -109,7 +124,7 @@ export const Login = () => { return null }) - if (redirectPath) navigate(redirectPath) + if (redirectPath) navigateAfterLogin(redirectPath) setIsLoading(false) setLoadingSpinnerDesc('') @@ -204,7 +219,7 @@ export const Login = () => { return null }) - if (redirectPath) navigate(redirectPath) + if (redirectPath) navigateAfterLogin(redirectPath) }) .catch((err) => { toast.error( @@ -264,7 +279,7 @@ export const Login = () => { return null }) - if (redirectPath) navigate(redirectPath) + if (redirectPath) navigateAfterLogin(redirectPath) }) .catch((err) => { toast.error(