From 9287c9bd49d2e0a454528679f774d3149f64ea30 Mon Sep 17 00:00:00 2001 From: austinkelsay Date: Sun, 11 Feb 2024 00:36:25 -0600 Subject: [PATCH] auto login / anon login --- src/components/Layout.js | 11 ++++++++++ src/hooks/useAutoLogin.js | 39 +++++++++++++++++++++++++++++++++++ src/pages/_app.js | 7 +++++-- src/pages/api/users/[slug].js | 1 - src/pages/login.js | 15 ++++++++++++++ 5 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 src/components/Layout.js create mode 100644 src/hooks/useAutoLogin.js diff --git a/src/components/Layout.js b/src/components/Layout.js new file mode 100644 index 0000000..f2b9be1 --- /dev/null +++ b/src/components/Layout.js @@ -0,0 +1,11 @@ +import React from 'react'; +import { useAutoLogin } from '@/hooks/useAutoLogin'; + +const Layout = ({ children }) => { + useAutoLogin(); + console.log('Layout'); + + return <>{children}; +}; + +export default Layout; diff --git a/src/hooks/useAutoLogin.js b/src/hooks/useAutoLogin.js new file mode 100644 index 0000000..d7f15e3 --- /dev/null +++ b/src/hooks/useAutoLogin.js @@ -0,0 +1,39 @@ +import { useEffect } from 'react'; +import { useRouter } from 'next/router'; +import { useDispatch } from 'react-redux'; +import axios from 'axios'; +import { setPubkey, setUsername } from "@/redux/reducers/userReducer"; + +export const useAutoLogin = () => { + const dispatch = useDispatch(); + const router = useRouter(); + console.log('useAutoLogin'); + + useEffect(() => { + const publicKey = window.localStorage.getItem('pubkey'); + console.log('Auto logging in with public key:', publicKey); + + if (publicKey) { + const login = async () => { + try { + const response = await axios.get(`/api/users/${publicKey}`); + if (response.status === 200 && response.data) { + dispatch(setPubkey(publicKey)); + if (response.data.username) { + dispatch(setUsername(response.data.username)); + } + } else { + // Handle user not found or other errors appropriately + } + } catch (error) { + console.error('Error during auto login:', error); + // Handle error + } + }; + + login(); + } + }, [dispatch]); + + return null; +}; diff --git a/src/pages/_app.js b/src/pages/_app.js index 2a181d3..2536b1e 100644 --- a/src/pages/_app.js +++ b/src/pages/_app.js @@ -3,6 +3,7 @@ import { Provider } from "react-redux"; import { store } from "@/redux/store"; import Navbar from '@/components/navbar/Navbar'; import { ToastProvider } from '@/hooks/useToast'; +import Layout from '@/components/layout'; import '@/styles/globals.css' import 'primereact/resources/themes/lara-dark-indigo/theme.css'; @@ -13,8 +14,10 @@ export default function MyApp({ - - + + + + diff --git a/src/pages/api/users/[slug].js b/src/pages/api/users/[slug].js index a23a398..e2634f2 100644 --- a/src/pages/api/users/[slug].js +++ b/src/pages/api/users/[slug].js @@ -9,7 +9,6 @@ export default async function handler(req, res) { try { let user; if (isPubkey) { - console.log('is pub', slug); // If slug is a pubkey user = await getUserByPubkey(slug); } else { diff --git a/src/pages/login.js b/src/pages/login.js index c8036b5..930101b 100644 --- a/src/pages/login.js +++ b/src/pages/login.js @@ -5,6 +5,7 @@ import { useRouter } from "next/router"; import { Button } from 'primereact/button'; import { useToast } from "@/hooks/useToast"; import { useNostr } from "@/hooks/useNostr"; +import { generateSecretKey, getPublicKey } from 'nostr-tools' import { findKind0Username } from "@/utils/nostr"; import { setPubkey, setUsername } from "@/redux/reducers/userReducer"; @@ -12,6 +13,7 @@ const Login = () => { const dispatch = useDispatch(); const router = useRouter(); const { showToast } = useToast(); + const { fetchKind0 } = useNostr(); const nostrLogin = async () => { try { @@ -28,6 +30,7 @@ const Login = () => { const response = await axios.get(`/api/users/${publicKey}`); if (response.status === 200 && response.data) { dispatch(setPubkey(publicKey)); + window.localStorage.setItem('pubkey', publicKey); if (response.data.username) { dispatch(setUsername(response.data.username)); } @@ -47,6 +50,7 @@ const Login = () => { const createUserResponse = await axios.post(`/api/users`, payload); if (createUserResponse.status === 201) { dispatch(setPubkey(publicKey)); + window.localStorage.setItem('pubkey', publicKey); if (username) { dispatch(setUsername(username)); } @@ -60,6 +64,16 @@ const Login = () => { } }; + const anonymousLogin = async () => { + const secretKey = generateSecretKey(); + const publicKey = getPublicKey(secretKey); + + dispatch(setPubkey(publicKey)); + window.localStorage.setItem('pubkey', publicKey); + window.localStorage.setItem('seckey', secretKey); + router.push('/'); + }; + return (

Login

@@ -75,6 +89,7 @@ const Login = () => { icon="pi pi-user" className="text-[#f8f8ff] w-[250px] my-4" rounded + onClick={anonymousLogin} />
)