Merge pull request #55 from hyle-team/feature/add-auth-session
feat: add auth session
This commit is contained in:
commit
f397e096ce
3 changed files with 41 additions and 53 deletions
|
|
@ -6,8 +6,6 @@ import useUpdateUser from '@/hook/useUpdateUser';
|
||||||
import AlertType from '@/interfaces/common/AlertType';
|
import AlertType from '@/interfaces/common/AlertType';
|
||||||
import ConnectButtonProps from '@/interfaces/props/components/UI/ConnectButton/ConnectButtonProps';
|
import ConnectButtonProps from '@/interfaces/props/components/UI/ConnectButton/ConnectButtonProps';
|
||||||
import ZanoWindow from '@/interfaces/common/ZanoWindow';
|
import ZanoWindow from '@/interfaces/common/ZanoWindow';
|
||||||
import { getSavedWalletCredentials, setWalletCredentials } from '@/utils/utils';
|
|
||||||
import { uuid } from 'uuidv4';
|
|
||||||
import Button from '../Button/Button';
|
import Button from '../Button/Button';
|
||||||
|
|
||||||
function ConnectButton(props: ConnectButtonProps) {
|
function ConnectButton(props: ConnectButtonProps) {
|
||||||
|
|
@ -28,41 +26,51 @@ function ConnectButton(props: ConnectButtonProps) {
|
||||||
await (window as unknown as ZanoWindow).zano.request('GET_WALLET_DATA')
|
await (window as unknown as ZanoWindow).zano.request('GET_WALLET_DATA')
|
||||||
).data;
|
).data;
|
||||||
|
|
||||||
if (!walletData?.address) {
|
const walletAddress = walletData?.address;
|
||||||
|
const walletAlias = walletData?.alias;
|
||||||
|
|
||||||
|
if (!walletAddress) {
|
||||||
throw new Error('Companion is offline');
|
throw new Error('Companion is offline');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!walletData?.alias) {
|
if (!walletAlias) {
|
||||||
throw new Error('Alias not found');
|
throw new Error('Alias not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
let nonce = '';
|
if (typeof walletAddress !== 'string' || typeof walletAlias !== 'string') {
|
||||||
let signature = '';
|
throw new Error('Invalid wallet data');
|
||||||
let publicKey = '';
|
|
||||||
|
|
||||||
const existingWallet = getSavedWalletCredentials();
|
|
||||||
|
|
||||||
if (existingWallet) {
|
|
||||||
nonce = existingWallet.nonce;
|
|
||||||
signature = existingWallet.signature;
|
|
||||||
publicKey = existingWallet.publicKey;
|
|
||||||
} else {
|
|
||||||
const generatedNonce = uuid();
|
|
||||||
const signResult = await (window as unknown as ZanoWindow).zano.request(
|
|
||||||
'REQUEST_MESSAGE_SIGN',
|
|
||||||
{ message: generatedNonce },
|
|
||||||
null,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!signResult?.data?.result) {
|
|
||||||
throw new Error('Sign denied');
|
|
||||||
}
|
|
||||||
|
|
||||||
nonce = generatedNonce;
|
|
||||||
signature = signResult.data.result.sig;
|
|
||||||
publicKey = signResult.data.result.pkey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const authRequestRes = await fetch('/api/auth/request-auth', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
address: walletAddress,
|
||||||
|
alias: walletAlias,
|
||||||
|
}),
|
||||||
|
}).then((res) => res.json());
|
||||||
|
|
||||||
|
const authMessage = authRequestRes?.data;
|
||||||
|
|
||||||
|
if (!authRequestRes.success || typeof authMessage !== 'string') {
|
||||||
|
throw new Error('Unknown error during auth request');
|
||||||
|
}
|
||||||
|
|
||||||
|
const signResult = await (window as unknown as ZanoWindow).zano.request(
|
||||||
|
'REQUEST_MESSAGE_SIGN',
|
||||||
|
{ message: authMessage },
|
||||||
|
null,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!signResult?.data?.result) {
|
||||||
|
throw new Error('Sign denied');
|
||||||
|
}
|
||||||
|
|
||||||
|
const signature = signResult.data.result.sig;
|
||||||
|
const publicKey = signResult.data.result.pkey;
|
||||||
|
|
||||||
const result = await fetch('/api/auth', {
|
const result = await fetch('/api/auth', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
|
@ -70,11 +78,11 @@ function ConnectButton(props: ConnectButtonProps) {
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
data: {
|
data: {
|
||||||
alias: walletData.alias,
|
alias: walletAlias,
|
||||||
address: walletData.address,
|
address: walletAddress,
|
||||||
signature,
|
signature,
|
||||||
publicKey,
|
publicKey,
|
||||||
message: nonce,
|
message: authMessage,
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
}).then((res) => res.json());
|
}).then((res) => res.json());
|
||||||
|
|
@ -83,14 +91,6 @@ function ConnectButton(props: ConnectButtonProps) {
|
||||||
throw new Error('Server auth error');
|
throw new Error('Server auth error');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!existingWallet) {
|
|
||||||
setWalletCredentials({
|
|
||||||
publicKey,
|
|
||||||
signature,
|
|
||||||
nonce,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
sessionStorage.setItem('token', result?.data);
|
sessionStorage.setItem('token', result?.data);
|
||||||
|
|
||||||
updateWalletState(dispatch, { ...walletData, connected: true });
|
updateWalletState(dispatch, { ...walletData, connected: true });
|
||||||
|
|
@ -105,7 +105,6 @@ function ConnectButton(props: ConnectButtonProps) {
|
||||||
setAlertState('error');
|
setAlertState('error');
|
||||||
setAlertErrMessage((error as { message: string }).message);
|
setAlertErrMessage((error as { message: string }).message);
|
||||||
setTimeout(() => setAlertState(null), 3000);
|
setTimeout(() => setAlertState(null), 3000);
|
||||||
setWalletCredentials(undefined);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import Button from '@/components/UI/Button/Button';
|
||||||
|
|
||||||
import { useWindowWidth } from '@react-hook/window-size';
|
import { useWindowWidth } from '@react-hook/window-size';
|
||||||
import ConnectButton from '@/components/UI/ConnectButton/ConnectButton';
|
import ConnectButton from '@/components/UI/ConnectButton/ConnectButton';
|
||||||
import { classes, notationToString, setWalletCredentials, shortenAddress } from '@/utils/utils';
|
import { classes, notationToString, shortenAddress } from '@/utils/utils';
|
||||||
import useAdvancedTheme from '@/hook/useTheme';
|
import useAdvancedTheme from '@/hook/useTheme';
|
||||||
|
|
||||||
import { Store } from '@/store/store-reducer';
|
import { Store } from '@/store/store-reducer';
|
||||||
|
|
@ -54,7 +54,6 @@ function Header({ isLg }: { isLg?: boolean }) {
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
sessionStorage.removeItem('token');
|
sessionStorage.removeItem('token');
|
||||||
setWalletCredentials(undefined);
|
|
||||||
updateWalletState(dispatch, null);
|
updateWalletState(dispatch, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -111,16 +111,6 @@ export const localeTimeLeft = (now: number | null, timestamp: number) => {
|
||||||
return `${intToStrFixedLen(hours)}:${intToStrFixedLen(minutes)}:${intToStrFixedLen(seconds)}`;
|
return `${intToStrFixedLen(hours)}:${intToStrFixedLen(minutes)}:${intToStrFixedLen(seconds)}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getSavedWalletCredentials() {
|
|
||||||
const savedWallet = localStorage.getItem('wallet');
|
|
||||||
if (!savedWallet) return undefined;
|
|
||||||
try {
|
|
||||||
return JSON.parse(savedWallet) as WalletCredentials;
|
|
||||||
} catch {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setWalletCredentials(credentials: WalletCredentials | undefined) {
|
export function setWalletCredentials(credentials: WalletCredentials | undefined) {
|
||||||
if (credentials) {
|
if (credentials) {
|
||||||
localStorage.setItem('wallet', JSON.stringify(credentials));
|
localStorage.setItem('wallet', JSON.stringify(credentials));
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue