33import { createClient } from "~/utils/supabase/client" ;
44import { useRouter , useSearchParams } from "next/navigation" ;
55import { useState , useEffect , useCallback , useRef } from "react" ;
6+ import internalError from "~/utils/internalError" ;
67
78export const LoginWithToken = ( ) => {
89 const loginAttempted = useRef ( false ) ;
910 const searchParams = useSearchParams ( ) ;
1011 const router = useRouter ( ) ;
11- const [ secretToken ] = useState ( ( ) => {
12- const t = searchParams . get ( "t" ) ;
13- if ( t && typeof window !== "undefined" ) {
12+ const [ secretToken ] = useState ( ( ) => searchParams . get ( "t" ) ) ;
13+ useEffect ( ( ) => {
14+ if ( secretToken && typeof window !== "undefined" ) {
1415 const url = new URL ( window . location . href ) ;
1516 url . searchParams . delete ( "t" ) ;
1617 window . history . replaceState ( { } , "" , url ) ;
1718 }
18- return t ;
19- } ) ;
19+ } , [ secretToken ] ) ;
2020 const [ url ] = useState ( searchParams . get ( "url" ) ) ;
2121 const [ done , setDone ] = useState ( false ) ;
2222 const [ error , setError ] = useState < string | null > (
@@ -30,15 +30,23 @@ export const LoginWithToken = () => {
3030 token : secretToken ! ,
3131 } ) ;
3232 if ( result . error ) {
33- setError ( result . error . message ) ;
33+ setError ( "Could not connect to DiscourseGraphs" ) ;
34+ internalError ( { error : result . error , type : "get-secret-token" } ) ;
3435 return ;
3536 }
3637 if ( result . data == null ) {
37- setError ( "This token does not exist" ) ;
38+ setError ( "Could not retrieve information, please try again." ) ;
39+ internalError ( { error : "missing token" , type : "get-secret-token" } ) ;
3840 return ;
3941 }
4042 if ( typeof result . data !== "string" ) {
41- setError ( "Payload is not a string" ) ;
43+ setError (
44+ "DiscourseGraphs configuration error, the team has been warned" ,
45+ ) ;
46+ internalError ( {
47+ error : "payload-not-string" ,
48+ type : "get-secret-token" ,
49+ } ) ;
4250 return ;
4351 }
4452 const data = JSON . parse ( result . data ) as {
@@ -53,7 +61,10 @@ export const LoginWithToken = () => {
5361 ! data . access_token ||
5462 ! data . refresh_token
5563 ) {
56- setError ( "Malformed token information" ) ;
64+ setError (
65+ "DiscourseGraphs configuration error, the team has been warned" ,
66+ ) ;
67+ internalError ( { error : "misshaped-payload" , type : "get-secret-token" } ) ;
5768 return ;
5869 }
5970 const response = await client . auth . setSession ( data ) ;
@@ -63,9 +74,8 @@ export const LoginWithToken = () => {
6374 router . replace ( url ) ;
6475 }
6576 } catch ( error ) {
66- setError (
67- error instanceof Error ? error . message : "Unknown error occurred" ,
68- ) ;
77+ setError ( "Unkown error while logging you in." ) ;
78+ internalError ( { error, type : "token-login-exception" } ) ;
6979 } finally {
7080 setDone ( true ) ;
7181 }
0 commit comments