|
1 | 1 | import { accountLogin, accountLogout, accountProfile } from "@/lib/api/sdk.gen"
|
2 | 2 | import type { Team, User } from "@/lib/api/types.gen"
|
| 3 | +import { toast } from "sonner" |
3 | 4 | import { create } from "zustand"
|
4 | 5 | import { persist } from "zustand/middleware"
|
5 | 6 |
|
@@ -27,18 +28,27 @@ export const useAuthStore = create<AuthState>()(
|
27 | 28 | login: async (email: string, password: string) => {
|
28 | 29 | set({ isLoading: true })
|
29 | 30 | try {
|
30 |
| - // First make the login request |
31 |
| - await accountLogin({ body: { username: email, password } }) |
| 31 | + // Login request |
| 32 | + const response = await accountLogin({ body: { username: email, password } }) |
32 | 33 |
|
33 |
| - // Add a small delay to ensure the cookie is set |
34 |
| - await new Promise((resolve) => setTimeout(resolve, 100)) |
| 34 | + if (response.status === 201) { |
| 35 | + // Add a small delay to ensure the cookie is set |
| 36 | + await new Promise((resolve) => setTimeout(resolve, 100)) |
35 | 37 |
|
36 |
| - // Then verify the authentication by getting the profile |
37 |
| - const { data: user } = await accountProfile() |
38 |
| - set({ user, isAuthenticated: true }) |
| 38 | + // Verify the authentication by getting the profile |
| 39 | + const { data: user } = await accountProfile() |
| 40 | + // TODO: Maybe add a check for user here? |
| 41 | + set({ user, isAuthenticated: true }) |
| 42 | + return |
| 43 | + } |
| 44 | + |
| 45 | + set({ user: null, currentTeam: null, isAuthenticated: false }) |
| 46 | + toast.error(response.error?.title || "Login failed") |
39 | 47 | } catch (error) {
|
40 | 48 | set({ user: null, currentTeam: null, isAuthenticated: false })
|
41 |
| - throw error |
| 49 | + toast.error("An error occurred", { |
| 50 | + description: error instanceof Error ? error.message : "Unknown error", |
| 51 | + }) |
42 | 52 | } finally {
|
43 | 53 | set({ isLoading: false })
|
44 | 54 | }
|
|
0 commit comments