Fix: Redirect to dashboard after login
- Redirect to dashboard after successful login. - Created `/auth/reset` page for password reset functionality.
This commit is contained in:
parent
95207e71ef
commit
9a179d136e
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
import { Toaster } from "@/components/ui/sonner";
|
import { Toaster } from "@/components/ui/sonner";
|
||||||
import { TooltipProvider } from "@/components/ui/tooltip";
|
import { TooltipProvider } from "@/components/ui/tooltip";
|
||||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||||
@ -8,6 +9,7 @@ import { useAuthStore } from "@/stores/authStore";
|
|||||||
import ProtectedRoute from "@/components/auth/ProtectedRoute";
|
import ProtectedRoute from "@/components/auth/ProtectedRoute";
|
||||||
import Layout from "@/components/layout/Layout";
|
import Layout from "@/components/layout/Layout";
|
||||||
import ResetPassword from '@/pages/ResetPassword';
|
import ResetPassword from '@/pages/ResetPassword';
|
||||||
|
import AuthReset from '@/pages/AuthReset';
|
||||||
|
|
||||||
// Pages
|
// Pages
|
||||||
import Index from "./pages/Index";
|
import Index from "./pages/Index";
|
||||||
@ -60,6 +62,7 @@ function App() {
|
|||||||
{/* Rotas públicas */}
|
{/* Rotas públicas */}
|
||||||
<Route path="/" element={<Landing />} />
|
<Route path="/" element={<Landing />} />
|
||||||
<Route path="/auth" element={<Auth />} />
|
<Route path="/auth" element={<Auth />} />
|
||||||
|
<Route path="/auth/reset" element={<AuthReset />} />
|
||||||
<Route path="/email-confirmation" element={<EmailConfirmation />} />
|
<Route path="/email-confirmation" element={<EmailConfirmation />} />
|
||||||
<Route path="/reset-password" element={<ResetPassword />} />
|
<Route path="/reset-password" element={<ResetPassword />} />
|
||||||
|
|
||||||
|
|||||||
@ -25,7 +25,7 @@ const ForgotPasswordForm = ({ onBackToLogin }: ForgotPasswordFormProps) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const { error } = await supabase.auth.resetPasswordForEmail(email, {
|
const { error } = await supabase.auth.resetPasswordForEmail(email, {
|
||||||
redirectTo: `${window.location.origin}/reset-password`,
|
redirectTo: `${window.location.origin}/auth/reset`,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
|||||||
@ -53,7 +53,7 @@ const LoginForm = ({ isLoading, setIsLoading, onForgotPassword }: LoginFormProps
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Redirecionar para dashboard
|
// Redirecionar para dashboard
|
||||||
navigate('/');
|
navigate('/dashboard');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Erro inesperado no login:', error);
|
console.error('Erro inesperado no login:', error);
|
||||||
|
|||||||
139
src/pages/AuthReset.tsx
Normal file
139
src/pages/AuthReset.tsx
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
|
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Label } from "@/components/ui/label";
|
||||||
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
||||||
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
||||||
|
import { Lock } from "lucide-react";
|
||||||
|
import { supabase } from '@/integrations/supabase/client';
|
||||||
|
import { toast } from "sonner";
|
||||||
|
|
||||||
|
const AuthReset = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const [searchParams] = useSearchParams();
|
||||||
|
const [novaSenha, setNovaSenha] = useState('');
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [tokenValido, setTokenValido] = useState(true);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const accessToken = searchParams.get('access_token');
|
||||||
|
|
||||||
|
if (!accessToken) {
|
||||||
|
setTokenValido(false);
|
||||||
|
setError('Token de redefinição inválido ou ausente.');
|
||||||
|
}
|
||||||
|
}, [searchParams]);
|
||||||
|
|
||||||
|
const handleSubmit = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
setIsLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
if (!novaSenha) {
|
||||||
|
setError('Por favor, digite uma nova senha.');
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (novaSenha.length < 6) {
|
||||||
|
setError('A senha deve ter pelo menos 6 caracteres.');
|
||||||
|
setIsLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { error } = await supabase.auth.updateUser({
|
||||||
|
password: novaSenha
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
setError('Erro ao redefinir a senha: ' + error.message);
|
||||||
|
} else {
|
||||||
|
toast.success('Senha redefinida com sucesso!');
|
||||||
|
navigate('/auth');
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
setError('Erro inesperado. Tente novamente.');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!tokenValido) {
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
|
||||||
|
<Card className="w-full max-w-md">
|
||||||
|
<CardHeader className="space-y-1 text-center">
|
||||||
|
<CardTitle className="text-2xl text-red-600">Erro</CardTitle>
|
||||||
|
<CardDescription className="text-red-500">
|
||||||
|
Token de redefinição inválido ou ausente.
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<Button
|
||||||
|
onClick={() => navigate('/auth')}
|
||||||
|
variant="outline"
|
||||||
|
className="w-full"
|
||||||
|
>
|
||||||
|
Voltar ao login
|
||||||
|
</Button>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen flex items-center justify-center bg-gray-50 px-4">
|
||||||
|
<Card className="w-full max-w-md">
|
||||||
|
<CardHeader className="space-y-1 text-center">
|
||||||
|
<div className="flex justify-center mb-4">
|
||||||
|
<Lock className="h-12 w-12 text-blue-500" />
|
||||||
|
</div>
|
||||||
|
<CardTitle className="text-2xl">Redefinição de senha</CardTitle>
|
||||||
|
<CardDescription>
|
||||||
|
Digite sua nova senha
|
||||||
|
</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form onSubmit={handleSubmit} className="space-y-4">
|
||||||
|
<div className="space-y-2">
|
||||||
|
<Label htmlFor="nova-senha">Nova senha</Label>
|
||||||
|
<Input
|
||||||
|
id="nova-senha"
|
||||||
|
type="password"
|
||||||
|
placeholder="Digite sua nova senha"
|
||||||
|
value={novaSenha}
|
||||||
|
onChange={(e) => setNovaSenha(e.target.value)}
|
||||||
|
required
|
||||||
|
disabled={isLoading}
|
||||||
|
minLength={6}
|
||||||
|
className="rounded-lg border border-gray-300"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && (
|
||||||
|
<Alert variant="destructive">
|
||||||
|
<AlertDescription>{error}</AlertDescription>
|
||||||
|
</Alert>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<Button
|
||||||
|
id="botao-confirmar"
|
||||||
|
type="submit"
|
||||||
|
className="w-full bg-blue-600 hover:bg-blue-700 rounded-lg"
|
||||||
|
disabled={isLoading}
|
||||||
|
>
|
||||||
|
{isLoading ? "Atualizando..." : "Atualizar senha"}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AuthReset;
|
||||||
Loading…
Reference in New Issue
Block a user