fix: Fixed auto login

This commit is contained in:
florian 2023-08-13 10:42:25 +02:00
parent fbf15d4c16
commit 973d6fc13f
6 changed files with 93 additions and 39 deletions

View File

@ -1,4 +1,4 @@
import { NostrImage, createImgProxyUrl } from '../nostrImageDownload';
import { NostrImage } from '../nostrImageDownload';
import './DetailsView.css';
import { useNDK } from '@nostr-dev-kit/ndk-react';
import DetailsAuthor from './DetailsAuthor';

View File

@ -26,6 +26,9 @@ import IconGrid from './Icons/IconGrid';
import useNav from '../utils/useNav';
import { NDKEvent } from '@nostr-dev-kit/ndk';
import { useGlobalState } from '../utils/globalState';
import useAutoLogin from '../utils/useAutoLogin';
type AlbyNostr = typeof window.nostr & { enabled: boolean };
/*
FEATURES:
@ -69,6 +72,7 @@ const SlideShow = () => {
const [showSettings, setShowSettings] = useState(false);
const { currentSettings: settings } = useNav();
const [state, setState] = useGlobalState();
const { autoLogin, setAutoLogin } = useAutoLogin();
const fetch = () => {
if (!ndk) {
@ -127,6 +131,7 @@ const SlideShow = () => {
// reset all
setPosts([]);
images.current = [];
clearTimeout(fetchTimeoutHandle.current);
fetch();
}
@ -173,6 +178,13 @@ const SlideShow = () => {
};
useEffect(() => {
setTimeout(() => {
if (autoLogin && window.nostr) {
// auto login when alby is available
onLogin();
}
}, 500);
document.body.addEventListener('keydown', onKeyDown);
return () => {
document.body.removeEventListener('keydown', onKeyDown);
@ -190,6 +202,7 @@ const SlideShow = () => {
}
const onLogin = async () => {
setAutoLogin(true);
const result = await loginWithNip07();
if (!result) {
console.error('Login failed.');
@ -201,6 +214,7 @@ const SlideShow = () => {
};
const onLogout = () => {
setAutoLogin(false);
setState({ userNPub: undefined, profile: undefined });
};

View File

@ -148,6 +148,7 @@ export const adultNPubs = [
'npub1tsrs6ptjnq5hluxawfme5sfxalfscapequm3ej0yfw65scwu8lys8q7y7l', // 💜 🔞EUPHORIA 🔞💜
'npub1fh8e9pnm8rfln0k7c6uh8wrvmva8enkdzsgzsc7v9jk97up23ewqs6kuue', // nostrporn
'npub1apr6dy5z4f0qs4cnswxj0gf37g46jxvh7xgwgs4wvzm6stu8f0asd4996r', // Anime Girl
'npub1acwrv7aqgu949mw0zxmw2akgsjqp574nnq4vcl9wln5355q79w5ssv9qxg', // Arianna
];
export const adultPublicKeys = adultNPubs.map(npub => (nip19.decode(npub).data as string).toLowerCase());
@ -168,7 +169,7 @@ export const defaultRelays = [
'wss://relay.damus.io',
'wss://relay.nostr.band',
'wss://nos.lol',
'wss://eden.nostr.land',
//'wss://eden.nostr.land',
'wss://relay.shitforce.one/',
'wss://nostr.wine',
// "wss://nostr1.current.fyi/",

View File

@ -1,43 +1,10 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import { NDKProvider } from '@nostr-dev-kit/ndk-react';
import App from './App';
import './index.css';
import { defaultRelays } from './components/env';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import GlobalState from './utils/globalState';
const router = createBrowserRouter([
{
path: '/',
element: <App />,
},
{
path: 'global',
element: <App />,
},
{
path: 'tags/:tags',
element: <App />,
},
{
path: 'profile/:npub',
element: <App />,
},
{
path: 'p/:npub',
element: <App />,
},
{
path: '/:npub',
element: <App />,
},
]);
import MainInner from './mainInner';
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<NDKProvider relayUrls={defaultRelays}>
<GlobalState>
<RouterProvider router={router} />
</GlobalState>
</NDKProvider>
<GlobalState>
<MainInner />
</GlobalState>
);

44
src/mainInner.tsx Normal file
View File

@ -0,0 +1,44 @@
import App from './App';
import './index.css';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import { NDKProvider } from '@nostr-dev-kit/ndk-react';
import { defaultRelays } from './components/env';
const MainInner = () => {
//const [state] = useGlobalState();
const router = createBrowserRouter([
{
path: '/',
element: <App />,
},
{
path: 'global',
element: <App />,
},
{
path: 'tags/:tags',
element: <App />,
},
{
path: 'profile/:npub',
element: <App />,
},
{
path: 'p/:npub',
element: <App />,
},
{
path: '/:npub',
element: <App />,
},
]);
return (
<NDKProvider relayUrls={defaultRelays}>
<RouterProvider router={router} />
</NDKProvider>
);
};
export default MainInner;

28
src/utils/useAutoLogin.ts Normal file
View File

@ -0,0 +1,28 @@
import { useEffect, useState } from 'react';
declare global {
interface Window {
localStorage: any;
}
}
const useAutoLogin = () => {
const [autoLogin, setAutoLogin] = useState(false);
useEffect(() => {
const disclaimerAcceptedPreviously = JSON.parse(localStorage.getItem('autoLogin') as string);
if (disclaimerAcceptedPreviously === true) {
setAutoLogin(true);
}
}, []);
return {
autoLogin,
setAutoLogin: (login: boolean) => {
setAutoLogin(login);
localStorage.setItem('autoLogin', JSON.stringify(login));
},
};
};
export default useAutoLogin;