@@ -2,8 +2,10 @@ import { useRouter } from "next/router";
22import { useEffect , useState } from "react" ;
33
44export default function Authorize ( ) {
5+ const [ isChecked , setIsChecked ] = useState ( false ) ;
56 const [ authUrl , setAuthUrl ] = useState ( "" ) ;
67 const router = useRouter ( ) ;
8+ const [ isLoading , setIsLoading ] = useState ( false ) ;
79
810 useEffect ( ( ) => {
911 // Replace with your Facebook app's client ID and redirect URI
@@ -13,42 +15,114 @@ export default function Authorize() {
1315
1416 const url = `https://www.facebook.com/v11.0/dialog/oauth?client_id=${ clientId } &redirect_uri=${ redirectUri } &scope=${ scope } &response_type=token` ;
1517 setAuthUrl ( url ) ;
18+
19+ // Check if there's an access token in the URL fragment
20+ const hash = window . location . hash . substring ( 1 ) ;
21+ const params = new URLSearchParams ( hash ) ;
22+ const accessToken = params . get ( "access_token" ) ;
23+
24+ if ( accessToken ) {
25+ handleAccessToken ( accessToken ) ;
26+ }
1627 } , [ ] ) ;
1728
29+ const handleAccessToken = async ( accessToken : string ) => {
30+ setIsLoading ( true ) ; // Set loading to true when the request starts
31+ try {
32+ const response = await fetch ( "/api/facebook-auth" , {
33+ method : "POST" ,
34+ headers : {
35+ "Content-Type" : "application/json" ,
36+ } ,
37+ body : JSON . stringify ( { accessToken } ) ,
38+ } ) ;
39+ const data = await response . json ( ) ;
40+
41+ if ( data . success ) {
42+ router . push ( {
43+ pathname : "/authorize/success" ,
44+ query : { details : JSON . stringify ( data . data ) } ,
45+ } ) ;
46+ } else {
47+ console . error ( "Error saving account:" , data . error ) ;
48+ router . push ( {
49+ pathname : "/authorize/error" ,
50+ query : { message : data . error } ,
51+ } ) ;
52+ }
53+ } catch ( error ) {
54+ console . error ( "Error:" , error ) ;
55+ router . push ( {
56+ pathname : "/authorize/error" ,
57+ query : { message : "An unexpected error occurred. Please try again." } ,
58+ } ) ;
59+ } finally {
60+ setIsLoading ( false ) ; // Set loading to false when the request completes
61+ }
62+ } ;
63+
1864 const handleAuthorize = ( ) => {
19- router . push ( authUrl ) ;
65+ window . location . href = authUrl ;
2066 } ;
2167
2268 return (
2369 < div className = "flex flex-col items-center justify-center min-h-screen p-12" >
24- < div className = "w-full max-w-2xl text-center" >
25- < h1 className = "text-3xl font-bold mb-4 text-center" >
26- Authorize Facebook App
27- </ h1 >
28- < p className = "mb-6 text-center" >
29- To provide you with the best experience, we need access to your
30- Facebook pages. By authorizing our app, you allow us to manage and
31- retrieve events from your pages, which helps us keep your event
32- information up-to-date and accurate in our database.
33- </ p >
34- < p className = "my-6 text-center" >
35- Click the button below whenever you are ready!
36- </ p >
37- < div className = "flex justify-center" >
70+ { isLoading ? (
71+ < >
72+ < div className = "w-full max-w-2xl text-center" >
73+ < h1 className = "text-3xl font-bold mb-4 text-center" >
74+ Just a sec...
75+ </ h1 >
76+ < p className = "mb-6 text-center text-gray-500" >
77+ We are processing your request. This may take a few seconds.
78+ </ p >
79+ </ div >
80+ < button
81+ className = "text-sm mt-2 text-gray-500 absolute bottom-5"
82+ onClick = { ( ) => router . push ( "/" ) }
83+ >
84+ Click here to go back if nothing happens...
85+ </ button >
86+ </ >
87+ ) : (
88+ < >
89+ < div className = "w-full max-w-2xl text-center" >
90+ < h1 className = "text-3xl font-bold mb-4 text-center" >
91+ Authorize CebEvents FB App
92+ </ h1 >
93+ < p className = "mb-6 text-center" >
94+ By authorizing our app, you allow us to manage and retrieve events
95+ from your pages, which helps us keep your event information
96+ up-to-date and accurate in this app and our database.
97+ </ p >
98+ < div className = "my-6 text-center" >
99+ < label className = "inline-flex items-center" >
100+ < input
101+ type = "checkbox"
102+ className = "form-checkbox"
103+ onChange = { ( e ) => setIsChecked ( e . target . checked ) }
104+ />
105+ < span className = "ml-2" > I am ready to proceed...</ span >
106+ </ label >
107+ </ div >
108+ < div className = "flex justify-center" >
109+ < button
110+ onClick = { handleAuthorize }
111+ className = { `px-4 py-2 rounded-lg text-white ${ isChecked ? "bg-blue-500" : "bg-gray-400 cursor-not-allowed" } ` }
112+ disabled = { ! isChecked || isLoading } // Disable button when loading
113+ >
114+ Begin Authorization
115+ </ button >
116+ </ div >
117+ </ div >
38118 < button
39- onClick = { handleAuthorize }
40- className = "px-4 py-2 bg-blue-500 text-white rounded-lg"
119+ className = "text-sm mt-2 text-gray-500 absolute bottom-5"
120+ onClick = { ( ) => router . push ( "/" ) }
41121 >
42- Begin Authorization
122+ Click here to go back
43123 </ button >
44- </ div >
45- < button
46- className = "text-sm mt-2 text-gray-500"
47- onClick = { ( ) => router . push ( "/" ) }
48- >
49- Click here to go back
50- </ button >
51- </ div >
124+ </ >
125+ ) }
52126 </ div >
53127 ) ;
54128}
0 commit comments