feat: implement email verification and password reset features
- Add EmailTokenService for handling email verification and password reset tokens. - Create EmailService for sending verification and reset emails via SMTP. - Update AuthService to handle email verification status during login. - Modify user registration to redirect to a check email screen instead of issuing a token. - Implement resend verification email functionality. - Add deep link handling for email verification and password reset in the mobile app. - Update mobile app routes and components to support new email verification flow. - Enhance error handling for unverified emails during login attempts. - Update configuration to include SMTP settings for email service.
This commit is contained in:
@@ -1,15 +1,43 @@
|
||||
import React from 'react';
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { View, Text, StyleSheet } from 'react-native';
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { useLocalSearchParams } from 'expo-router';
|
||||
import { Screen } from '@presentation/components/layout/Screen';
|
||||
import { Header } from '@presentation/components/layout/Header';
|
||||
import { Button } from '@presentation/components/ui/Button';
|
||||
import { colors, typography, spacing, radius } from '@shared/theme';
|
||||
import { resendVerificationUseCase } from '@infrastructure/container';
|
||||
|
||||
const COOLDOWN_SECS = 60;
|
||||
|
||||
/**
|
||||
* Tela de Verificação de E-mail — RU-0
|
||||
* Mostrada após o cadastro bem-sucedido.
|
||||
*/
|
||||
export default function VerifyEmailScreen(): React.ReactElement {
|
||||
export default function CheckEmailScreen(): React.ReactElement {
|
||||
const { email } = useLocalSearchParams<{ email: string }>();
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [cooldown, setCooldown] = useState(0);
|
||||
const [sent, setSent] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (cooldown <= 0) return;
|
||||
const timer = setTimeout(() => setCooldown((c) => c - 1), 1000);
|
||||
return () => clearTimeout(timer);
|
||||
}, [cooldown]);
|
||||
|
||||
async function handleResend(): Promise<void> {
|
||||
if (!email || cooldown > 0) return;
|
||||
setIsLoading(true);
|
||||
try {
|
||||
await resendVerificationUseCase.execute(email);
|
||||
setSent(true);
|
||||
setCooldown(COOLDOWN_SECS);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Screen>
|
||||
<Header title="Verificar e-mail" showBack />
|
||||
@@ -24,6 +52,27 @@ export default function VerifyEmailScreen(): React.ReactElement {
|
||||
Enviamos um link de verificação para o seu e-mail. Clique no link para ativar sua conta e poder fazer login.
|
||||
</Text>
|
||||
|
||||
{email && (
|
||||
<Button
|
||||
label={
|
||||
cooldown > 0
|
||||
? `Reenviar em ${cooldown}s`
|
||||
: sent
|
||||
? 'Reenviar e-mail'
|
||||
: 'Reenviar e-mail'
|
||||
}
|
||||
variant="secondary"
|
||||
onPress={handleResend}
|
||||
isLoading={isLoading}
|
||||
disabled={cooldown > 0}
|
||||
style={styles.resendButton}
|
||||
/>
|
||||
)}
|
||||
|
||||
{sent && cooldown > 0 && (
|
||||
<Text style={styles.sentText}>E-mail reenviado! Verifique sua caixa de entrada.</Text>
|
||||
)}
|
||||
|
||||
<View style={styles.hint}>
|
||||
<Ionicons name="information-circle-outline" size={16} color={colors.textSecondary} />
|
||||
<Text style={styles.hintText}>Não recebeu? Verifique sua pasta de spam.</Text>
|
||||
@@ -64,11 +113,20 @@ const styles = StyleSheet.create({
|
||||
textAlign: 'center',
|
||||
lineHeight: typography.fontSize.base * 1.6,
|
||||
},
|
||||
resendButton: {
|
||||
width: '100%',
|
||||
},
|
||||
sentText: {
|
||||
fontFamily: typography.fontFamily.ui,
|
||||
fontSize: typography.fontSize.sm,
|
||||
color: colors.accent,
|
||||
textAlign: 'center',
|
||||
},
|
||||
hint: {
|
||||
flexDirection: 'row',
|
||||
alignItems: 'center',
|
||||
gap: spacing[2],
|
||||
marginTop: spacing[4],
|
||||
marginTop: spacing[2],
|
||||
},
|
||||
hintText: {
|
||||
fontFamily: typography.fontFamily.ui,
|
||||
@@ -1,5 +1,6 @@
|
||||
import React, { useState } from 'react';
|
||||
import { View, Text, StyleSheet, TouchableOpacity, Alert } from 'react-native';
|
||||
import axios from 'axios';
|
||||
import { Link, useRouter } from 'expo-router';
|
||||
import { useForm, Controller } from 'react-hook-form';
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
@@ -42,8 +43,15 @@ export default function LoginScreen(): React.ReactElement {
|
||||
await setSession(session);
|
||||
router.replace('/(app)/(tabs)/home');
|
||||
} catch (err) {
|
||||
console.error('login error', err);
|
||||
Alert.alert('Erro', 'E-mail ou senha incorretos.');
|
||||
if (axios.isAxiosError(err) && err.response?.data?.code === 'EMAIL_NOT_VERIFIED') {
|
||||
Alert.alert(
|
||||
'E-mail não verificado',
|
||||
'Confirme seu e-mail antes de entrar. Verifique sua caixa de entrada.',
|
||||
[{ text: 'OK', onPress: () => router.push(`/(auth)/check-email?email=${encodeURIComponent(data.email)}`) }],
|
||||
);
|
||||
} else {
|
||||
Alert.alert('Erro', 'E-mail ou senha incorretos.');
|
||||
}
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import { Input } from '@presentation/components/ui/Input';
|
||||
import { Button } from '@presentation/components/ui/Button';
|
||||
import { colors, typography, spacing } from '@shared/theme';
|
||||
import { registerUseCase } from '@infrastructure/container';
|
||||
import { useAuthStore } from '@store/authStore';
|
||||
|
||||
const schema = z.object({
|
||||
email: z.string().email('E-mail inválido'),
|
||||
@@ -31,7 +30,6 @@ type FormData = z.infer<typeof schema>;
|
||||
*/
|
||||
export default function RegisterScreen(): React.ReactElement {
|
||||
const router = useRouter();
|
||||
const setSession = useAuthStore((s) => s.setSession);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
const { control, handleSubmit, formState: { errors } } = useForm<FormData>({
|
||||
@@ -42,9 +40,8 @@ export default function RegisterScreen(): React.ReactElement {
|
||||
async function onSubmit(data: FormData): Promise<void> {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const session = await registerUseCase.execute({ email: data.email, password: data.password });
|
||||
await setSession(session);
|
||||
router.replace('/(auth)/verify-email');
|
||||
await registerUseCase.execute({ email: data.email, password: data.password });
|
||||
router.replace(`/(auth)/check-email?email=${encodeURIComponent(data.email)}`);
|
||||
} catch {
|
||||
Alert.alert('Erro', 'Não foi possível criar sua conta.');
|
||||
} finally {
|
||||
|
||||
@@ -19,9 +19,10 @@ export default function RootLayout(): React.ReactElement | null {
|
||||
useEffect(() => {
|
||||
if (isLoading) return;
|
||||
const inAuthGroup = segments[0] === '(auth)';
|
||||
const inPublicRoute = segments[0] === 'verify-email' || segments[0] === 'filament';
|
||||
if (isAuthenticated && inAuthGroup) {
|
||||
router.replace('/(app)/(tabs)/home');
|
||||
} else if (!isAuthenticated && !inAuthGroup) {
|
||||
} else if (!isAuthenticated && !inAuthGroup && !inPublicRoute) {
|
||||
router.replace('/(auth)/login');
|
||||
}
|
||||
}, [isAuthenticated, isLoading, segments, router]);
|
||||
@@ -35,6 +36,8 @@ export default function RootLayout(): React.ReactElement | null {
|
||||
<Stack.Screen name="index" />
|
||||
<Stack.Screen name="(auth)" />
|
||||
<Stack.Screen name="(app)" />
|
||||
<Stack.Screen name="verify-email" />
|
||||
<Stack.Screen name="filament/[id]" />
|
||||
</Stack>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { View, Text, StyleSheet, ActivityIndicator } from 'react-native';
|
||||
import { useLocalSearchParams, useRouter } from 'expo-router';
|
||||
import { Ionicons } from '@expo/vector-icons';
|
||||
import { Screen } from '@presentation/components/layout/Screen';
|
||||
import { Button } from '@presentation/components/ui/Button';
|
||||
import { colors, typography, spacing, radius } from '@shared/theme';
|
||||
import { verifyEmailUseCase } from '@infrastructure/container';
|
||||
|
||||
type Status = 'loading' | 'success' | 'error';
|
||||
|
||||
/**
|
||||
* Handler de deep link: meowspool://verify-email?token=xxx
|
||||
* Chama o backend para confirmar o e-mail e exibe o resultado.
|
||||
*/
|
||||
export default function VerifyEmailDeepLink(): React.ReactElement {
|
||||
const { token } = useLocalSearchParams<{ token: string }>();
|
||||
const router = useRouter();
|
||||
const [status, setStatus] = useState<Status>('loading');
|
||||
const [errorMessage, setErrorMessage] = useState('Não foi possível verificar o e-mail.');
|
||||
|
||||
useEffect(() => {
|
||||
if (!token) {
|
||||
setErrorMessage('Link inválido ou expirado.');
|
||||
setStatus('error');
|
||||
return;
|
||||
}
|
||||
|
||||
verifyEmailUseCase.execute(token)
|
||||
.then(() => setStatus('success'))
|
||||
.catch((e: unknown) => {
|
||||
const msg = e instanceof Error ? e.message : 'Erro desconhecido.';
|
||||
setErrorMessage(msg);
|
||||
setStatus('error');
|
||||
});
|
||||
}, [token]);
|
||||
|
||||
return (
|
||||
<Screen>
|
||||
<View style={styles.content}>
|
||||
{status === 'loading' && (
|
||||
<>
|
||||
<ActivityIndicator size="large" color={colors.accent} />
|
||||
<Text style={styles.message}>Verificando e-mail…</Text>
|
||||
</>
|
||||
)}
|
||||
|
||||
{status === 'success' && (
|
||||
<>
|
||||
<View style={[styles.iconWrapper, styles.iconSuccess]}>
|
||||
<Ionicons name="checkmark-circle-outline" size={64} color={colors.accent} />
|
||||
</View>
|
||||
<Text style={styles.title}>E-mail verificado!</Text>
|
||||
<Text style={styles.message}>Sua conta está ativa. Faça login para continuar.</Text>
|
||||
<Button
|
||||
label="Ir para o login"
|
||||
onPress={() => router.replace('/(auth)/login')}
|
||||
style={styles.cta}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
|
||||
{status === 'error' && (
|
||||
<>
|
||||
<View style={[styles.iconWrapper, styles.iconError]}>
|
||||
<Ionicons name="close-circle-outline" size={64} color={colors.error ?? '#FF6B6B'} />
|
||||
</View>
|
||||
<Text style={styles.title}>Falha na verificação</Text>
|
||||
<Text style={styles.message}>{errorMessage}</Text>
|
||||
<Button
|
||||
label="Voltar ao login"
|
||||
onPress={() => router.replace('/(auth)/login')}
|
||||
style={styles.cta}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</View>
|
||||
</Screen>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
content: {
|
||||
flex: 1,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: spacing[4],
|
||||
paddingHorizontal: spacing[6],
|
||||
},
|
||||
iconWrapper: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
borderRadius: radius.xl,
|
||||
backgroundColor: colors.bgSurface,
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
marginBottom: spacing[2],
|
||||
},
|
||||
iconSuccess: {},
|
||||
iconError: {},
|
||||
title: {
|
||||
fontFamily: typography.fontFamily.ui,
|
||||
fontSize: typography.fontSize.xl,
|
||||
fontWeight: typography.fontWeight.bold,
|
||||
color: colors.textPrimary,
|
||||
textAlign: 'center',
|
||||
},
|
||||
message: {
|
||||
fontFamily: typography.fontFamily.ui,
|
||||
fontSize: typography.fontSize.base,
|
||||
color: colors.textSecondary,
|
||||
textAlign: 'center',
|
||||
lineHeight: typography.fontSize.base * 1.6,
|
||||
},
|
||||
cta: { marginTop: spacing[2], width: '100%' },
|
||||
});
|
||||
Reference in New Issue
Block a user