Files
kottgard-production/docs/annotated/src/store/auth.annotated.ts
T

180 lines
7.5 KiB
TypeScript

/**
* ANNOTATED COPY — every line explained
* Source: src/store/auth.ts
* NOT used by the app — read this to learn how the real file works
*/
// Zustand — simple global state store (cart, auth, locale)
import { create } from 'zustand';
// Zustand — simple global state store (cart, auth, locale)
import { persist } from 'zustand/middleware';
// Import project module (@/ alias = src/ folder in tsconfig)
import { Address, Order, User } from '@/types';
// Import project module (@/ alias = src/ folder in tsconfig)
import { DEMO_EMAIL } from '@/lib/constants';
// Import project module (@/ alias = src/ folder in tsconfig)
import { DEMO_ORDERS } from '@/lib/demo-orders';
// (blank line — separates logical blocks for readability)
// TypeScript interface — contract for object properties and methods
interface AuthState {
// Line 8: user: User | null;
user: User | null;
// Line 9: orders: Order[];
orders: Order[];
// Line 10: isAuthenticated: boolean;
isAuthenticated: boolean;
// Line 11: login: (email: string, password: string) => boolean;
login: (email: string, password: string) => boolean;
// Line 12: register: (data: {
register: (data: {
// Line 13: name: string;
name: string;
// Line 14: email: string;
email: string;
// Line 15: password: string;
password: string;
// Line 16: phone: string;
phone: string;
// Line 17: address: Address;
address: Address;
// Closing brace — end of block (function, if, object, JSX)
}) => boolean;
// Line 19: logout: () => void;
logout: () => void;
// Line 20: updateProfile: (data: Partial<User>) => void;
updateProfile: (data: Partial<User>) => void;
// Line 21: addOrder: (order: Order) => void;
addOrder: (order: Order) => void;
// Closing brace — end of block (function, if, object, JSX)
}
// (blank line — separates logical blocks for readability)
// Line 24: const DEMO_USER: User = {
const DEMO_USER: User = {
// Property or array item — trailing comma allowed in TypeScript
id: 'demo-1',
// Property or array item — trailing comma allowed in TypeScript
name: 'Ahmed Khan',
// Property or array item — trailing comma allowed in TypeScript
email: DEMO_EMAIL,
// Property or array item — trailing comma allowed in TypeScript
phone: '+46 72 585 50 50',
// Line 29: address: {
address: {
// Property or array item — trailing comma allowed in TypeScript
street: 'Tingvallavägen 11',
// Property or array item — trailing comma allowed in TypeScript
city: 'Märsta',
// Property or array item — trailing comma allowed in TypeScript
state: 'Stockholm',
// Property or array item — trailing comma allowed in TypeScript
zip: '195 31',
// Closing brace — end of block (function, if, object, JSX)
},
// Property or array item — trailing comma allowed in TypeScript
createdAt: '2025-03-15T10:00:00.000Z',
// Closing brace — end of block (function, if, object, JSX)
};
// (blank line — separates logical blocks for readability)
// Named export constant — shared config/data imported elsewhere
export const useAuthStore = create<AuthState>()(
// Zustand persist — save store to localStorage between visits
persist(
// Line 40: (set, get) => ({
(set, get) => ({
// Property or array item — trailing comma allowed in TypeScript
user: null,
// Property or array item — trailing comma allowed in TypeScript
orders: [],
// Property or array item — trailing comma allowed in TypeScript
isAuthenticated: false,
// (blank line — separates logical blocks for readability)
// Line 45: login: (email, password) => {
login: (email, password) => {
// Conditional branch — different behavior based on runtime value
if (email === DEMO_EMAIL && password === 'demo123') {
// Line 47: const existingOrders = get().orders;
const existingOrders = get().orders;
// Line 48: set({
set({
// Property or array item — trailing comma allowed in TypeScript
user: DEMO_USER,
// Property or array item — trailing comma allowed in TypeScript
isAuthenticated: true,
// Property or array item — trailing comma allowed in TypeScript
orders: existingOrders.length > 0 ? existingOrders : DEMO_ORDERS,
// Closing brace — end of block (function, if, object, JSX)
});
// Return value from function
return true;
// Closing brace — end of block (function, if, object, JSX)
}
// Line 55: const stored = get().user;
const stored = get().user;
// Conditional branch — different behavior based on runtime value
if (stored && stored.email === email) {
// Line 57: set({ isAuthenticated: true });
set({ isAuthenticated: true });
// Return value from function
return true;
// Closing brace — end of block (function, if, object, JSX)
}
// Return value from function
return false;
// Closing brace — end of block (function, if, object, JSX)
},
// (blank line — separates logical blocks for readability)
// Line 63: register: (data) => {
register: (data) => {
// Line 64: const user: User = {
const user: User = {
// Property or array item — trailing comma allowed in TypeScript
id: `user-${Date.now()}`,
// Property or array item — trailing comma allowed in TypeScript
name: data.name,
// Property or array item — trailing comma allowed in TypeScript
email: data.email,
// Property or array item — trailing comma allowed in TypeScript
phone: data.phone,
// Property or array item — trailing comma allowed in TypeScript
address: data.address,
// Property or array item — trailing comma allowed in TypeScript
createdAt: new Date().toISOString(),
// Closing brace — end of block (function, if, object, JSX)
};
// Line 72: set({ user, isAuthenticated: true });
set({ user, isAuthenticated: true });
// Return value from function
return true;
// Closing brace — end of block (function, if, object, JSX)
},
// (blank line — separates logical blocks for readability)
// Property or array item — trailing comma allowed in TypeScript
logout: () => set({ isAuthenticated: false }),
// (blank line — separates logical blocks for readability)
// Line 78: updateProfile: (data) => {
updateProfile: (data) => {
// Line 79: const current = get().user;
const current = get().user;
// Conditional branch — different behavior based on runtime value
if (current) {
// Line 81: set({ user: { ...current, ...data } });
set({ user: { ...current, ...data } });
// Closing brace — end of block (function, if, object, JSX)
}
// Closing brace — end of block (function, if, object, JSX)
},
// (blank line — separates logical blocks for readability)
// Line 85: addOrder: (order) => {
addOrder: (order) => {
// Line 86: set({ orders: [order, ...get().orders] });
set({ orders: [order, ...get().orders] });
// Closing brace — end of block (function, if, object, JSX)
},
// Closing brace — end of block (function, if, object, JSX)
}),
// Line 89: { name: 'kott-gard-auth' }
{ name: 'kott-gard-auth' }
// Line 90: )
)
// Line 91: );
);