Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/brendon1555/authjs-mongoose-adapter

An adapter for Auth.js/NextAuth.js to allow you to connect to a MongoDB using Mongoose.
https://github.com/brendon1555/authjs-mongoose-adapter

authjs mongodb mongoose next-auth nextjs

Last synced: 3 months ago
JSON representation

An adapter for Auth.js/NextAuth.js to allow you to connect to a MongoDB using Mongoose.

Awesome Lists containing this project

README

        











Mongoose Adapter - NextAuth.js / Auth.js




TypeScript


npm


Downloads


GitHub Stars

---

> [!WARNING]
>
> This adapter cannot support Next.js middleware with `next-auth` until Mongoose supports the Next.js Edge Runtime. _Or_, Next.js adds Node.js `net` API support to middlewares. You _CAN_ still generally use it with Next.js however.
> See: [Mongoose docs](https://mongoosejs.com/docs/nextjs.html#:~:text=Mongoose%20does%20not%20currently%20support%20Next.js%20Edge%20Runtime.)

## Getting Started

1. Install Auth.js (`@auth/express`, `@auth/sveltekit`, `next-auth`, etc.), `mongoose` and `@brendon1555/authjs-mongoose-adapter`:

```bash
npm install @brendon1555/authjs-mongoose-adapter mongoose @auth/express
```

2. Add a MongoDB connection string to your environment variables:

```env
MONGODB_URI=mongodb://localhost:27017/my-database
```

3. Add this adapter to your auth config:

1. Express

```js
import { ExpressAuth } from "@auth/express";
import { MongooseAdapter } from "@brendon1555/authjs-mongoose-adapter";

const app = express();

app.use(
"/auth/*",
ExpressAuth({
adapter: MongooseAdapter(process.env.MONGODB_URI),
...
})
);
```

2. SvelteKit

```js
import { SvelteKitAuth } from "@auth/sveltekit";
import { MongooseAdapter } from "@brendon1555/authjs-mongoose-adapter"

export const { handle, signIn, signOut } = SvelteKitAuth({
adapter: MongoDBAdapter(process.env.MONGODB_URI),
...
});
```

3. Next.js (Middleware not supported yet)

```js
import NextAuth from "next-auth"
import { MongooseAdapter } from "@brendon1555/authjs-mongoose-adapter"

export default NextAuth({
adapter: MongooseAdapter(process.env.MONGODB_URI),
...
})
```

## License

ISC