# Authenticate

Authenticate a user with your server. [Learn more](https://moss.megaeth.com/developers/concepts/authentication)

```ts title="Pure JS"
import { mega } from '@megaeth-labs/wallet-sdk';

const auth = await mega.authenticate();

if (auth.status === 'success' && auth.jwt) {
    // Send the token to your server to verify and start your own session.
    await fetch('/api/login', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ token: auth.jwt }),
    });
}
```

```tsx title="React"
import { useAuthenticate } from '@megaeth-labs/wallet-sdk-react';

export default function SignInButton() {
    const { mutate: authenticate, isPending } = useAuthenticate({
        onSuccess: async (auth) => {
            if (auth.status !== 'success' || !auth.jwt) return;

            // Send the token to your server to verify and start your own session.
            await fetch('/api/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ token: auth.jwt }),
            });
        },
    });

    return (
        <button onClick={() => authenticate()} disabled={isPending}>
            Sign in
        </button>
    );
}
```

```tsx title="Wagmi"
import type { MegaWalletProvider } from '@megaeth-labs/wallet-wagmi-connector';
import { useAccount } from 'wagmi';

export default function SignInButton() {
    const { connector } = useAccount();

    async function authenticate() {
        const provider = (await connector?.getProvider()) as MegaWalletProvider;
        const { jwt } = await provider.authenticate();

        // Send the token to your server to verify and start your own session.
        await fetch('/api/login', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ token: jwt }),
        });
    }

    return (
        <button onClick={authenticate} disabled={!connector}>
            Sign in
        </button>
    );
}
```

```ts
interface AuthenticateResponse {
    status: 'success' | 'cancelled' | 'error';
    error?: string;
    jwt?: string;
}
```

```ts title="server.ts"
// Your app's host as the browser sees it: no scheme, and any port included.
const APP_HOST = 'app.example.com';

// POST /api/login
export async function login(request: Request): Promise<Response> {
    const { token } = await request.json();

    const url = new URL('https://wallet-api.megaeth.com/v1/partner-auth/verify');
    url.searchParams.set('origin', APP_HOST);
    url.searchParams.set('token', token);

    // Fails when the token is invalid, has expired or was issued for a different host.
    const response = await fetch(url);
    if (!response.ok) return new Response('Unauthorised', { status: 401 });

    const { walletAddress } = await response.json();

    // Issue your own session for walletAddress, for example a cookie.
    return Response.json({ walletAddress });
}
```
