78 lines
1.9 KiB
Markdown
78 lines
1.9 KiB
Markdown
|
# 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
|
||
|
<script>
|
||
|
import { onMount } from 'svelte';
|
||
|
import {
|
||
|
generateAuthSig,
|
||
|
getStoredAuthSig,
|
||
|
logout
|
||
|
} from '$lib/services/authWithMetamask/authWithMetamask';
|
||
|
|
||
|
let authSig = null;
|
||
|
let error = null;
|
||
|
|
||
|
async function handleAuth() {
|
||
|
try {
|
||
|
authSig = await generateAuthSig();
|
||
|
} catch (err) {
|
||
|
error = err.message;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function handleLogout() {
|
||
|
logout();
|
||
|
authSig = null;
|
||
|
}
|
||
|
|
||
|
onMount(() => {
|
||
|
authSig = getStoredAuthSig();
|
||
|
});
|
||
|
</script>
|
||
|
```
|