From 26888e98243c85f5754fb2e0a2e97b82c3b17cc3 Mon Sep 17 00:00:00 2001 From: Lucas Santana Date: Thu, 19 Dec 2024 19:32:49 -0300 Subject: [PATCH] Correcoes --- src/App.tsx | 16 +++++++++++---- src/components/auth/LoginForm.tsx | 33 ++++++++++++++++++++----------- src/hooks/useAuth.ts | 13 +++++++++++- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index a5c33f4..9604ff1 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,6 +9,7 @@ import { StoryLibrary } from './components/library/StoryLibrary'; import { AuthUser, SavedStory } from './types/auth'; import { User, Theme } from './types'; import { AuthProvider } from './contexts/AuthContext' +import { useNavigate } from 'react-router-dom'; type AppStep = | 'welcome' @@ -20,6 +21,7 @@ type AppStep = | 'library'; export function App() { + const navigate = useNavigate(); const [step, setStep] = useState('welcome'); const [user, setUser] = useState(null); const [authUser, setAuthUser] = useState(null); @@ -34,10 +36,15 @@ export function App() { }, ]); - const handleLogin = (email: string, password: string) => { - // In production: Implement actual authentication - setAuthUser({ id: '1', email, name: 'Usuário' }); - setStep('library'); + const handleLogin = async (credentials: { email: string; password: string }) => { + try { + // In production: Implement actual authentication + setAuthUser({ id: '1', email: credentials.email, name: 'Usuário' }); + // Redirecionar para o dashboard ao invés de library + navigate('/dashboard'); + } catch (err) { + console.error('Erro ao fazer login:', err); + } }; const handleRegistrationComplete = (userData: User) => { @@ -78,6 +85,7 @@ export function App() { {step === 'login' && (
setStep('register')} /> diff --git a/src/components/auth/LoginForm.tsx b/src/components/auth/LoginForm.tsx index 9d964af..0edc6f9 100644 --- a/src/components/auth/LoginForm.tsx +++ b/src/components/auth/LoginForm.tsx @@ -1,17 +1,20 @@ import React, { useState } from 'react'; import { LogIn } from 'lucide-react'; import { useAuth } from '../../hooks/useAuth'; +import { useNavigate } from 'react-router-dom'; interface LoginFormProps { userType: 'school' | 'teacher' | 'student'; - onLogin?: (email: string, password: string) => void; + onLogin?: (credentials: { email: string; password: string }) => Promise; + onRegisterClick?: () => void; } -export function LoginForm({ userType, onLogin }: LoginFormProps) { +export function LoginForm({ userType, onLogin, onRegisterClick }: LoginFormProps) { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(''); const { signIn } = useAuth(); + const navigate = useNavigate(); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); @@ -20,7 +23,11 @@ export function LoginForm({ userType, onLogin }: LoginFormProps) { try { const { user } = await signIn(email, password); if (user) { - onLogin(email, password); + if (userType === 'school') { + navigate('/dashboard'); + } else if (onLogin) { + await onLogin({ email, password }); + } } } catch (err) { setError('Erro ao fazer login. Verifique suas credenciais.'); @@ -83,15 +90,17 @@ export function LoginForm({ userType, onLogin }: LoginFormProps) { Entrar -
- -
+ {onRegisterClick && ( +
+ +
+ )}
); diff --git a/src/hooks/useAuth.ts b/src/hooks/useAuth.ts index 05f2353..1d63120 100644 --- a/src/hooks/useAuth.ts +++ b/src/hooks/useAuth.ts @@ -1,10 +1,20 @@ import { useState, useEffect } from 'react' import { supabase } from '../lib/supabase' -import { User } from '@supabase/supabase-js' +import { User, Session } from '@supabase/supabase-js' + +interface AuthContextType { + user: User | null; + loading: boolean; + error: string | null; + signIn: (email: string, password: string) => Promise<{ user: User; session: Session }>; + signUp: (email: string, password: string) => Promise<{ user: User; session: Session }>; + signOut: () => Promise; +} export function useAuth() { const [user, setUser] = useState(null) const [loading, setLoading] = useState(true) + const [error, setError] = useState(null) useEffect(() => { // Verificar sessão atual @@ -67,6 +77,7 @@ export function useAuth() { return { user, loading, + error, signIn, signUp, signOut