diff --git a/src/controllers/AuthController.ts b/src/controllers/AuthController.ts index 8e488c5..1e853d1 100644 --- a/src/controllers/AuthController.ts +++ b/src/controllers/AuthController.ts @@ -1,13 +1,18 @@ import { EventTemplate } from 'nostr-tools' import { MetadataController, NostrController } from '.' -import { setAuthState, setMetadataEvent } from '../store/actions' +import { + setAuthState, + setMetadataEvent, + setRelayMapAction +} from '../store/actions' import store from '../store/store' import { base64DecodeAuthToken, base64EncodeSignedEvent, getAuthToken, getVisitedLink, - saveAuthToken + saveAuthToken, + compareObjects } from '../utils' import { appPrivateRoutes } from '../routes' import { SignedEvent } from '../types' @@ -30,7 +35,7 @@ export class AuthController { * @returns url to redirect if authentication successfull * or error if otherwise */ - async authenticateAndFindMetadata(pubkey: string) { + async authAndGetMetadataAndRelaysMap(pubkey: string) { this.metadataController .findMetadata(pubkey) .then((event) => { @@ -61,6 +66,27 @@ export class AuthController { }) ) + const relaysState = store.getState().relays + + if (relaysState) { + // Relays state is defined and there is no need to await for the latest relay map + this.nostrController.getRelayMap(pubkey).then((relayMap) => { + if (!compareObjects(relaysState?.map, relayMap)) { + store.dispatch(setRelayMapAction(relayMap.map)) + } + }) + } else { + // Relays state is not defined, await for the latest relay map + const relayMap = await this.nostrController.getRelayMap(pubkey) + + if (Object.keys(relayMap).length < 1) { + // Navigate user to relays page + return Promise.resolve(appPrivateRoutes.relays) + } + + store.dispatch(setRelayMapAction(relayMap.map)) + } + const visitedLink = getVisitedLink() if (visitedLink) { diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 7d26348..bc1366a 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -54,7 +54,7 @@ export const Login = () => { setLoadingSpinnerDesc('Authenticating and finding metadata') const redirectPath = - await authController.authenticateAndFindMetadata(pubkey) + await authController.authAndGetMetadataAndRelaysMap(pubkey) navigate(redirectPath) }) @@ -93,7 +93,7 @@ export const Login = () => { setLoadingSpinnerDesc('Authenticating and finding metadata') const redirectPath = await authController - .authenticateAndFindMetadata(publickey) + .authAndGetMetadataAndRelaysMap(publickey) .catch((err) => { toast.error('Error occurred in authentication: ' + err) return null @@ -188,7 +188,7 @@ export const Login = () => { setLoadingSpinnerDesc('Authenticating and finding metadata') const redirectPath = await authController - .authenticateAndFindMetadata(pubkey!) + .authAndGetMetadataAndRelaysMap(pubkey!) .catch((err) => { toast.error('Error occurred in authentication: ' + err) return null @@ -248,7 +248,7 @@ export const Login = () => { setLoadingSpinnerDesc('Authenticating and finding metadata') const redirectPath = await authController - .authenticateAndFindMetadata(pubkey!) + .authAndGetMetadataAndRelaysMap(pubkey!) .catch((err) => { toast.error('Error occurred in authentication: ' + err) return null diff --git a/src/pages/relays/index.tsx b/src/pages/relays/index.tsx index 3ee558c..ad1626a 100644 --- a/src/pages/relays/index.tsx +++ b/src/pages/relays/index.tsx @@ -176,6 +176,13 @@ export const RelaysPage = () => { } } + useEffect(() => { + // Display notification if an empty relay map has been received + if (relayMap && Object.keys(relayMap).length === 0) { + relayRequirementWarning() + } + }, [relayMap]) + return (