-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from No-Country-simulation/front-lazaro
login and register form connected to API
- Loading branch information
Showing
11 changed files
with
535 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
API_URL=https://clinica-medica-production.up.railway.app/api/v1/ | ||
API_URL=https://clinica-medica-production.up.railway.app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use server' | ||
import { schemaLogin } from '@/schemas' | ||
|
||
const BASE_URL = process.env.API_URL | ||
|
||
export const loginUser = async (formData: FormData) => { | ||
const url = BASE_URL + '/user' | ||
|
||
const nameFromForm = formData.get('name') as string | ||
const passwordFromForm = formData.get('password') as string | ||
|
||
const validatedFields = schemaLogin.safeParse({ | ||
name: nameFromForm, | ||
password: passwordFromForm, | ||
}) | ||
|
||
if (!validatedFields.success) { | ||
return { | ||
errors: validatedFields.error.flatten().fieldErrors, | ||
message: 'Debe rellenar todos los campos. Error al iniciar sesión.', | ||
} | ||
} | ||
|
||
const body = { | ||
name: nameFromForm, | ||
password: passwordFromForm, | ||
} | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(body), | ||
}) | ||
|
||
const responseData = await response.json() | ||
|
||
if (!responseData) { | ||
return { | ||
errors: {}, | ||
loginError: null, | ||
message: 'Algo salió mal...', | ||
} | ||
} | ||
|
||
if (responseData.error) { | ||
return { | ||
errors: {}, | ||
loginError: responseData.error, | ||
message: 'Error al iniciar sesión', | ||
} | ||
} | ||
|
||
return { | ||
success: 'Inicio de sesión exitoso', | ||
} | ||
} catch (error) { | ||
return { | ||
errors: {}, | ||
loginError: 'Error al comunicarse con el servidor' + error, | ||
message: 'Algo salió mal durante el inicio de sesión.' + error, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,79 @@ | ||
"use server" | ||
'use server' | ||
import { schemaRegister } from '@/schemas' | ||
|
||
const BASE_URL = process.env.API_URL | ||
|
||
export const createUser = async () => { | ||
export const createUser = async (formData: FormData) => { | ||
const url = BASE_URL + '/patients/create' | ||
|
||
const nameFromForm = formData.get('name') as string | ||
const emailFromForm = formData.get('email') as string | ||
const passwordFromForm = formData.get('password') as string | ||
const phoneFromForm = formData.get('phone') as string | ||
const insurerFromForm = formData.get('insurer') as string | ||
|
||
const validatedFields = schemaRegister.safeParse({ | ||
name: nameFromForm, | ||
email: emailFromForm, | ||
password: passwordFromForm, | ||
phone: phoneFromForm, | ||
insurer: insurerFromForm, | ||
}) | ||
|
||
if (!validatedFields.success) { | ||
return { | ||
errors: validatedFields.error.flatten().fieldErrors, | ||
message: 'Debe rellenar todos los campos. Error al Registrarse.', | ||
} | ||
} | ||
|
||
const body = { | ||
user: { | ||
name: nameFromForm, | ||
email: emailFromForm, | ||
password: passwordFromForm, | ||
phone: phoneFromForm, | ||
img: 'https://res.cloudinary.com/db395v0wf/image/upload/v1729121057/vooufndzyzyyfnyi4zwv.png', | ||
active: true, | ||
}, | ||
insurer: insurerFromForm, | ||
} | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(body), | ||
}) | ||
|
||
const responseData = await response.json() | ||
|
||
if (!responseData) { | ||
return { | ||
errors: {}, | ||
registerError: null, | ||
message: 'Algo salio mal...', | ||
} | ||
} | ||
|
||
if (responseData.message) { | ||
return { | ||
errors: {}, | ||
registerError: responseData.message, | ||
message: 'Error al registrarse', | ||
} | ||
} | ||
|
||
return { | ||
success: 'Registro exitoso', | ||
} | ||
} catch (error) { | ||
return { | ||
errors: {}, | ||
registerError: 'Error al comunicarse con el servidor' + error, | ||
message: 'Algo salió mal durante el registro.' + error, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,25 @@ | ||
import { z } from "zod" | ||
import { z } from 'zod' | ||
|
||
export const schemaRegister = z.object({ | ||
userName: z.string({ message: "Name required" }).min(8, { message: "first and last name" }), | ||
email: z.string({ message: "Email required" }).email({ message: "Email invalid" }), | ||
dni: z.string({ message: "DNI required" }).min(8, ({ message: "enter a valid DNI" })).regex(/^\d+$/, { message: "DNI must contain only numbers" }), | ||
city: z.string(), | ||
direction: z.string(), | ||
password: z.string({ message: "Password required" }).min(6, { message: "min 6 character" }), | ||
name: z | ||
.string({ message: 'Nombre requerido' }) | ||
.min(8, { message: 'Rellene con su Nombre completo' }), | ||
email: z | ||
.string({ message: 'Email requerido' }) | ||
.email({ message: 'Email invalido' }), | ||
phone: z | ||
.string({ message: 'Telefono requerido' }) | ||
.min(10, { message: 'Numero de telefono debe tener al menos 10 digitos' }) | ||
.regex(/^\d+$/, { message: 'Phone must contain only numbers' }), | ||
insurer: z.string({ message: 'Aseguradora u Obra social requerida' }), | ||
password: z | ||
.string({ message: 'Contraseña requerido' }) | ||
.min(6, { message: 'Minimo 6 Caracteres' }), | ||
}) | ||
|
||
export const schemaLogin = z.object({ | ||
email: z.string({ message: "Email required" }).email({ message: "Email invalid" }), | ||
password: z.string({ message: "Password required" }).min(6, { message: "min 6 character" }), | ||
}) | ||
name: z.string({ message: 'Nombre requerido' }), | ||
password: z | ||
.string({ message: 'Contraseña requerido' }) | ||
.min(6, { message: 'Minimo 6 Caracteres' }), | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.