remove usLogin

This commit is contained in:
austinkelsay 2024-08-07 17:17:51 -05:00
parent 19e0a03473
commit fea5a7b76c
2 changed files with 0 additions and 125 deletions

View File

@ -1,8 +1,6 @@
import React from 'react';
import { useLogin } from '@/hooks/useLogin';
const Layout = ({ children }) => {
useLogin();
return (
<div>

View File

@ -1,123 +0,0 @@
import { useCallback, useEffect } from 'react';
import { useRouter } from 'next/router';
import axios from 'axios';
import { generateSecretKey, getPublicKey } from 'nostr-tools';
import { findKind0Fields } from "@/utils/nostr";
import { useToast } from './useToast';
import { useNDKContext } from "@/context/NDKContext";
export const useLogin = () => {
const router = useRouter();
const { showToast } = useToast();
const ndk = useNDKContext();
// Attempt Auto Login on render
useEffect(() => {
const autoLogin = async () => {
const user = window.localStorage.getItem('user');
const publicKey = JSON.parse(user)?.pubkey;
if (!publicKey) return;
try {
const response = await axios.get(`/api/users/${publicKey}`);
console.log('auto login response:', response);
if (response.status === 200 && response.data) {
window.localStorage.setItem('user', JSON.stringify(response.data));
} else if (response.status === 204) {
// User not found, create a new user
const author = await ndk.getUser({ pubkey: publicKey });
const kind0 = await author.fetchProfile();
console.log('kind0:', kind0);
let fields = null;
if (kind0) {
fields = await findKind0Fields(kind0);
}
const payload = { pubkey: publicKey, ...fields };
try {
const createUserResponse = await axios.post(`/api/users`, payload);
console.log('create user response:', createUserResponse);
if (createUserResponse.status === 201) {
window.localStorage.setItem('user', JSON.stringify(createUserResponse.data));
} else {
console.error('Error creating user:', createUserResponse);
}
} catch (createError) {
console.error('Error creating user:', createError);
showToast('error', 'Error Creating User', 'Failed to create user');
}
}
} catch (error) {
console.error('Error during auto login:', error);
}
};
autoLogin();
}, [ndk, showToast]);
const nostrLogin = useCallback(async () => {
if (!window || !window.nostr) {
showToast('error', 'Nostr Unavailable', 'Nostr is not available');
return;
}
const publicKey = await window.nostr.getPublicKey();
if (!publicKey) {
showToast('error', 'Public Key Error', 'Failed to obtain public key');
return;
}
try {
const response = await axios.get(`/api/users/${publicKey}`);
let userData;
if (response.status === 204) {
// User not found, create a new user
const author = await ndk.getUser({ pubkey: publicKey });
const kind0 = await author.fetchProfile();
let fields = {};
if (kind0) {
fields = await findKind0Fields(kind0);
}
const payload = { pubkey: publicKey, ...fields };
const createUserResponse = await axios.post(`/api/users`, payload);
if (createUserResponse.status !== 201) {
throw new Error('Failed to create user');
}
userData = createUserResponse.data;
} else {
userData = response.data;
}
window.localStorage.setItem('user', JSON.stringify(userData));
router.push('/').then(() => window.location.reload());
} catch (error) {
console.error('Error during login:', error);
showToast('error', 'Login Error', error.message || 'Failed to log in');
}
}, [router, showToast, ndk]);
const anonymousLogin = useCallback(() => {
try {
const secretKey = generateSecretKey();
const publicKey = getPublicKey(secretKey);
// need to fix with byteToHex
const hexSecretKey = secretKey.toString('hex');
window.localStorage.setItem('user', JSON.stringify({ pubkey: publicKey, secretKey: hexSecretKey }));
router.push('/').then(() => window.location.reload());
} catch (error) {
console.error('Error during anonymous login:', error);
showToast('error', 'Error Logging In', 'Failed to log in');
}
}, [router, showToast]);
return { nostrLogin, anonymousLogin };
};