An open API service indexing awesome lists of open source software.

https://github.com/kiwamizamurai/fiber-nextauth

A Fiber middleware for validating JWT tokens generated by Auth.js (NextAuth).
https://github.com/kiwamizamurai/fiber-nextauth

authjs fiber golang jwt middleware nextauth

Last synced: 9 months ago
JSON representation

A Fiber middleware for validating JWT tokens generated by Auth.js (NextAuth).

Awesome Lists containing this project

README

          

# fiber-nextauth

[![Go Reference](https://pkg.go.dev/badge/github.com/kiwamizamurai/fiber-nextauth.svg)](https://pkg.go.dev/github.com/kiwamizamurai/fiber-nextauth)
[![Go Report Card](https://goreportcard.com/badge/github.com/kiwamizamurai/fiber-nextauth)](https://goreportcard.com/report/github.com/kiwamizamurai/fiber-nextauth)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

A Fiber middleware for validating JWT tokens generated by Auth.js (NextAuth). Perfect for projects combining Next.js frontend with Fiber backend.

> [!CAUTION]
> This module was implemented for learning purposes. While it works, it has not been thoroughly security audited. Use in production at your own risk.

## Installation

```bash
go get github.com/kiwamizamurai/fiber-nextauth
```

## Quick Start

### Basic Usage

```go
package main

import (
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v5"
"github.com/kiwamizamurai/fiber-nextauth/pkg"
)

func main() {
app := fiber.New()

// For Auth.js v5
auth := nextauthjwt.New(nextauthjwt.DefaultConfig())
// For NextAuth.js v4
// auth := nextauthjwt.New(nextauthjwt.NewV4Config())

// Protected route
app.Get("/protected", auth.Middleware(), func(c *fiber.Ctx) error {
claims := c.Locals("user").(jwt.MapClaims)
return c.JSON(fiber.Map{
"message": "Protected route accessed successfully!",
"user": fiber.Map{
"name": claims["name"],
"email": claims["email"],
},
})
})

app.Listen(":3000")
}
```

### Custom Configuration

```go
cfg := nextauthjwt.DefaultConfig()
cfg.Secret = os.Getenv("NEXTAUTH_SECRET")
cfg.SecureCookie = true
cfg.CSRFEnabled = true
cfg.CheckExpiry = true

auth := nextauthjwt.New(cfg)
```

## Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `NEXTAUTH_SECRET` | Secret key for JWT validation | - |
| `NEXTAUTH_URL` | URL to determine secure cookie usage | - |

## Examples

The [examples](./examples) directory contains complete implementation examples:

- `nextjs/`: Next.js v14 example with NextAuth.js v4
- `authjs/`: Next.js v14 example with Auth.js v5
- `fiber/`: Go Fiber backend example
- `compose.yml`: Docker Compose setup for running all examples

### Running Examples

With Docker Compose:
```bash
cd examples
docker compose up
```

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## Reference
1. https://www.reddit.com/r/nextjs/comments/179shhd/next_auth_with_seprate_backend_golang/
2. https://github.com/nextauthjs/next-auth/blob/main/packages/core/src/jwt.ts
3. https://github.com/TCatshoek/fastapi-nextauth-jwt