From a895f83db76dd933f6f3735c62e4238ced18a2de Mon Sep 17 00:00:00 2001 From: 230417 <230417@epvc.pt> Date: Tue, 3 Feb 2026 15:23:04 +0000 Subject: [PATCH] Remove unnecessary blank lines in ShopCard and Explore components --- web/src/components/ShopCard.tsx | 1 + web/src/lib/events.ts | 18 +++-- web/src/pages/AuthRegister.tsx | 132 +++++++++++++++++--------------- web/src/pages/EventsCreate.tsx | 88 +++++++++++++++------ web/src/pages/Explore.tsx | 1 - web/src/pages/Profile.tsx | 14 +++- web/src/routes.tsx | 3 +- 7 files changed, 161 insertions(+), 96 deletions(-) diff --git a/web/src/components/ShopCard.tsx b/web/src/components/ShopCard.tsx index fa0ed30..b1fcc1c 100644 --- a/web/src/components/ShopCard.tsx +++ b/web/src/components/ShopCard.tsx @@ -57,3 +57,4 @@ export const ShopCard = ({ shop }: { shop: BarberShop }) => ( + diff --git a/web/src/lib/events.ts b/web/src/lib/events.ts index 342966b..9d38a45 100644 --- a/web/src/lib/events.ts +++ b/web/src/lib/events.ts @@ -9,21 +9,27 @@ export type EventRow = { user_id: string } -// LISTAR eventos (RLS deve garantir que só vês os teus) +/** + * LISTAR eventos do utilizador autenticado + * (RLS garante isolamento por user_id) + */ export async function listEvents() { const user = await getUser() if (!user) throw new Error('Utilizador não autenticado') const { data, error } = await supabase .from('events') - .select('id,title,description,date,user_id') + .select('id, title, description, date, user_id') .order('date', { ascending: true }) if (error) throw error return (data ?? []) as EventRow[] } -// CRIAR evento +/** + * CRIAR evento + * user_id vem automaticamente de auth.uid() + */ export async function createEvent(input: { title: string description?: string @@ -38,11 +44,9 @@ export async function createEvent(input: { title: input.title, description: input.description ?? null, date: input.date, - // user_id: - // - Se a tua coluna user_id tem DEFAULT auth.uid(), NÃO metas aqui. - // - Se não tem default, então mete: user_id: user.id + // ❌ NÃO enviar user_id se tiver DEFAULT auth.uid() }) - .select('id,title,description,date,user_id') + .select('id, title, description, date, user_id') .single() if (error) throw error diff --git a/web/src/pages/AuthRegister.tsx b/web/src/pages/AuthRegister.tsx index e5031e1..8467829 100644 --- a/web/src/pages/AuthRegister.tsx +++ b/web/src/pages/AuthRegister.tsx @@ -1,5 +1,5 @@ -import { FormEvent, useState } from 'react' -import { useNavigate, Link } from 'react-router-dom' +import { FormEvent, useEffect, useState } from 'react' +import { Link, useNavigate } from 'react-router-dom' import { Input } from '../components/ui/input' import { Button } from '../components/ui/button' import { Card } from '../components/ui/card' @@ -17,61 +17,66 @@ export default function AuthRegister() { const navigate = useNavigate() - const onSubmit = async (e: FormEvent) => { + // 🔐 Se já estiver logado, não pode ver o registo + useEffect(() => { + let mounted = true + ;(async () => { + const { data } = await supabase.auth.getSession() + if (!mounted) return + if (data.session) { + navigate('/explorar', { replace: true }) + } + })() + return () => { + mounted = false + } + }, [navigate]) + + async function onSubmit(e: FormEvent) { e.preventDefault() setError('') if (!name.trim()) return setError('Preencha o nome completo') if (!email.trim()) return setError('Preencha o email') if (!password.trim()) return setError('Preencha a senha') - if (role === 'barbearia' && !shopName.trim()) return setError('Informe o nome da barbearia') - - setLoading(true) + if (role === 'barbearia' && !shopName.trim()) { + return setError('Informe o nome da barbearia') + } try { - // 1) Criar conta no Supabase Auth - const { data, error: signUpError } = await supabase.auth.signUp({ + setLoading(true) + + const { data, error } = await supabase.auth.signUp({ email, password, options: { data: { - name, + name: name.trim(), role, - shop_name: role === 'barbearia' ? shopName : null, + shopName: role === 'barbearia' ? shopName.trim() : null, }, }, }) - if (signUpError) throw signUpError - const userId = data.user?.id - if (!userId) { - // Em alguns casos o user pode exigir confirmação por email - // Mesmo assim, mostramos uma mensagem útil. - throw new Error('Conta criada, mas sessão não iniciada. Verifica o email para confirmar a conta.') + if (error) throw error + + // 🔔 Se confirmação de email estiver ON + if (!data.session) { + navigate('/login', { + replace: true, + state: { + msg: 'Conta criada! Confirma o email antes de fazer login.', + }, + }) + return } - // 2) Criar perfil na tabela public.profiles - const { error: profileError } = await supabase - .from('profiles') - .upsert( - { - id: userId, - name, - role, - shop_name: role === 'barbearia' ? shopName : null, - }, - { onConflict: 'id' } - ) - if (profileError) throw profileError - - // 3) Redirecionar - navigate('/explorar', { replace: true }) - } catch (e: any) { - // Mensagens comuns - const msg = - e?.message || - (typeof e === 'string' ? e : 'Erro ao criar conta') - setError(msg) + // ✅ Login automático + navigate(role === 'barbearia' ? '/painel' : '/explorar', { + replace: true, + }) + } catch (err: any) { + setError(err?.message || 'Erro ao criar conta') } finally { setLoading(false) } @@ -84,8 +89,12 @@ export default function AuthRegister() {
Escolha o tipo de acesso
++ Escolha o tipo de acesso +
{error && ( @@ -95,9 +104,11 @@ export default function AuthRegister() { )}