# Code coverage report for store/auth.js

# All files / store auth.js

100% Statements 25/25
100% Branches 2/2
100% Functions 5/5
100% Lines 24/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48

    1x

    3x
    3x
    1x
    1x

    2x
    2x
    1x

    1x
    1x
    1x

    2x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

     import { create } from 'zustand'
    import api from '../api/client'

    export const useAuthStore = create((set, get) => ({
      user: null,
      isLoading: true,
      isAuthenticated: false,

      initialize: async () => {
        const token = localStorage.getItem('access_token')
        if (!token) {
          set({ isLoading: false, isAuthenticated: false })
          return
        }

        try {
          const { data } = await api.get('/auth/me')
          set({ user: data, isLoading: false, isAuthenticated: true })
        } catch (error) {
          localStorage.removeItem('access_token')
          localStorage.removeItem('refresh_token')
          set({ user: null, isLoading: false, isAuthenticated: false })
        }
      },

      login: async (email, password) => {
        const { data } = await api.post('/auth/login', { email, password })
        localStorage.setItem('access_token', data.access_token)
        localStorage.setItem('refresh_token', data.refresh_token)
        set({ user: data.user, isAuthenticated: true })
        return data
      },

      register: async (email, username, password) => {
        const { data } = await api.post('/auth/register', { email, username, password })
        localStorage.setItem('access_token', data.access_token)
        localStorage.setItem('refresh_token', data.refresh_token)
        set({ user: data.user, isAuthenticated: true })
        return data
      },

      logout: () => {
        localStorage.removeItem('access_token')
        localStorage.removeItem('refresh_token')
        set({ user: null, isAuthenticated: false })
      },
    }))