Skip to content

Commit ed0cfd2

Browse files
committed
refactor: enhance authentication handling and user profile management
- Updated middleware to check authentication using Better Auth session and fallback to server-side user check. - Simplified LoginForm by removing unused hooks and added console logs for better debugging. - Modified ProtectedRoute to check user verification status using profile data. - Refactored ProfileDataClient to utilize a new auth status hook for user data. - Streamlined useAuth hook to fetch user profile and manage authentication state more efficiently. - Removed reliance on Zustand store for auth state management in favor of Better Auth integration.
1 parent acc1b8a commit ed0cfd2

8 files changed

Lines changed: 168 additions & 289 deletions

File tree

components/auth/login-form.tsx

Lines changed: 6 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import { z } from 'zod';
77
import { Button } from '@/components/ui/button';
88
import { Input } from '@/components/ui/input';
99
import { Label } from '@/components/ui/label';
10-
import { useAuthActions, useAuthErrorHandler } from '@/hooks/use-auth';
1110
import { authClient } from '@/lib/auth-client';
12-
import { useAuthStore } from '@/lib/stores/auth-store';
1311
import { toast } from 'sonner';
1412

1513
const loginSchema = z.object({
@@ -26,8 +24,6 @@ interface LoginFormProps {
2624

2725
export function LoginForm({ onSuccess, onError }: LoginFormProps) {
2826
const [isLoading, setIsLoading] = useState(false);
29-
const { setError } = useAuthActions();
30-
const { handleAuthError } = useAuthErrorHandler();
3127

3228
const {
3329
register,
@@ -40,7 +36,7 @@ export function LoginForm({ onSuccess, onError }: LoginFormProps) {
4036
const onSubmit = async (data: LoginFormData) => {
4137
try {
4238
setIsLoading(true);
43-
setError(null);
39+
console.log('Attempting login for:', data.email);
4440

4541
const { error } = await authClient.signIn.email(
4642
{
@@ -49,35 +45,8 @@ export function LoginForm({ onSuccess, onError }: LoginFormProps) {
4945
rememberMe: true,
5046
},
5147
{
52-
onSuccess: async () => {
53-
// Get session after successful login
54-
const session = await authClient.getSession();
55-
56-
if (session && typeof session === 'object' && 'user' in session) {
57-
const sessionUser = session.user as
58-
| {
59-
id: string;
60-
email: string;
61-
name?: string | null;
62-
image?: string | null;
63-
}
64-
| null
65-
| undefined;
66-
67-
if (sessionUser && sessionUser.id && sessionUser.email) {
68-
const authStore = useAuthStore.getState();
69-
await authStore.syncWithSession({
70-
id: sessionUser.id,
71-
email: sessionUser.email,
72-
name: sessionUser.name || undefined,
73-
image: sessionUser.image || undefined,
74-
role: 'USER',
75-
username: undefined,
76-
accessToken: undefined,
77-
});
78-
}
79-
}
80-
48+
onSuccess: () => {
49+
console.log('Login successful');
8150
toast.success('Login successful!');
8251
onSuccess?.();
8352
},
@@ -88,13 +57,7 @@ export function LoginForm({ onSuccess, onError }: LoginFormProps) {
8857
? errorObj.message
8958
: 'Login failed. Please try again.';
9059

91-
if (
92-
handleAuthError(errorObj as { status?: number; code?: string })
93-
) {
94-
return;
95-
}
96-
97-
setError(errorMessage);
60+
console.error('Login error:', errorObj);
9861
onError?.(errorMessage);
9962
toast.error(errorMessage);
10063
},
@@ -103,7 +66,7 @@ export function LoginForm({ onSuccess, onError }: LoginFormProps) {
10366

10467
if (error) {
10568
const errorMessage = error.message || 'Login failed. Please try again.';
106-
setError(errorMessage);
69+
console.error('Login error:', error);
10770
onError?.(errorMessage);
10871
toast.error(errorMessage);
10972
}
@@ -112,7 +75,7 @@ export function LoginForm({ onSuccess, onError }: LoginFormProps) {
11275
error instanceof Error
11376
? error.message
11477
: 'Login failed. Please try again.';
115-
setError(errorMessage);
78+
console.error('Login error:', error);
11679
onError?.(errorMessage);
11780
toast.error(errorMessage);
11881
} finally {

components/auth/protected-route.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function ProtectedRoute({
3434
}
3535

3636
// Check if user verification is required
37-
if (requireVerified && user && !user.isVerified) {
37+
if (requireVerified && user && !user.profile?.isVerified) {
3838
router.push('/auth/verify-email');
3939
return;
4040
}
@@ -78,7 +78,7 @@ export function ProtectedRoute({
7878
}
7979

8080
// Show fallback if user is not verified
81-
if (requireVerified && user && !user.isVerified) {
81+
if (requireVerified && user && !user.profile?.isVerified) {
8282
return (
8383
fallback || (
8484
<div className='flex min-h-screen items-center justify-center'>

components/profile/ProfileDataClient.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { Button } from '../ui/button';
1919
import ActivityHeatmap from './ActivityHeatMap';
2020
import { FutureFeature } from '../FeatureFuture';
21-
import { useAuthStore } from '@/lib/stores/auth-store';
21+
import { useAuthStatus } from '@/hooks/use-auth';
2222

2323
interface ProfileDataClientProps {
2424
user: GetMeResponse;
@@ -41,8 +41,8 @@ export default function ProfileDataClient({
4141
}: ProfileDataClientProps) {
4242
const [selectedFilter, setSelectedFilter] = useState('All');
4343

44-
// Get auth state from store
45-
const { user: authUser, isAuthenticated } = useAuthStore();
44+
// Get auth state from simplified hook
45+
const { user: authUser, isAuthenticated } = useAuthStatus();
4646

4747
// Determine if it's the user's own profile
4848
const isOwnProfile =

0 commit comments

Comments
 (0)