Skip to content

Commit 21df693

Browse files
authored
Merge pull request #63 from knowlyai/feature/auth
Feature/auth
2 parents 7a486c2 + 4c508f0 commit 21df693

21 files changed

Lines changed: 468 additions & 102 deletions

File tree

package-lock.json

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"react-icons": "^5.5.0",
3636
"react-router-dom": "^7.5.3",
3737
"tailwindcss": "^4.1.5",
38+
"uuidv4": "^6.2.13",
3839
"zod": "^3.25.28"
3940
},
4041
"devDependencies": {

src/features/login/pages/login.tsx

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,38 @@ import {
2828
Form
2929
} from '@/shared/components/form'
3030
import { Input } from '@/shared/components/input'
31-
import { Link } from 'react-router-dom'
31+
import { Link, useNavigate } from 'react-router-dom'
3232
import { containerVariants, cardVariants } from '@/shared/utils/animations'
33+
import { useLoginUserMutation } from '@/shared/hooks/use-user'
34+
import toast from 'react-hot-toast'
35+
import { AxiosError } from 'axios'
3336

3437
export function LoginPage() {
3538
const [showPassword, setShowPassword] = useState(false)
39+
const { mutateAsync: loginUser, isPending } = useLoginUserMutation()
40+
const navigate = useNavigate()
3641

3742
const form = useForm<LoginFormData>({
3843
resolver: zodResolver(loginFormSchema),
3944
defaultValues: loginFormInitialValues,
4045
mode: 'onBlur'
4146
})
4247

43-
const onSubmit = (values: LoginFormData) => {
44-
console.log(values)
48+
const onSubmit = async (values: LoginFormData) => {
49+
try {
50+
const response = await loginUser(values)
51+
localStorage.setItem('token', response.id_token)
52+
toast.success('Login realizado com sucesso!')
53+
navigate('/bases', {
54+
replace: true
55+
})
56+
} catch (err) {
57+
const errorMessage =
58+
err instanceof AxiosError && err.response?.data?.message
59+
? err.response.data.message
60+
: 'Erro ao fazer login'
61+
toast.error(errorMessage)
62+
}
4563
}
4664

4765
return (
@@ -116,8 +134,12 @@ export function LoginPage() {
116134
)}
117135
/>
118136
<CardFooter className="flex flex-col items-center justify-center pt-4 pb-0">
119-
<Button type="submit" className="w-full">
120-
Login
137+
<Button
138+
type="submit"
139+
className="w-full"
140+
disabled={isPending}
141+
>
142+
{isPending ? 'Entrando...' : 'Login'}
121143
</Button>
122144
<div className="text-foreground/70 mt-2 text-center text-sm">
123145
Ainda não tem uma conta?{' '}

src/features/playground/pages/playground.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export function PlaygroundPage() {
177177
</div>
178178

179179
{/* Chat Container */}
180-
<Card className="flex min-h-0 flex-1 flex-col overflow-hidden">
180+
<Card className="flex min-h-0 max-w-2xl flex-1 flex-col overflow-hidden">
181181
<CardHeader className="flex-shrink-0 pb-3">
182182
<CardTitle className="flex items-center gap-2">
183183
<Bot className="h-5 w-5" />
@@ -231,7 +231,7 @@ export function PlaygroundPage() {
231231
: 'bg-muted text-foreground'
232232
}`}
233233
>
234-
<p className="text-sm whitespace-pre-wrap">
234+
<p className="p-[6px] text-sm whitespace-pre-wrap">
235235
{message.content}
236236
</p>
237237
<p className={`mt-1 text-xs opacity-70`}>

src/features/sign-up/pages/sign-up-page.tsx

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ import { motion } from 'framer-motion'
3838
import { Eye, EyeOff } from 'lucide-react'
3939
import { useState } from 'react'
4040
import { containerVariants, cardVariants } from '@/shared/utils/animations'
41+
import { useCreateUserMutation } from '@/shared/hooks/use-user'
42+
import { AxiosError } from 'axios'
43+
import { useNavigate } from 'react-router-dom'
4144

4245
export function SignUpPage() {
4346
const [showPassword, setShowPassword] = useState(false)
4447
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
48+
const navigate = useNavigate()
49+
50+
const { mutateAsync: createUser, isPending } = useCreateUserMutation()
4551

4652
const form = useForm<SignUpFormData>({
4753
resolver: zodResolver(signUpFormSchema),
@@ -73,9 +79,39 @@ export function SignUpPage() {
7379
onChange(formattedValue)
7480
}
7581

76-
function onSubmit(values: SignUpFormData) {
77-
console.log(values)
78-
toast.success('Conta criada com sucesso!')
82+
async function onSubmit(values: SignUpFormData) {
83+
try {
84+
const isIndividual = values.documentType === 'individual'
85+
86+
// Convert birthDate to seconds since epoch if provided
87+
const birthDateSeconds = values.birthDate
88+
? Math.floor(values.birthDate.getTime() / 1000)
89+
: undefined
90+
91+
await createUser({
92+
name: values.name,
93+
email: values.email,
94+
password: values.password,
95+
cellphone: values.phone,
96+
personType: isIndividual ? 'PF' : 'PJ',
97+
cpfCnpj: values.document.replace(/\D/g, ''),
98+
birthDate: birthDateSeconds,
99+
plan: 'Bronze'
100+
})
101+
102+
form.reset()
103+
toast.success(
104+
'Conta criada! Confirme seu cadastro pelo link enviado para o seu e-mail. Você será redirecionado para a página de login.',
105+
{ duration: 10000 }
106+
)
107+
setTimeout(() => {
108+
navigate('/login', { replace: true })
109+
}, 10000)
110+
} catch (err) {
111+
const errorMessage =
112+
((err as AxiosError).response?.data as string) || 'Erro ao fazer login'
113+
toast.error(errorMessage)
114+
}
79115
}
80116

81117
return (
@@ -326,7 +362,9 @@ export function SignUpPage() {
326362
/>
327363
</CardContent>
328364
<CardFooter className="justify-center">
329-
<Button type="submit">Criar conta</Button>
365+
<Button type="submit" disabled={isPending}>
366+
{isPending ? 'Criando...' : 'Criar conta'}
367+
</Button>
330368
</CardFooter>
331369
</Card>
332370
</motion.div>

src/features/user-area/components/knowledge-base-detail-modal.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function KnowledgeBaseDetailModal({
3535
const getStatusColor = (status: string) => {
3636
switch (status.toLowerCase()) {
3737
case 'active':
38-
return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-300'
38+
return 'bg-green-600'
3939
case 'processing':
4040
return 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-300'
4141
case 'error':

src/features/user-area/pages/create-base-pipeline.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react'
1+
import { useEffect, useRef, useState } from 'react'
22
import { useLocation, useNavigate } from 'react-router-dom'
33
import { motion } from 'framer-motion'
44
import {
@@ -48,6 +48,9 @@ export function CreateBasePipeline() {
4848

4949
const createKnowledgeBaseMutation = useCreateKnowledgeBaseMutation()
5050

51+
// Guard to prevent double-run under React StrictMode in development
52+
const didRunRef = useRef(false)
53+
5154
const [pipeline, setPipeline] = useState<PipelineState>({
5255
currentStep: 0,
5356
steps: [
@@ -239,6 +242,9 @@ export function CreateBasePipeline() {
239242
}
240243

241244
useEffect(() => {
245+
if (didRunRef.current) return
246+
didRunRef.current = true
247+
242248
if (formData) {
243249
executeSteps()
244250
} else {

src/features/user-area/pages/create-base.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { motion } from 'framer-motion'
55
import { Save, ArrowLeft } from 'lucide-react'
66
import { useNavigate } from 'react-router-dom'
77
import toast from 'react-hot-toast'
8-
8+
import { v4 as uuidv4 } from 'uuid'
99
import { Background } from '@/shared/components/background'
1010
import { Layout } from '@/shared/components/layout'
1111
import { Button } from '@/shared/components/button'
@@ -64,7 +64,7 @@ export function CreateBase() {
6464
.replace(/[\u0300-\u036f]/g, '') // Remove diacritics
6565
.replace(/[^a-z0-9\s]/g, '-')
6666
.replace(/\s+/g, '-')
67-
data.slug = slug
67+
data.slug = slug.concat('-', uuidv4().slice(0, 8)) // Append random string to ensure uniqueness
6868
// Navigate to pipeline page with form data
6969
navigate('/bases/create/pipeline', { state: { formData: data } })
7070
} catch (error) {

0 commit comments

Comments
 (0)