Skip to content

Commit

Permalink
Reduce getDatabase calls in Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
renatodellosso committed Feb 24, 2025
1 parent 790097a commit b78ba52
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions lib/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { wait } from "./client/ClientUtils";

const adapter = MongoDBAdapter(clientPromise, { databaseName: process.env.DB });

const cachedDb = getDatabase();

export const AuthenticationOptions: AuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
providers: [
Expand All @@ -28,11 +30,7 @@ export const AuthenticationOptions: AuthOptions = {
profile.email,
profile.picture,
false,
await GenerateSlug(
await getDatabase(),
CollectionId.Users,
profile.name,
),
await GenerateSlug(await cachedDb, CollectionId.Users, profile.name),
[],
[],
);
Expand Down Expand Up @@ -60,11 +58,7 @@ export const AuthenticationOptions: AuthOptions = {
profile.email,
profile.picture,
false,
await GenerateSlug(
await getDatabase(),
CollectionId.Users,
profile.name,
),
await GenerateSlug(await cachedDb, CollectionId.Users, profile.name),
[],
[],
profile.sub,
Expand All @@ -90,7 +84,7 @@ export const AuthenticationOptions: AuthOptions = {
callbacks: {
async session({ session, user }) {
session.user = await (
await getDatabase()
await cachedDb
).findObjectById(CollectionId.Users, new ObjectId(user.id));

return session;
Expand Down Expand Up @@ -141,13 +135,13 @@ export const AuthenticationOptions: AuthOptions = {
today.toDateString()
) {
// We use user.id since user._id strangely doesn't exist on user.
db.updateObjectById(
CollectionId.Users,
new ObjectId(typedUser._id?.toString()),
{
lastSignInDateTime: today,
},
);
db.updateObjectById(
CollectionId.Users,
new ObjectId(typedUser._id?.toString()),
{
lastSignInDateTime: today,
},
);
}

new ResendUtils().createContact(typedUser as User);
Expand Down

0 comments on commit b78ba52

Please sign in to comment.