repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
Projectflow
stars
latest
clone command
git clone gitlawb://did:key:z6Mkfh4Y...QBEi/projectflowgit clone gitlawb://did:key:z6Mkfh4Y.../projectflowb3cded1async from playground1d ago| #1 | import { StateCreator } from "zustand"; |
| #2 | import type { User } from "../../../domain/entities"; |
| #3 | import { users } from "../../../infrastructure/mock-data/users"; |
| #4 | import { generateId } from "../../../infrastructure/helpers"; |
| #5 | import type { StoreState } from "../store.types"; |
| #6 | |
| #7 | export interface AuthSlice { |
| #8 | currentUser: User | null; |
| #9 | isAuthenticated: boolean; |
| #10 | login: (email: string, password: string) => boolean; |
| #11 | register: (name: string, email: string, password: string) => boolean; |
| #12 | logout: () => void; |
| #13 | } |
| #14 | |
| #15 | export const createAuthSlice: StateCreator<StoreState, [], [], AuthSlice> = ( |
| #16 | set |
| #17 | ) => ({ |
| #18 | currentUser: null, |
| #19 | isAuthenticated: false, |
| #20 | login: (email: string, _password: string) => { |
| #21 | const user = users.find((u) => u.email === email); |
| #22 | if (user) { |
| #23 | set({ currentUser: user, isAuthenticated: true }); |
| #24 | return true; |
| #25 | } |
| #26 | set({ currentUser: users[0], isAuthenticated: true }); |
| #27 | return true; |
| #28 | }, |
| #29 | register: (name: string, email: string, _password: string) => { |
| #30 | const newUser: User = { |
| #31 | id: generateId(), |
| #32 | name, |
| #33 | email, |
| #34 | avatar: "", |
| #35 | createdAt: new Date().toISOString(), |
| #36 | }; |
| #37 | set({ currentUser: newUser, isAuthenticated: true }); |
| #38 | return true; |
| #39 | }, |
| #40 | logout: () => { |
| #41 | set({ currentUser: null, isAuthenticated: false }); |
| #42 | }, |
| #43 | }); |
| #44 |