From cf1a83006e8dd85b1890e45444b224dd7e29daf2 Mon Sep 17 00:00:00 2001 From: Samuel Andert Date: Mon, 24 Jul 2023 11:51:39 +0200 Subject: [PATCH] Refactor authWithMetamask to enhance modularity and introduce logout functionality --- src/lib/components/Login/Login.svelte | 63 +++++++++++++++ src/lib/services/authWithMetamask/README.md | 78 +++++++++++++++++++ .../authWithMetamask/authWithMetamask.ts | 61 +++++++++++++++ src/routes/+page.svelte | 6 +- 4 files changed, 205 insertions(+), 3 deletions(-) create mode 100644 src/lib/components/Login/Login.svelte create mode 100644 src/lib/services/authWithMetamask/README.md create mode 100644 src/lib/services/authWithMetamask/authWithMetamask.ts diff --git a/src/lib/components/Login/Login.svelte b/src/lib/components/Login/Login.svelte new file mode 100644 index 0000000..b8fdaf2 --- /dev/null +++ b/src/lib/components/Login/Login.svelte @@ -0,0 +1,63 @@ + + + +{#if authSig} +
+

+ Metamask Address: {authSig.address} +

+ +
+ +{:else if error} +
+

{error}

+
+ +{:else} +
+ +
+{/if} diff --git a/src/lib/services/authWithMetamask/README.md b/src/lib/services/authWithMetamask/README.md new file mode 100644 index 0000000..a951e2b --- /dev/null +++ b/src/lib/services/authWithMetamask/README.md @@ -0,0 +1,78 @@ +# authWithMetamask.ts Documentation + +This module provides utility functions for handling authentication signatures (AuthSigs) with MetaMask using the `@lit-protocol/lit-node-client`. + +## Table of Contents +- [authWithMetamask.ts Documentation](#authwithmetamaskts-documentation) + - [Table of Contents](#table-of-contents) + - [Dependencies](#dependencies) + - [Functions](#functions) + - [generateAuthSig](#generateauthsig) + - [getStoredAuthSig](#getstoredauthsig) + - [logout](#logout) + - [Usage](#usage) + +## Dependencies +- `@lit-protocol/lit-node-client`: Used to interact with the Lit Protocol and sign authentication messages. +- `@lit-protocol/constants`: Provides constants used in this module, specifically `LOCAL_STORAGE_KEYS.AUTH_SIGNATURE` for local storage key. + +## Functions + +### generateAuthSig +**Description:** +This asynchronous function initiates the signing of an authentication message using MetaMask and stores the signed message (AuthSig) in the browser's local storage. + +**Returns:** +An `authSig` object containing the signed message if successful. + +**Throws:** +An error with a descriptive message if the signing process fails. + +### getStoredAuthSig +**Description:** +Retrieves the stored AuthSig from the browser's local storage. + +**Returns:** +A parsed `authSig` object if found, or `null` if not. + +### logout +**Description:** +Clears the stored AuthSig from the browser's local storage. + +**Returns:** +`void` + +## Usage + +To use the `authWithMetamask.ts` module in a Svelte component: + +```svelte + +``` \ No newline at end of file diff --git a/src/lib/services/authWithMetamask/authWithMetamask.ts b/src/lib/services/authWithMetamask/authWithMetamask.ts new file mode 100644 index 0000000..31dad90 --- /dev/null +++ b/src/lib/services/authWithMetamask/authWithMetamask.ts @@ -0,0 +1,61 @@ +import { checkAndSignAuthMessage } from '@lit-protocol/lit-node-client'; +import { LOCAL_STORAGE_KEYS } from '@lit-protocol/constants'; + +function createMessage({ text, sender, type }: { text: string; sender: string; type: string }) { + console.log(`[${type}] - ${sender} - ${text}`); +} + +export async function generateAuthSig(): Promise { + createMessage({ + text: "Attempting to generate AuthSig using MetaMask.", + sender: '$lib/services/authWithMetamask/authWithMetamask.ts', + type: 'SYSTEM' + }); + + try { + const authSig = await checkAndSignAuthMessage({ chain: 'xdai' }); + + createMessage({ + text: "AuthSig generated and storing in local storage.", + sender: '$lib/services/authWithMetamask/authWithMetamask.ts', + type: 'SYSTEM' + }); + + localStorage.setItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE, JSON.stringify(authSig)); + return authSig; + } catch (err) { + console.error(err); + + createMessage({ + text: `Failed to sign auth message: ${err.message}`, + sender: '$lib/services/authWithMetamask/authWithMetamask.ts', + type: 'ERROR' + }); + + throw new Error(`Failed to sign auth message: ${err.message}`); + } +} + +export function getStoredAuthSig(): any | null { + createMessage({ + text: "Fetching stored AuthSig from local storage.", + sender: '$lib/services/authWithMetamask/authWithMetamask.ts', + type: 'SYSTEM' + }); + + const storedAuthSig = localStorage.getItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE); + if (storedAuthSig) { + return JSON.parse(storedAuthSig); + } + return null; +} + +export function logout(): void { + createMessage({ + text: "Clearing AuthSig from local storage.", + sender: '$lib/services/authWithMetamask/authWithMetamask.ts', + type: 'SYSTEM' + }); + + localStorage.removeItem(LOCAL_STORAGE_KEYS.AUTH_SIGNATURE); +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 8fd8467..046c999 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -4,7 +4,7 @@ let componentsData = { layout: ` grid-template-areas: - "app", + "login", "main" "footer"; grid-template-rows: 150px 1fr auto; @@ -12,9 +12,9 @@ children: [ { id: 1, - componentName: 'GoogleAuth', + componentName: 'Login', props: {}, - slot: 'app' + slot: 'login' }, { id: 2,