Skip to content

Commit

Permalink
fix(Auth): fix server-side usage and dependency errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeaturner committed Feb 20, 2025
1 parent 4f3c6e2 commit 1e3248c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
35 changes: 26 additions & 9 deletions lib/auth-functions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"use server";
import { verifyPassword } from "@/utils/auth";
import { addCourseToUser, createCourseIfNotExists, createExternalUser, getUser, getUserById } from "@/lib/auth";
import {
addCourseToUser,
createCourseIfNotExists,
createExternalUser,
getUser,
getUserById,
} from "@/lib/auth";
import connectDB from "./database";
import { ActionResult } from "./types";
import { lucia } from "./auth";
import { createLuciaSession, createLuciaSessionCookie } from "./auth";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import * as jose from "jose";
Expand Down Expand Up @@ -44,8 +50,11 @@ export async function fallbackLogin(
};
}

const session = await lucia.createSession(existingUser.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);
const session = await createLuciaSession({
userId: existingUser.id,
attributes: {},
});
const sessionCookie = await createLuciaSessionCookie(session.id);

cookies().set(
sessionCookie.name,
Expand All @@ -61,9 +70,14 @@ export async function adaptLogin(raw: string): Promise<boolean> {
const secret = new TextEncoder().encode(process.env.CLIENT_AUTH_SECRET);

const { payload, protectedHeader } = await jose.jwtVerify(raw, secret);
if (!payload.user_id || !payload.role || !payload.course_id) throw new Error("Invalid payload");
if (!payload.user_id || !payload.role || !payload.course_id)
throw new Error("Invalid payload");

const { user_id, role, course_id } = payload as { user_id: number; role: 2 | 3, course_id: number };
const { user_id, role, course_id } = payload as {
user_id: number;
role: 2 | 3;
course_id: number;
};

await connectDB();

Expand All @@ -78,12 +92,15 @@ export async function adaptLogin(raw: string): Promise<boolean> {
}

await createCourseIfNotExists(course_id);
if(existingUser.courses.indexOf(course_id.toString()) === -1) {
if (existingUser.courses.indexOf(course_id.toString()) === -1) {
await addCourseToUser(existingUser.id, course_id);
}

const session = await lucia.createSession(existingUser.id, {});
const sessionCookie = lucia.createSessionCookie(session.id);
const session = await createLuciaSession({
userId: existingUser.id,
attributes: {},
});
const sessionCookie = await createLuciaSessionCookie(session.id);

cookies().set(
sessionCookie.name,
Expand Down
21 changes: 18 additions & 3 deletions lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use server";
import { Lucia, TimeSpan, type Session, type User } from "lucia";
import { MongodbAdapter } from "@lucia-auth/adapter-mongodb";
import connectDB from "./database";
Expand All @@ -8,10 +9,15 @@ import session from "./models/session";
import { Types } from "mongoose";
import adaptCourses from "./models/adaptCourses";
import ADAPTCourseConnector from "./ADAPTCourseConnector";
globalThis.crypto = webcrypto as Crypto;

type CreateLuciaSessionProps = {
userId: string;
attributes: {};
options?: { sessionId?: string | undefined };
};

// Initialize Lucia after adapter is available
export const lucia = new Lucia(
const lucia = new Lucia(
// @ts-ignore
new MongodbAdapter(session.collection, user.collection),
{
Expand All @@ -33,6 +39,15 @@ export const lucia = new Lucia(
}
);

export async function createLuciaSession(props: CreateLuciaSessionProps) {
await connectDB();
return lucia.createSession(props.userId, props.attributes, props.options);
}

export async function createLuciaSessionCookie(sessionId: string) {
return lucia.createSessionCookie(sessionId);
}

export const validateRequest = async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
> => {
Expand Down Expand Up @@ -222,7 +237,7 @@ export const addCourseToUser = async (
} catch (err) {
console.error(err);
}
}
};

declare module "lucia" {
interface Register {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1e3248c

Please sign in to comment.