From 62a8207bcdf7db3208210eb4d099d92a0d9691b8 Mon Sep 17 00:00:00 2001 From: daniyal Date: Wed, 14 Aug 2024 14:50:28 +0500 Subject: [PATCH] feat: export nsec --- src/components/ModForm.tsx | 2 +- src/layout/header.tsx | 17 ++++++++----- src/pages/innerMod.tsx | 6 ++--- src/pages/settings.tsx | 52 ++++++++++++++++++++++++++++++++++++-- src/store/reducers/user.ts | 22 ++++++++++++---- 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/components/ModForm.tsx b/src/components/ModForm.tsx index a88f88b..836f5df 100644 --- a/src/components/ModForm.tsx +++ b/src/components/ModForm.tsx @@ -185,7 +185,7 @@ export const ModForm = ({ existingModData }: ModFormProps) => { let hexPubkey: string - if (userState.isAuth && userState.user?.pubkey) { + if (userState.auth && userState.user?.pubkey) { hexPubkey = userState.user.pubkey as string } else { hexPubkey = (await window.nostr?.getPublicKey()) as string diff --git a/src/layout/header.tsx b/src/layout/header.tsx index 77f8a40..7cc2e9b 100644 --- a/src/layout/header.tsx +++ b/src/layout/header.tsx @@ -11,7 +11,7 @@ import { ZapButtons, ZapPresets, ZapQR } from '../components/Zap' import { MetadataController, ZapController } from '../controllers' import { useAppDispatch, useAppSelector } from '../hooks' import { appRoutes } from '../routes' -import { setIsAuth, setUser } from '../store/reducers/user' +import { setAuth, setUser } from '../store/reducers/user' import mainStyles from '../styles//main.module.scss' import navStyles from '../styles/nav.module.scss' import '../styles/popup.css' @@ -29,10 +29,15 @@ export const Header = () => { noBanner: true, onAuth: (npub, opts) => { if (opts.type === 'logout') { - dispatch(setIsAuth(false)) + dispatch(setAuth(null)) dispatch(setUser(null)) } else { - dispatch(setIsAuth(true)) + dispatch( + setAuth({ + method: opts.method, + localNsec: opts.localNsec + }) + ) dispatch( setUser({ npub, @@ -128,7 +133,7 @@ export const Header = () => { Settings - {!userState.isAuth && ( + {!userState.auth && ( { Login )} - {userState.isAuth && userState.user && ( + {userState.auth && userState.user && (
{userState.user.image && ( { setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') - if (userState.isAuth && userState.user?.pubkey) { + if (userState.auth && userState.user?.pubkey) { userHexKey = userState.user.pubkey as string } else { userHexKey = (await window.nostr?.getPublicKey()) as string diff --git a/src/pages/innerMod.tsx b/src/pages/innerMod.tsx index bec80b1..7135d36 100644 --- a/src/pages/innerMod.tsx +++ b/src/pages/innerMod.tsx @@ -230,7 +230,7 @@ const Game = ({ game, author }: GameProps) => {
- {userState.isAuth && userState.user?.pubkey === author && ( + {userState.auth && userState.user?.pubkey === author && ( @@ -1110,7 +1110,7 @@ const ZapMod = ({ modDetails }: ZapModProps) => { setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') - if (userState.isAuth && userState.user?.pubkey) { + if (userState.auth && userState.user?.pubkey) { userHexKey = userState.user.pubkey as string } else { userHexKey = (await window.nostr?.getPublicKey()) as string @@ -1275,7 +1275,7 @@ const ZapSite = () => { setIsLoading(true) setLoadingSpinnerDesc('Getting user pubkey') - if (userState.isAuth && userState.user?.pubkey) { + if (userState.auth && userState.user?.pubkey) { userHexKey = userState.user.pubkey as string } else { userHexKey = (await window.nostr?.getPublicKey()) as string diff --git a/src/pages/settings.tsx b/src/pages/settings.tsx index 726b83e..dc0bf84 100644 --- a/src/pages/settings.tsx +++ b/src/pages/settings.tsx @@ -1,8 +1,11 @@ import { logout } from 'nostr-login' import { Link, useLocation } from 'react-router-dom' +import { toast } from 'react-toastify' import { InputField } from '../components/Inputs' import { ProfileSection } from '../components/ProfileSection' +import { useAppSelector } from '../hooks' import { appRoutes } from '../routes' +import { AuthMethod } from '../store/reducers/user' import '../styles/feed.css' import '../styles/innerPage.css' import '../styles/popup.css' @@ -10,7 +13,7 @@ import '../styles/profile.css' import '../styles/settings.css' import '../styles/styles.css' import '../styles/write.css' -import { useAppSelector } from '../hooks' +import { copyTextToClipboard } from '../utils' export const SettingsPage = () => { const location = useLocation() @@ -135,7 +138,52 @@ const SettingTabs = () => { Admin
- {userState.isAuth && ( + + {userState.auth && + userState.auth.method === AuthMethod.Local && + userState.auth.localNsec && ( +
+ +

+ NOTICE: Make sure you save your private key (nsec) somewhere + safe. +

+
+ + +
+

+ WARNING: Do not sign-out without saving your nsec somewhere + safe. Otherwise, you'll lose access to your "account". +

+
+ )} + + {userState.auth && ( diff --git a/src/store/reducers/user.ts b/src/store/reducers/user.ts index 9968bb1..34fa3b7 100644 --- a/src/store/reducers/user.ts +++ b/src/store/reducers/user.ts @@ -1,13 +1,25 @@ import { createSlice, PayloadAction } from '@reduxjs/toolkit' import { UserProfile } from '../../types/user' +export enum AuthMethod { + Connect = 'connect', + ReadOnly = 'readOnly', + Extension = 'extension', + Local = 'local', + OTP = 'otp' +} +export interface IUserAuth { + method: AuthMethod + localNsec?: string +} + export interface IUserState { - isAuth: boolean + auth: IUserAuth | null user: UserProfile } const initialState: IUserState = { - isAuth: false, + auth: null, user: {} } @@ -15,8 +27,8 @@ export const userSlice = createSlice({ name: 'user', initialState, reducers: { - setIsAuth(state, action: PayloadAction) { - state = { ...state, isAuth: action.payload } + setAuth(state, action: PayloadAction) { + state = { ...state, auth: action.payload } return state }, setUser(state, action: PayloadAction) { @@ -26,6 +38,6 @@ export const userSlice = createSlice({ } }) -export const { setIsAuth, setUser } = userSlice.actions +export const { setAuth, setUser } = userSlice.actions export default userSlice.reducer