Ignore:
Timestamp:
03/22/26 17:58:40 (4 months ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
73b69b2
Parents:
b62cefc
Message:

Added fixes for the login,stories management and reading lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • chapterx-frontend/src/store/authStore.ts

    rb62cefc racf690c  
    2727      token: null,
    2828      showMatureContent: true,
    29       allUsers: [...mockUsers],
     29      allUsers: [],
    3030
    3131      login: async (emailOrUsername, password) => {
     
    3636            : get().allUsers.find(u => u.username === emailOrUsername)?.email || emailOrUsername
    3737          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          }
    5150          set({ currentUser: user, token })
    5251          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
    5560        }
    5661
     
    6671
    6772      register: async (data, role) => {
    68         // Try backend first
    6973        try {
    7074          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)
    7398        }
    7499
Note: See TracChangeset for help on using the changeset viewer.