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).
- Host: GitHub
- URL: https://github.com/kiwamizamurai/fiber-nextauth
- Owner: kiwamizamurai
- License: mit
- Created: 2025-02-07T04:16:30.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-07T12:58:45.000Z (over 1 year ago)
- Last Synced: 2025-04-02T09:40:22.810Z (about 1 year ago)
- Topics: authjs, fiber, golang, jwt, middleware, nextauth
- Language: Go
- Homepage:
- Size: 108 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fiber-nextauth
[](https://pkg.go.dev/github.com/kiwamizamurai/fiber-nextauth)
[](https://goreportcard.com/report/github.com/kiwamizamurai/fiber-nextauth)
[](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