WFDASHDRIVER-APP/components/AuthProvider.tsx
SJCoetz b738bc7495 feat: initialize project with Next.js, TypeScript, and Tailwind CSS
- Added package.json with dependencies for Next.js, React, and Tailwind CSS.
- Created postcss.config.js for Tailwind CSS and autoprefixer integration.
- Added SVG icon for the application in public/icons/icon.svg.
- Created manifest.json for PWA support with app details and icon.
- Configured Tailwind CSS in tailwind.config.js with custom colors and animations.
- Set up TypeScript configuration in tsconfig.json for strict type checking and module resolution.
2026-05-24 21:51:29 +02:00

52 lines
1.2 KiB
TypeScript

'use client';
import React, { createContext, useContext, useState, useEffect } from 'react';
import { pb, getCurrentUser, isAuthenticated } from '@/lib/pocketbase';
interface AuthContextType {
isLoggedIn: boolean;
user: ReturnType<typeof getCurrentUser>;
isLoading: boolean;
refresh: () => void;
}
const AuthContext = createContext<AuthContextType>({
isLoggedIn: false,
user: null,
isLoading: true,
refresh: () => {},
});
export function AuthProvider({ children }: { children: React.ReactNode }) {
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [user, setUser] = useState<ReturnType<typeof getCurrentUser>>(null);
const [isLoading, setIsLoading] = useState(true);
const refresh = () => {
const valid = isAuthenticated();
setIsLoggedIn(valid);
setUser(getCurrentUser());
};
useEffect(() => {
refresh();
setIsLoading(false);
// Listen for auth store changes
const unsubscribe = pb.authStore.onChange(() => {
refresh();
});
return () => {
unsubscribe();
};
}, []);
return (
<AuthContext.Provider value={{ isLoggedIn, user, isLoading, refresh }}>
{children}
</AuthContext.Provider>
);
}
export const useAuth = () => useContext(AuthContext);