Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"tasks": {
"runDev": {
"rundev": {
"description": "Run the local development build",
"command": "deno run -A --watch --unstable-cron --env-file=.env.local main.ts"
},
"runProd": {
"runprod": {
"description": "Run the local production build",
"command": "deno run -A --watch --unstable-cron --env-file=.env.production main.ts"
},
Expand All @@ -13,29 +13,29 @@
"description": "Run the tests",
"command": "deno test -A --unstable-kv --no-check"
},
"testAuth": {
"testauth": {
"description": "Run only authentication tests",
"command": "deno test -A --unstable-kv --no-check tests/auth-test.ts"
},
// Resend API
"resendCheck": {
"resendcheck": {
"description": "Tests the Resend API",
"command": "deno run -A --watch --unstable-cron --env-file=.env.local ./utils/emails/sendTest.ts"
},
// Neo4j API
"n4jSeedL": {
"n4jseedl": {
"description": "Seed the local instance of neo4j",
"command": "deno run -A --env-file=.env.local queries/seed.ts"
},
"n4jSeedP": {
"n4jseedp": {
"description": "Seed the production instance of neo4j",
"command": "deno run -A --env-file=.env.production queries/seed.ts"
},
"n4jResetL": {
"n4jresetl": {
"description": "Reset the local instance of neo4j",
"command": "deno run -A --env-file=.env.local queries/reset.ts"
},
"n4jResetP": {
"n4jresetp": {
"description": "Reset the production instance of neo4j",
"command": "deno run -A --env-file=.env.production queries/reset.ts"
},
Expand Down
131 changes: 131 additions & 0 deletions routes/authRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Router } from "oak";
import { createClient } from "supabase";
import { verifyUser } from "utils/auth/authMiddleware.ts";

const router = new Router();
const routes: string[] = [];

router.post("/signin/magic-link", async (ctx) => {
console.group(`|============ Sign In Request ============|`);
const body = await ctx.request.body.json();
const name = body.name;
const email = body.email;

if (!email) {
ctx.response.status = 400;
ctx.response.body = { error: "Email is required" };
return;
}

try {
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
const supabase = createClient(supabaseUrl, supabaseKey);

const { data, error } = await supabase.auth.signInWithOtp({
email,
options: {
emailRedirectTo: "http://localhost:8080/auth/callback",
// [ ] tdHi: Get callback URL from Alex
data: {
name: name
}
},
});

console.group(`|====== Sign In Result ======|`);
if (error) {
console.info(`| Error`);
console.error(error);

ctx.response.status = 500;
ctx.response.body = { error: error.message };
} else {
console.info(`| Success`);
console.log(data);

ctx.response.status = 200;
ctx.response.body = { message: "Magic link sent", data };
}
console.groupEnd();
} catch (error) {
console.error(error);
ctx.response.status = 500;
ctx.response.body = { error };
}

console.groupEnd();
});
routes.push("signin/magic-link");

router.get("/auth/callback", async (ctx) => {
console.group("|============ Auth Callback ============|");

try {
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
const supabase = createClient(supabaseUrl, supabaseKey);

// Get the access_token and refresh_token from URL params
const params = new URLSearchParams(ctx.request.url.search);
const access_token = params.get("access_token");
const refresh_token = params.get("refresh_token");

if (!access_token || !refresh_token) {
throw new Error("No tokens provided in callback");
}

// Get the user data using the tokens
const { data: { user }, error: getUserError } = await supabase.auth.getUser(access_token);

if (getUserError || !user) {
throw getUserError || new Error("No user found");
}

// Get the user's profile


console.group(`|============ Supabase Auth User ============|`);
const { data: profile, error: profileError } = await supabase
.from("profiles")
.select("*")
.eq("id", user.id)
.single();

if (profileError) {
throw profileError;
}

console.log(user);
console.groupEnd();

console.group(`|============ Neo4j User ============|`)
const neoUser = {
authId: user.id,
name: user.user_metadata.name,
email: user.email
};
console.log(neoUser);
console.groupEnd();

// Redirect to frontend with success
ctx.response.redirect(`${Deno.env.get("FRONTEND_URL")}/login/success`);

} catch (error: unknown) {
console.error("Callback error:", error);
// Redirect to frontend with error
const errorMessage = error instanceof Error ? error.message : "An unknown error occurred";
ctx.response.redirect(`${Deno.env.get("FRONTEND_URL")}/login/error?message=${encodeURIComponent(errorMessage)}`);
}

console.groupEnd();
});
routes.push("auth/callback");

router.get("/user", verifyUser, (ctx) => {});
// routes.push("user");

router.post("/signout", verifyUser, async (ctx) => {});
// routes.push("signout");

export { router as authRouter, routes as authRoutes };
45 changes: 0 additions & 45 deletions routes/authRoutes/authRoutes.ts

This file was deleted.

4 changes: 2 additions & 2 deletions routes/hubRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Router } from "oak";
import { Subrouter } from "types/serverTypes.ts";
import { authRouter, authRoutes } from "authRoutes/authRoutes.ts";
import { authRouter, authRoutes } from "./authRoutes.ts";
import { editRouter, editRoutes } from "dbRoutes/editRoutes.ts";
import { getRouter, getRoutes } from "dbRoutes/getRoutes.ts";
import { findRouter, findRoutes } from "dbRoutes/findRoutes.ts";
import { sendRouter, sendRoutes } from "emailRoutes/sendRoutes.ts";
import { sendRouter, sendRoutes } from "./sendRoutes.ts";
import { toolRouter, toolRoutes } from "dbRoutes/toolRoutes.ts";
import { writeRouter, writeRoutes } from "dbRoutes/writeRoutes.ts";

Expand Down
File renamed without changes.
5 changes: 0 additions & 5 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
import { assertEquals } from "jsr:@std/assert";
import { delay } from "jsr:@std/async/delay";
import { expect } from "jsr:@std/expect";

// Import all auth tests
import "./auth/auth.test.ts";
import "./auth/authConfig.test.ts";
import "./auth/denoKvUserStore.test.ts";
import "./auth/sendMagicLink.test.ts";

// Basic tests to verify the test runner works
Expand Down
31 changes: 0 additions & 31 deletions utils/auth/authLogger.ts

This file was deleted.

2 changes: 1 addition & 1 deletion utils/auth/authMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Context, Next } from "oak";
import { createClient } from "supabase";
import { getNeo4jUserData } from "./neo4jUserLink.ts";

const devMode:boolean = true;
const devMode:boolean = Deno.env.get("DENO_ENV") !== "production";

export async function verifyUser(ctx: Context, next: () => Promise<unknown>) {
console.groupCollapsed("|=== Verification Middleware ===|");
Expand Down
34 changes: 0 additions & 34 deletions utils/auth/supabase.ts

This file was deleted.