Skip to content

Commit 04eeb1e

Browse files
committed
feat(auth): 🛂 create new public user profile when signing up
1 parent a43fa82 commit 04eeb1e

File tree

2 files changed

+103
-28
lines changed

2 files changed

+103
-28
lines changed

deno.jsonc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"tasks": {
3-
"runDev": {
3+
"rundev": {
44
"description": "Run the local development build",
55
"command": "deno run -A --watch --unstable-cron --env-file=.env.local main.ts"
66
},
7-
"runProd": {
7+
"runprod": {
88
"description": "Run the local production build",
99
"command": "deno run -A --watch --unstable-cron --env-file=.env.production main.ts"
1010
},
@@ -13,29 +13,29 @@
1313
"description": "Run the tests",
1414
"command": "deno test -A --unstable-kv --no-check"
1515
},
16-
"testAuth": {
16+
"testauth": {
1717
"description": "Run only authentication tests",
1818
"command": "deno test -A --unstable-kv --no-check tests/auth-test.ts"
1919
},
2020
// Resend API
21-
"resendCheck": {
21+
"resendcheck": {
2222
"description": "Tests the Resend API",
2323
"command": "deno run -A --watch --unstable-cron --env-file=.env.local ./utils/emails/sendTest.ts"
2424
},
2525
// Neo4j API
26-
"n4jSeedL": {
26+
"n4jseedl": {
2727
"description": "Seed the local instance of neo4j",
2828
"command": "deno run -A --env-file=.env.local queries/seed.ts"
2929
},
30-
"n4jSeedP": {
30+
"n4jseedp": {
3131
"description": "Seed the production instance of neo4j",
3232
"command": "deno run -A --env-file=.env.production queries/seed.ts"
3333
},
34-
"n4jResetL": {
34+
"n4jresetl": {
3535
"description": "Reset the local instance of neo4j",
3636
"command": "deno run -A --env-file=.env.local queries/reset.ts"
3737
},
38-
"n4jResetP": {
38+
"n4jresetp": {
3939
"description": "Reset the production instance of neo4j",
4040
"command": "deno run -A --env-file=.env.production queries/reset.ts"
4141
},

routes/authRoutes.ts

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const routes: string[] = [];
88
router.post("/signin/magic-link", async (ctx) => {
99
console.group(`|============ Sign In Request ============|`);
1010
const body = await ctx.request.body.json();
11+
const name = body.name;
1112
const email = body.email;
1213

1314
if (!email) {
@@ -16,37 +17,111 @@ router.post("/signin/magic-link", async (ctx) => {
1617
return;
1718
}
1819

19-
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
20-
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
21-
const supabase = createClient(supabaseUrl, supabaseKey);
20+
try {
21+
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
22+
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
23+
const supabase = createClient(supabaseUrl, supabaseKey);
2224

23-
const { data, error } = await supabase.auth.signInWithOtp({
24-
email,
25-
options: { emailRedirectTo: "http://localhost:5000/auth/callback" },
26-
});
25+
const { data, error } = await supabase.auth.signInWithOtp({
26+
email,
27+
options: {
28+
emailRedirectTo: "http://localhost:8080/auth/callback",
29+
// [ ] tdHi: Get callback URL from Alex
30+
data: {
31+
name: name
32+
}
33+
},
34+
});
2735

28-
// [ ] tdHi: Get callback URL from Alex
36+
console.group(`|====== Sign In Result ======|`);
37+
if (error) {
38+
console.info(`| Error`);
39+
console.error(error);
2940

30-
console.group(`|====== Sign In Result ======|`);
31-
if (error) {
32-
console.info(`| Error`);
33-
console.error(error);
41+
ctx.response.status = 500;
42+
ctx.response.body = { error: error.message };
43+
} else {
44+
console.info(`| Success`);
45+
console.log(data);
3446

47+
ctx.response.status = 200;
48+
ctx.response.body = { message: "Magic link sent", data };
49+
}
50+
console.groupEnd();
51+
} catch (error) {
52+
console.error(error);
3553
ctx.response.status = 500;
36-
ctx.response.body = { error: error.message };
37-
} else {
38-
console.info(`| Success`);
39-
console.log(data);
40-
41-
ctx.response.status = 200;
42-
ctx.response.body = { message: "Magic link sent", data };
54+
ctx.response.body = { error };
4355
}
44-
console.groupEnd();
4556

4657
console.groupEnd();
4758
});
4859
routes.push("signin/magic-link");
4960

61+
router.get("/auth/callback", async (ctx) => {
62+
console.group("|============ Auth Callback ============|");
63+
64+
try {
65+
const supabaseUrl = Deno.env.get("SUPABASE_URL")!;
66+
const supabaseKey = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
67+
const supabase = createClient(supabaseUrl, supabaseKey);
68+
69+
// Get the access_token and refresh_token from URL params
70+
const params = new URLSearchParams(ctx.request.url.search);
71+
const access_token = params.get("access_token");
72+
const refresh_token = params.get("refresh_token");
73+
74+
if (!access_token || !refresh_token) {
75+
throw new Error("No tokens provided in callback");
76+
}
77+
78+
// Get the user data using the tokens
79+
const { data: { user }, error: getUserError } = await supabase.auth.getUser(access_token);
80+
81+
if (getUserError || !user) {
82+
throw getUserError || new Error("No user found");
83+
}
84+
85+
// Get the user's profile
86+
87+
88+
console.group(`|============ Supabase Auth User ============|`);
89+
const { data: profile, error: profileError } = await supabase
90+
.from("profiles")
91+
.select("*")
92+
.eq("id", user.id)
93+
.single();
94+
95+
if (profileError) {
96+
throw profileError;
97+
}
98+
99+
console.log(user);
100+
console.groupEnd();
101+
102+
console.group(`|============ Neo4j User ============|`)
103+
const neoUser = {
104+
authId: user.id,
105+
name: user.user_metadata.name,
106+
email: user.email
107+
};
108+
console.log(neoUser);
109+
console.groupEnd();
110+
111+
// Redirect to frontend with success
112+
ctx.response.redirect(`${Deno.env.get("FRONTEND_URL")}/login/success`);
113+
114+
} catch (error: unknown) {
115+
console.error("Callback error:", error);
116+
// Redirect to frontend with error
117+
const errorMessage = error instanceof Error ? error.message : "An unknown error occurred";
118+
ctx.response.redirect(`${Deno.env.get("FRONTEND_URL")}/login/error?message=${encodeURIComponent(errorMessage)}`);
119+
}
120+
121+
console.groupEnd();
122+
});
123+
routes.push("auth/callback");
124+
50125
router.get("/user", verifyUser, (ctx) => {});
51126
// routes.push("user");
52127

0 commit comments

Comments
 (0)