# fastify-siwe

> [Sign In with Ethereum](https://login.xyz) middleware for [Fastify](https://fastify.io).

Latest version **0.1.2** (published 2023-07-21) · 0 weekly downloads

## Install

```sh
npm install fastify-siwe
pnpm add fastify-siwe
yarn add fastify-siwe
bun add fastify-siwe
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2023-07-21 |
| First published | 2022-04-23 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 125.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | ethworks, marik_d |
| Keywords | fastify, Sign in with Ethereum, siwe, ethereum, auth |

## Links

- npm: https://www.npmjs.com/package/fastify-siwe
- Repository: https://github.com/vanruch/fastify-siwe
- Homepage: https://github.com/vanruch/fastify-siwe#readme
- Issues: https://github.com/vanruch/fastify-siwe/issues
- npm.io page: https://npm.io/package/fastify-siwe

## Dependencies (3)

- [siwe](https://npm.io/package/siwe.md) 2.0.5
- [ethers](https://npm.io/package/ethers.md) 5.5.1
- [fastify-plugin](https://npm.io/package/fastify-plugin.md) 4.0.0

## Alternatives

- [@clerk/clerk-expo](https://npm.io/package/@clerk/clerk-expo.md) — 133.6K weekly downloads
- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@oxyhq/services](https://npm.io/package/@oxyhq/services.md) — 2.3K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads

## Recent versions

- 0.1.2 (latest) — 2023-07-21
- 0.1.2-dev.87d3cbe (dev) — 2023-07-21
- 0.1.2-dev.ece4da4 — 2023-07-21
- 0.1.2-dev.fa2391b — 2022-11-07
- 0.1.2-dev.1edc026 — 2022-08-30
- 0.1.2-dev.bc26100 — 2022-08-25
- 0.1.1-dev.0f85b0d — 2022-08-12
- 0.1.1 — 2022-08-08
- 0.1.0 — 2022-04-23

## README

# fastify-siwe

[Sign In with Ethereum](https://login.xyz) middleware for [Fastify](https://fastify.io).

## How it works

![diagram](./images/sequence.drawio.svg)

1. The frontend request a nonce from the backend server. The nonce must be generated and verified on the backend to protect against replay attacks.
2. The dapp signs the message with user's wallet and saves the signed message locally.
3. An `Authentication` header is included in every request with the signed message.

### Header format

```
Authorization: Bearer <json encoded message>
```

Example of the signed message:

```json
{
  "signature":"0xafa5d63362c63b0da57f152afb0fbd296abd1aec046355927f2c34e26ab67b1a58ce34bcd609d312293391005c25780a87c110cfb6374a747184a35b047b08d91c",
  "message":{
    "domain":"localhost:3002",
    "address":"0x2C6e1d8a2E457c5D79fAD2c9F2f0f463e0Df5376",
    "statement":"Sign in with Ethereum to the app.",
    "uri":"http://localhost:3002",
    "version":"1",
    "chainId":1,
    "nonce":"JGiZrkbZ2uwUXl5yl",
    "issuedAt":"2022-04-23T17:25:20.427Z"
  }
}
```

## Installation

```
npm add fastify-siwe

yarn add fastify-siwe

pnpm add fastify-siwe
```

## Usage

Register the middleware:

```typescript
import { signInWithEthereum } from 'fastify-siwe'

fastify.register(signInWithEthereum())
```

All requests come decorated with `req.siwe` object.
`req.siwe.session` will contain be present if the request is authenticated.

```typescript
fastify.get(
  '/siwe/me',
  {},
  async function handler(
    this: FastifyInstance,
    req: FastifyRequest,
    reply,
  ) {
    if (!req.siwe.session) {
      reply.status(401).send()
      return
    }

    console.log('address =', req.siwe.session.address)
  },
)
```

Generating a new nonce:

```typescript
const nonce = await req.siwe.generateNonce()
```

> Checkout the full example at `packages/example`

### Implementing a custom store

By default sessions are stored in memory. Session data will be lost on server restart.

To preserve sessions you can implement a custom store backed by a database.

```typescript
import { SessionStore, signInWithEthereum } from 'fastify-siwe'

class MyStore implements SessionStore {
  async get(nonce: string): Promise<StoredSession | undefined> {
    // Fetch from database
  }

  public async save(session: StoredSession){
    // Save to database
  }


  async remove(nonce: string): Promise<void> {
    // Delete from database
  }
}

fastify.register(signInWithEthereum({ store: new MyStore() }))
```

---
_Source: https://npm.io/package/fastify-siwe · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
