diff --git a/src/components/AppBar/AppBar.tsx b/src/components/AppBar/AppBar.tsx index 7beae68..763af39 100644 --- a/src/components/AppBar/AppBar.tsx +++ b/src/components/AppBar/AppBar.tsx @@ -67,7 +67,9 @@ export const AppBar = () => { setAuthState({ loggedIn: false, usersPubkey: undefined, - loginMethod: undefined + loginMethod: undefined, + nsecBunkerPubkey: undefined, + nsecbunkerRelay: undefined }) ) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 63e4bc6..034f603 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -12,13 +12,14 @@ import { import { updateKeyPair, updateLoginMethod, - updateNsecbunkerPubkey + updateNsecbunkerPubkey, + updateNsecbunkerRelay } from '../../store/actions' import { LoginMethods } from '../../store/auth/types' import { Dispatch } from '../../store/store' import styles from './style.module.scss' import { useNavigate } from 'react-router-dom' -import { queryNip05 } from '../../utils' +import { pubToHex, queryNip05 } from '../../utils' export const Login = () => { const dispatch: Dispatch = useDispatch() @@ -206,7 +207,71 @@ export const Login = () => { }) } + const loginWithBunkerConnectionString = async () => { + // Extract the key + const keyStartIndex = inputValue.indexOf('bunker://') + 'bunker://'.length + const keyEndIndex = inputValue.indexOf('?relay=') + const key = inputValue.substring(keyStartIndex, keyEndIndex) + + const pubkey = await pubToHex(key) + + if (!pubkey) { + toast.error('Invalid pubkey in bunker connection string.') + setIsLoading(false) + return + } + + // Extract the relay value + const relayIndex = inputValue.indexOf('relay=') + const relay = inputValue.substring( + relayIndex + 'relay='.length, + inputValue.length + ) + + setIsLoading(true) + setLoadingSpinnerDesc('Initializing bunker NDK') + + await nostrController.nsecBunkerInit([relay]) + + setLoadingSpinnerDesc('Creating remote signer') + await nostrController + .createNsecBunkerSigner(pubkey) + .then(async (signer) => { + signer.on('authUrl', (url: string) => { + setAuthUrl(url) + }) + + dispatch(updateLoginMethod(LoginMethods.nsecBunker)) + dispatch(updateNsecbunkerPubkey(pubkey)) + dispatch(updateNsecbunkerRelay(relay)) + + setLoadingSpinnerDesc('Authenticating and finding metadata') + + const redirectPath = await authController + .authenticateAndFindMetadata(pubkey!) + .catch((err) => { + toast.error('Error occurred in authentication: ' + err) + return null + }) + + if (redirectPath) navigate(redirectPath) + }) + .catch((err) => { + toast.error( + 'An error occurred while creating nsecbunker signer: ' + err + ) + }) + .finally(() => { + setIsLoading(false) + setLoadingSpinnerDesc('') + }) + } + const login = () => { + if (inputValue.startsWith('bunker://')) { + return loginWithBunkerConnectionString() + } + if (inputValue.startsWith('nsec')) { return loginWithNsec() } @@ -238,7 +303,7 @@ export const Login = () => {