# Wallet Authentication

The Prismon SDK provides seamless wallet authentication using Solana wallets. This guide explains how to authenticate users with solana wallets.

### Prerequisites <a href="#prerequisites" id="prerequisites"></a>

* A Solana wallet (Phantom or Solflare) configured for devnet.
* Prismon SDK installed and configured (see Getting Started).
* @`solana/wallet-adapter-react` and related packages installed.

### Login Authentication Flow <a href="#authentication-flow" id="authentication-flow"></a>

{% stepper %}
{% step %}
**Fetch a Challenge**: Call `users.getChallenge(publicKey)` with the wallet's public key to get a `{ challenge, challengeId }`
{% endstep %}

{% step %}
&#x20;**Sign the Challenge**: Use the wallet `signMessage`to sign the challenge.
{% endstep %}

{% step %}
**Login**: Send the signed challenge, public key, and challenge ID `users.loginWallet`to authenticate and receive a JWT.
{% endstep %}
{% endstepper %}

> Note: Getting a user challenge has been handled by the SDK, there will be no need to implement that, except you want to, feel free to dive in.

#### Import statements&#x20;

```typescript
import { useWallet } from '@solana/wallet-adapter-react';
import { createPrismonClient } from '@prismon/sdk';
import { useState } from 'react';
```

#### Hooks

```typescript
const { publicKey, signMessage, connected, signTransaction } = useWallet();
const [message, setMessage] = useState<string>('');
```

#### Destructure connected wallet publicKey and signMessage&#x20;

```typescript
  const wallet = {
    publicKey: publicKey,
    signMessage: signMessage,
  };
```

#### Sign up with Wallet Method from Prismon

```typescript
 const responsee = await client.users.signUpWallet(
        wallet
      );
```

#### Login method from Prismon

```typescript
 const responsee = await client.users.loginWallet(
        wallet,
      );
```

{% hint style="warning" %}
Remember to validate
{% endhint %}

#### At the end, you should have something like this below for Signup.

```typescript
  const handleSignup = async () => {
    if (!connected || !publicKey) {
      alert("Please connect your wallet first");
      return;
    }
    if (!signMessage) {
      alert("Wallet does not support signing messages", "error");
      return;
    }

     const responsee = await client.users.signUpWallet(
        wallet,
      );

      if (response.success) {
        //Do something
      } else {
        //Do something
      }
    
  };
```

#### At the end, you should have something like this below for Login.

```typescript
  const handleLogin = async () => {
    if (!connected || !publicKey) {
      alert("Please connect your wallet first");
      return;
    }
    if (!signMessage) {
      alert("Wallet does not support signing messages");
      return;
    }

     const responsee = await client.users.loginWallet(
        wallet
      );

      if (response.success) {
        //Do something
        if (response.data.token) {
          client.setJwtToken(response.data.token);
        }
      } else {
        //Do something
      }
    
  };
```

### Response

#### On successful login, `loginWallet` returns:

```typescript
{
  "success": true,
  "data": {
    "userId": "string",
    "token": "jwt-token",
    "succeeded": true,
    "message": "Login successful"
  }
}
```

Store the token using `client.setJwtToken(token)` for authenticated API calls.

### Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* **signMessage Error**
* * Ensure your wallet (e.g., Phantom, Solflare) supports signMessage.
  * Try Phantom if Solflare fails.
  * Update your wallet to the latest version.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lucky-israel.gitbook.io/prismon-docs/features/editor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
