Correcoes

This commit is contained in:
Lucas Santana 2024-12-19 19:32:49 -03:00
parent 014e71e3f1
commit 26888e9824
3 changed files with 45 additions and 17 deletions

View File

@ -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<AppStep>('welcome');
const [user, setUser] = useState<User | null>(null);
const [authUser, setAuthUser] = useState<AuthUser | null>(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' && (
<div className="min-h-screen flex items-center justify-center p-6">
<LoginForm
userType="school"
onLogin={handleLogin}
onRegisterClick={() => setStep('register')}
/>

View File

@ -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<void>;
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
</button>
<div className="text-center">
<button
type="button"
onClick={onRegisterClick}
className="text-purple-600 hover:text-purple-500"
>
Criar uma nova conta
</button>
</div>
{onRegisterClick && (
<div className="text-center">
<button
type="button"
onClick={onRegisterClick}
className="text-purple-600 hover:text-purple-500"
>
Criar uma nova conta
</button>
</div>
)}
</form>
</div>
);

View File

@ -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<void>;
}
export function useAuth() {
const [user, setUser] = useState<User | null>(null)
const [loading, setLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
useEffect(() => {
// Verificar sessão atual
@ -67,6 +77,7 @@ export function useAuth() {
return {
user,
loading,
error,
signIn,
signUp,
signOut