@next-auth/mongodb-adapter
Advanced tools
Comparing version 0.0.2-next.285 to 0.0.2-next.298
@@ -0,9 +1,13 @@ | ||
import { ObjectId } from "mongodb"; | ||
import type { Adapter } from "next-auth/adapters"; | ||
import type * as MongoDB from "mongodb"; | ||
export declare const collections: { | ||
Users: string; | ||
Accounts: string; | ||
Sessions: string; | ||
VerificationTokens: string; | ||
}; | ||
import type { MongoClient } from "mongodb"; | ||
export interface MongoDBAdapterOptions { | ||
collections?: { | ||
Users?: string; | ||
Accounts?: string; | ||
Sessions?: string; | ||
VerificationTokens?: string; | ||
}; | ||
} | ||
export declare const defaultCollections: Required<Required<MongoDBAdapterOptions>["collections"]>; | ||
export declare const format: { | ||
@@ -16,5 +20,3 @@ /** Takes a mongoDB object and returns a plain old JavaScript object */ | ||
/** Converts from string to ObjectId */ | ||
export declare function _id(hex?: string): MongoDB.ObjectId; | ||
export declare function MongoDBAdapter(options: { | ||
db: MongoDB.Db; | ||
}): Adapter; | ||
export declare function _id(hex?: string): ObjectId; | ||
export declare function MongoDBAdapter(client: Promise<MongoClient>, options?: MongoDBAdapterOptions): Adapter; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MongoDBAdapter = exports._id = exports.format = exports.collections = void 0; | ||
exports.MongoDBAdapter = exports._id = exports.format = exports.defaultCollections = void 0; | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
const mongodb_1 = require("mongodb"); | ||
exports.collections = { | ||
exports.defaultCollections = { | ||
Users: "users", | ||
@@ -53,19 +54,23 @@ Accounts: "accounts", | ||
exports._id = _id; | ||
function MongoDBAdapter(options) { | ||
const { db: m } = options; | ||
function MongoDBAdapter(client, options = {}) { | ||
const { collections } = options; | ||
const { from, to } = exports.format; | ||
const { Users, Accounts, Sessions, VerificationTokens } = { | ||
Users: m.collection(exports.collections.Users), | ||
Accounts: m.collection(exports.collections.Accounts), | ||
Sessions: m.collection(exports.collections.Sessions), | ||
VerificationTokens: m.collection(exports.collections.VerificationTokens), | ||
}; | ||
const db = (async () => { | ||
const _db = (await client).db(); | ||
const c = { ...exports.defaultCollections, ...collections }; | ||
return { | ||
U: _db.collection(c.Users), | ||
A: _db.collection(c.Accounts), | ||
S: _db.collection(c.Sessions), | ||
V: _db.collection(c === null || c === void 0 ? void 0 : c.VerificationTokens), | ||
}; | ||
})(); | ||
return { | ||
async createUser(data) { | ||
const user = to(data); | ||
await Users.insertOne(user); | ||
await (await db).U.insertOne(user); | ||
return from(user); | ||
}, | ||
async getUser(id) { | ||
const user = await Users.findOne({ _id: _id(id) }); | ||
const user = await (await db).U.findOne({ _id: _id(id) }); | ||
if (!user) | ||
@@ -76,3 +81,3 @@ return null; | ||
async getUserByEmail(email) { | ||
const user = await Users.findOne({ email }); | ||
const user = await (await db).U.findOne({ email }); | ||
if (!user) | ||
@@ -83,6 +88,6 @@ return null; | ||
async getUserByAccount(provider_providerAccountId) { | ||
const account = await Accounts.findOne(provider_providerAccountId); | ||
const account = await (await db).A.findOne(provider_providerAccountId); | ||
if (!account) | ||
return null; | ||
const user = await Users.findOne({ _id: account.userId }); | ||
const user = await (await db).U.findOne({ _id: account.userId }); | ||
if (!user) | ||
@@ -93,3 +98,3 @@ return null; | ||
async updateUser(data) { | ||
const { value: user } = await Users.findOneAndUpdate({ _id: _id(data.id) }, { $set: data }); | ||
const { value: user } = await (await db).U.findOneAndUpdate({ _id: _id(data.id) }, { $set: data }); | ||
return from(user); | ||
@@ -99,6 +104,7 @@ }, | ||
const userId = _id(id); | ||
const m = await db; | ||
await Promise.all([ | ||
m.collection(exports.collections.Accounts).deleteMany({ userId }), | ||
m.collection(exports.collections.Sessions).deleteMany({ userId }), | ||
m.collection(exports.collections.Users).deleteOne({ _id: userId }), | ||
m.A.deleteMany({ userId }), | ||
m.S.deleteMany({ userId }), | ||
m.U.deleteOne({ _id: userId }), | ||
]); | ||
@@ -108,14 +114,14 @@ }, | ||
const account = to(data); | ||
await Accounts.insertOne(account); | ||
await (await db).A.insertOne(account); | ||
return account; | ||
}, | ||
async unlinkAccount(provider_providerAccountId) { | ||
const { value: account } = await Accounts.findOneAndDelete(provider_providerAccountId); | ||
const { value: account } = await (await db).A.findOneAndDelete(provider_providerAccountId); | ||
return from(account); | ||
}, | ||
async getSessionAndUser(sessionToken) { | ||
const session = await Sessions.findOne({ sessionToken }); | ||
const session = await (await db).S.findOne({ sessionToken }); | ||
if (!session) | ||
return null; | ||
const user = await Users.findOne({ _id: session.userId }); | ||
const user = await (await db).U.findOne({ _id: session.userId }); | ||
if (!user) | ||
@@ -130,11 +136,11 @@ return null; | ||
const session = to(data); | ||
await Sessions.insertOne(session); | ||
await (await db).S.insertOne(session); | ||
return from(session); | ||
}, | ||
async updateSession(data) { | ||
const { value: session } = await Sessions.findOneAndUpdate({ sessionToken: data.sessionToken }, { $set: data }); | ||
const { value: session } = await (await db).S.findOneAndUpdate({ sessionToken: data.sessionToken }, { $set: data }); | ||
return from(session); | ||
}, | ||
async deleteSession(sessionToken) { | ||
const { value: session } = await Sessions.findOneAndDelete({ | ||
const { value: session } = await (await db).S.findOneAndDelete({ | ||
sessionToken, | ||
@@ -145,7 +151,7 @@ }); | ||
async createVerificationToken(data) { | ||
await VerificationTokens.insertOne(to(data)); | ||
await (await db).V.insertOne(to(data)); | ||
return data; | ||
}, | ||
async useVerificationToken(identifier_token) { | ||
const { value: verificationToken } = await VerificationTokens.findOneAndDelete(identifier_token); | ||
const { value: verificationToken } = await (await db).V.findOneAndDelete(identifier_token); | ||
if (!verificationToken) | ||
@@ -152,0 +158,0 @@ return null; |
{ | ||
"name": "@next-auth/mongodb-adapter", | ||
"version": "0.0.2-next.285+671609e", | ||
"version": "0.0.2-next.298+f84c048", | ||
"description": "mongoDB adapter for next-auth.", | ||
@@ -39,3 +39,3 @@ "homepage": "https://next-auth.js.org", | ||
}, | ||
"gitHead": "671609e55b98646c83b1270f9674b464f2e5b70f" | ||
"gitHead": "f84c0486775af8d36f3756e11fbf2f443f398ad0" | ||
} |
<p align="center"> | ||
<br/> | ||
<a href="https://next-auth.js.org" target="_blank"><img height="64px" src="https://next-auth.js.org/img/logo/logo-sm.png" /></a> <img height="64px" src="./logo.svg" /> | ||
<h3 align="center"><b>mongoDB Adapter</b> - NextAuth.js</h3> | ||
<h3 align="center"><b>MongoDB Adapter</b> - NextAuth.js</h3> | ||
<p align="center"> | ||
@@ -17,6 +17,4 @@ Open Source. Full Stack. Own Your Data. | ||
This is the mongoDB Adapter for [`next-auth`](https://next-auth.js.org). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package. | ||
This is the MongoDB Adapter for [`next-auth`](https://next-auth.js.org). This package can only be used in conjunction with the primary `next-auth` package. It is not a standalone package. | ||
You can find the mongoDB schema in the docs at [next-auth.js.org/adapters/mongodb](https://next-auth.js.org/adapters/mongodb). | ||
## Getting Started | ||
@@ -77,12 +75,6 @@ | ||
// https://next-auth.js.org/configuration/options | ||
export default async function auth(req, res) { | ||
return await NextAuth(req, res, { | ||
adapter: MongoDBAdapter({ | ||
db: (await clientPromise).db("your-database") | ||
}), | ||
// https://next-auth.js.org/configuration/providers | ||
providers: [], | ||
... | ||
}) | ||
} | ||
export default NextAuth({ | ||
adapter: MongoDBAdapter(clientPromise), | ||
... | ||
}) | ||
``` | ||
@@ -89,0 +81,0 @@ |
11185
178
87