@@ -8,6 +8,7 @@ const routes: string[] = [];
88router . 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} ) ;
4859routes . 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+
50125router . get ( "/user" , verifyUser , ( ctx ) => { } ) ;
51126// routes.push("user");
52127
0 commit comments