Changeset acf690c for chapterx-frontend/src/store/authStore.ts
- Timestamp:
- 03/22/26 17:58:40 (4 months ago)
- Branches:
- main
- Children:
- 73b69b2
- Parents:
- b62cefc
- File:
-
- 1 edited
-
chapterx-frontend/src/store/authStore.ts (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
chapterx-frontend/src/store/authStore.ts
rb62cefc racf690c 27 27 token: null, 28 28 showMatureContent: true, 29 allUsers: [ ...mockUsers],29 allUsers: [], 30 30 31 31 login: async (emailOrUsername, password) => { … … 36 36 : get().allUsers.find(u => u.username === emailOrUsername)?.email || emailOrUsername 37 37 const res = await axios.post(`${API_BASE}/auth/login`, { email, password }, { timeout: 3000 }) 38 const { token, userId, username } = res.data 39 const user = get().allUsers.find(u => u.user_id === userId) || 40 get().allUsers.find(u => u.username === username) || { 41 user_id: userId, 42 username, 43 email, 44 name: username, 45 surname: '', 46 role: 'regular' as const, 47 created_at: new Date().toISOString(), 48 follower_count: 0, 49 following_count: 0, 50 } 38 const { token, userId, username, name, surname, role } = res.data 39 const user: User = { 40 user_id: userId, 41 username, 42 email, 43 name: name ?? username, 44 surname: surname ?? '', 45 role: role ?? 'regular', 46 created_at: new Date().toISOString(), 47 follower_count: 0, 48 following_count: 0, 49 } 51 50 set({ currentUser: user, token }) 52 51 return 53 } catch { 54 // Fall through to mock login 52 } catch (err: any) { 53 // Only fall through to mock if the backend is unreachable (network/timeout) 54 // If the backend responded with an error (4xx/5xx), surface it to the user 55 if (err?.response) { 56 const message = err.response.data?.message || err.response.data || 'Invalid email or password.' 57 throw new Error(typeof message === 'string' ? message : 'Invalid email or password.') 58 } 59 // Network error / timeout — fall through to mock login 55 60 } 56 61 … … 66 71 67 72 register: async (data, role) => { 68 // Try backend first69 73 try { 70 74 await axios.post(`${API_BASE}/auth/register`, data, { timeout: 3000 }) 71 } catch { 72 // Fall through to mock register 75 // Register doesn't return a token — auto-login to get one 76 const loginRes = await axios.post(`${API_BASE}/auth/login`, { email: data.email, password: data.password }, { timeout: 3000 }) 77 const { token, userId, username } = loginRes.data 78 const newUser: User = { 79 user_id: userId, 80 username, 81 email: data.email, 82 name: data.name, 83 surname: data.surname, 84 role, 85 created_at: new Date().toISOString(), 86 follower_count: 0, 87 following_count: 0, 88 } 89 set(state => ({ allUsers: [...state.allUsers, newUser], currentUser: newUser, token })) 90 return 91 } catch (err: any) { 92 if (err?.response) { 93 const message = err.response.data?.message || err.response.data || 'Registration failed.' 94 throw new Error(typeof message === 'string' ? message : 'Registration failed.') 95 } 96 // Network error / timeout — fall through to mock register 97 console.warn('Backend unreachable during register, using mock:', err?.message) 73 98 } 74 99
Note:
See TracChangeset
for help on using the changeset viewer.
