@@ -67,14 +67,14 @@ export default function LoginScreen() {
6767 /*
6868 Send a request to the backend endpoint to get the logged-in user's username and profile settings
6969 */
70- const loadSettings = async ( ) => {
71-
70+ const loadSettings = async ( loggedInAs : string ) => {
7271 try {
73- const res = await fetch ( `${ API_BASE_URL } /profile/retreive/${ username } ` ) ;
72+ const res = await fetch (
73+ `${ API_BASE_URL } /profile/retreive/${ encodeURIComponent ( loggedInAs ) } `
74+ ) ;
7475 const data = await res . json ( ) ;
7576
76- // Update the copy of the logged-in user's username and profile settings using the retrieved data
77- setUsernameGlobal ( username ) ;
77+ setUsernameGlobal ( loggedInAs ) ;
7878 setHasConfiguredSettingsGlobal ( data . has_configured_settings )
7979 setShowStatsGlobal ( data . show_stats ) ;
8080 setHasEggAllergyGlobal ( data . has_egg_allergy ) ;
@@ -107,35 +107,54 @@ export default function LoginScreen() {
107107 string - psswrd: The password entered by the user to login into an account
108108 */
109109 const loginUser = async ( name : string , psswrd : string ) => {
110+ const trimmedUser = name . trim ( ) ;
111+ const trimmedPass = psswrd . trim ( ) ;
112+ setError ( { message : '' , status : '' } ) ;
113+
110114 try {
111115 const res = await fetch ( `${ API_BASE_URL } /auth/login` , {
112- method : "POST" ,
113- headers : { "Content-Type" : "application/json" } ,
114- body : JSON . stringify ( { username : name , password : psswrd } )
115- }
116- ) ;
117- const data = await res . json ( ) ;
118-
119- // If the backend endpoint returns an error, store the error message
120- // (The user failed to log in to an account with the entered username and password)
121- if ( data . status === 'error' ) {
122- setError ( data ) ;
123- console . log ( error ) ;
116+ method : 'POST' ,
117+ headers : { 'Content-Type' : 'application/json' } ,
118+ body : JSON . stringify ( {
119+ username : trimmedUser ,
120+ password : trimmedPass ,
121+ } ) ,
122+ } ) ;
123+
124+ let data : { status ?: string ; message ?: string } = { } ;
125+ try {
126+ data = await res . json ( ) ;
127+ } catch {
128+ setError ( {
129+ message : 'Invalid response from server.' ,
130+ status : 'error' ,
131+ } ) ;
132+ return ;
124133 }
125- // Else, update the copy of the user's username to the username they entered,
126- // load the user's profile settings from the backend endpoint, and route the user to the 'home' page
127- // (The user successfully logged in to an account with the entered username and password)
128- else if ( data . status === 'success' ) {
129- await loadSettings ( ) ;
130- router . push ( "/home" ) ;
134+
135+ if ( data . status === 'error' || ! res . ok ) {
136+ setError ( {
137+ message : data . message || `Login failed (${ res . status } )` ,
138+ status : 'error' ,
139+ } ) ;
140+ return ;
131141 }
132142
143+ if ( data . status === 'success' ) {
144+ setUsername ( trimmedUser ) ;
145+ await loadSettings ( trimmedUser ) ;
146+ router . push ( '/home' ) ;
147+ }
133148 } catch ( err ) {
134149 console . error ( err ) ;
150+ setError ( {
151+ message : 'Network error. Check your connection and try again.' ,
152+ status : 'error' ,
153+ } ) ;
135154 } finally {
136155 setLoading ( false ) ;
137156 }
138- }
157+ } ;
139158
140159 /*
141160 Log out the logged-in user by setting their profile settings to false, and routing to the splash page
0 commit comments