Skip to content

Commit

Permalink
Merge branch 'main' into zeroes
Browse files Browse the repository at this point in the history
  • Loading branch information
BanEvading authored Feb 24, 2025
2 parents acde19c + 7b9d17f commit 27245d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
43 changes: 18 additions & 25 deletions lib/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import CollectionId from "./client/CollectionId";
import { AdapterUser } from "next-auth/adapters";
import { wait } from "./client/ClientUtils";

const db = getDatabase();

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

const cachedDb = getDatabase();

export const AuthenticationOptions: AuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
providers: [
Expand All @@ -30,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 @@ -62,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 @@ -92,7 +84,7 @@ export const AuthenticationOptions: AuthOptions = {
callbacks: {
async session({ session, user }) {
session.user = await (
await db
await cachedDb
).findObjectById(CollectionId.Users, new ObjectId(user.id));

return session;
Expand All @@ -106,7 +98,10 @@ export const AuthenticationOptions: AuthOptions = {
* For email sign in, runs when the "Sign In" button is clicked (before email is sent).
*/
async signIn({ user }) {
console.log(`User is signing in: ${user.name}, ${user.email}, ${user.id}`);

Analytics.signIn(user.name ?? "Unknown User");
const db = await getDatabase(false);

let typedUser = user as Partial<User>;
if (!typedUser.slug || typedUser._id?.toString() != typedUser.id) {
Expand All @@ -116,9 +111,9 @@ export const AuthenticationOptions: AuthOptions = {
);
let foundUser: User | undefined = undefined;
while (!foundUser) {
foundUser = await (
await db
).findObject(CollectionId.Users, { email: typedUser.email });
foundUser = await db.findObject(CollectionId.Users, {
email: typedUser.email,
});

if (!foundUser) await wait(50);
}
Expand All @@ -128,7 +123,7 @@ export const AuthenticationOptions: AuthOptions = {
typedUser._id = foundUser._id;
typedUser.lastSignInDateTime = new Date();

typedUser = await repairUser(await db, typedUser);
typedUser = await repairUser(db, typedUser);

console.log("User updated:", typedUser._id?.toString());
};
Expand All @@ -142,14 +137,12 @@ export const AuthenticationOptions: AuthOptions = {
today.toDateString()
) {
// We use user.id since user._id strangely doesn't exist on user.
await getDatabase().then((db) =>
db.updateObjectById(
CollectionId.Users,
new ObjectId(typedUser._id?.toString()),
{
lastSignInDateTime: today,
},
),
db.updateObjectById(
CollectionId.Users,
new ObjectId(typedUser._id?.toString()),
{
lastSignInDateTime: today,
},
);
}

Expand Down
14 changes: 9 additions & 5 deletions lib/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ clientPromise = global.clientPromise;

export { clientPromise };

export async function getDatabase(): Promise<DbInterface> {
export async function getDatabase(
useCache: boolean = true,
): Promise<DbInterface> {
if (!global.interface) {
await clientPromise;
const dbInterface = new CachedDbInterface(
new MongoDBInterface(clientPromise),
cacheOptions,
);

const mongo = new MongoDBInterface(clientPromise);

const dbInterface = useCache
? new CachedDbInterface(mongo, cacheOptions)
: mongo;
await dbInterface.init();
global.interface = dbInterface;

Expand Down

0 comments on commit 27245d5

Please sign in to comment.