@@ -15,6 +15,7 @@ export default function Profile() {
1515 const [ isSigningIn , setIsSigningIn ] = useState ( true ) ;
1616 const [ isLoggedIn , setIsLoggedIn ] = useState ( false ) ;
1717 const [ profileData , setProfileData ] = useState ( null ) ;
18+ const [ loadingSignIn , setLoadingSignIn ] = useState ( false ) ;
1819
1920 const [ loading , setLoading ] = useState ( true ) ; // NEW
2021
@@ -56,6 +57,8 @@ export default function Profile() {
5657 // Sign Up handler
5758 function handleSignUp ( e ) {
5859 e . preventDefault ( ) ;
60+ setLoadingSignIn ( true ) ;
61+
5962 const attributeList = [
6063 new CognitoUserAttribute ( { Name : "email" , Value : email } ) ,
6164 new CognitoUserAttribute ( { Name : "name" , Value : name } ) ,
@@ -68,6 +71,7 @@ export default function Profile() {
6871 console . log ( "Sign-up successful:" , data ) ;
6972 setNeedsConfirmation ( true ) ;
7073 }
74+ setLoadingSignIn ( false ) ;
7175 } ) ;
7276 }
7377
@@ -90,9 +94,40 @@ export default function Profile() {
9094 } ) ;
9195 }
9296
97+ async function fetchUserNotes ( userId , idToken ) {
98+ const res = await fetch (
99+ `${ process . env . LAMBDA_URL } get_notes?userId=${ userId } ` ,
100+ {
101+ method : "GET" ,
102+ headers : {
103+ Authorization : idToken ,
104+ "Content-Type" : "application/json" ,
105+ } ,
106+ }
107+ ) ;
108+
109+ if ( ! res . ok ) {
110+ throw new Error ( "Failed to fetch notes" ) ;
111+ }
112+ return await res . json ( ) ;
113+ }
114+
115+ function mergeNotes ( localNotes = [ ] , cloudNotes = [ ] ) {
116+ const map = new Map ( ) ;
117+ cloudNotes . forEach ( note => {
118+ if ( note . id ) map . set ( note . id , note ) ;
119+ } ) ;
120+ localNotes . forEach ( note => {
121+ if ( note . id ) map . set ( note . id , note ) ;
122+ } ) ;
123+
124+ return Array . from ( map . values ( ) ) ;
125+ }
126+
93127 // Sign In handler
94128 function handleSignIn ( e ) {
95129 e . preventDefault ( ) ;
130+ setLoadingSignIn ( true ) ;
96131
97132 const authDetails = new AuthenticationDetails ( {
98133 Username : email ,
@@ -105,7 +140,9 @@ export default function Profile() {
105140 } ) ;
106141
107142 user . authenticateUser ( authDetails , {
108- onSuccess : ( session ) => {
143+ onSuccess : async ( session ) => {
144+ setLoadingSignIn ( false ) ;
145+
109146 const idToken = session . getIdToken ( ) . getJwtToken ( ) ;
110147 const accessToken = session . getAccessToken ( ) . getJwtToken ( ) ;
111148 const refreshToken = session . getRefreshToken ( ) . getToken ( ) ;
@@ -114,6 +151,21 @@ export default function Profile() {
114151 { idToken, accessToken, refreshToken } ,
115152 ( ) => console . log ( "Tokens stored in chrome.storage.local" )
116153 ) ;
154+ const userId = session . getIdToken ( ) . payload . sub ;
155+
156+ try {
157+ const cloudNotes = await fetchUserNotes ( userId , idToken ) ;
158+ chrome . storage . local . get ( [ "notes" ] , ( result ) => {
159+ const localNotes = result . notes || [ ] ;
160+ const merged = mergeNotes ( localNotes , cloudNotes ) ;
161+
162+ chrome . storage . local . set ( { notes : merged } , ( ) =>
163+ console . log ( "Notes merged successfully" )
164+ ) ;
165+ } ) ;
166+ } catch ( err ) {
167+ console . error ( err ) ;
168+ }
117169
118170 user . getUserAttributes ( ( err , attrs ) => {
119171 if ( ! err && attrs ) {
@@ -127,6 +179,7 @@ export default function Profile() {
127179 } ) ;
128180 } ,
129181 onFailure : ( err ) => {
182+ setLoadingSignIn ( false ) ;
130183 alert ( err . message || "Sign-in failed" ) ;
131184 } ,
132185 } ) ;
@@ -138,8 +191,8 @@ export default function Profile() {
138191 if ( user ) {
139192 user . signOut ( ) ;
140193 }
141- chrome . storage . local . clear ( ( ) => {
142- console . log ( "Tokens cleared" ) ;
194+ chrome . storage . local . remove ( [ "idToken" , "accessToken" , "refreshToken" ] , ( ) => {
195+ console . log ( "Auth tokens cleared, notes preserved " ) ;
143196 setIsLoggedIn ( false ) ;
144197 setIsSigningIn ( true ) ;
145198 } ) ;
@@ -178,6 +231,7 @@ export default function Profile() {
178231 required
179232 />
180233 < br />
234+ < br />
181235 < button type = "submit" className = "btn btn-primary btn-sm rounded-pill px-3 text-white" > Confirm</ button >
182236 </ form >
183237 ) ;
@@ -220,7 +274,14 @@ export default function Profile() {
220274 required
221275 />
222276 < br />
223- < button type = "submit" > Sign Up</ button >
277+
278+ < button type = "submit" disabled = { loadingSignIn } >
279+ { loadingSignIn ? (
280+ < span className = "spinner-border spinner-border-sm" role = "status" aria-hidden = "true" > </ span >
281+ ) : (
282+ "Sign Up"
283+ ) }
284+ </ button >
224285 </ form >
225286 </ div >
226287
@@ -245,7 +306,13 @@ export default function Profile() {
245306 required
246307 />
247308 < br />
248- < button type = "submit" > Sign In</ button >
309+ < button type = "submit" disabled = { loadingSignIn } >
310+ { loadingSignIn ? (
311+ < span className = "spinner-border spinner-border-sm" role = "status" aria-hidden = "true" > </ span >
312+ ) : (
313+ "Sign In"
314+ ) }
315+ </ button >
249316 </ form >
250317 </ div >
251318
0 commit comments