Skip to content

Commit

Permalink
Merge pull request #2 from Varkoff/04-authentication
Browse files Browse the repository at this point in the history
Add Register Route
  • Loading branch information
Varkoff authored Apr 20, 2024
2 parents a49bba8 + 44da9aa commit abad676
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
6 changes: 2 additions & 4 deletions backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Controller, Get, Next, Post, Redirect, Req, Res, UseGuards } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';
import { NextFunction, Response } from 'express';
import { LocalAuthGuard } from './local-auth.guard';

@Controller()
export class AuthController {
@UseGuards(LocalAuthGuard)
@Get('/authenticate')
@Redirect('/')
async login(@Req() req: Request) {
console.log({ requestUser: req.user });
// return req.user;
login() {
}

@Post('auth/logout')
Expand Down
1 change: 1 addition & 0 deletions frontend/app/routes/_public+/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const loader = async ({ request, context }: LoaderFunctionArgs) => {

export const action = async ({ request, context }: ActionFunctionArgs) => {
const formData = await request.formData();

const submission = await parseWithZod(formData, {
async: true,
schema: LoginSchema.superRefine(async (data, ctx) => {
Expand Down
53 changes: 30 additions & 23 deletions frontend/app/routes/_public+/register.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { getFormProps, getInputProps, useForm } from '@conform-to/react';
import { getZodConstraint, parseWithZod } from '@conform-to/zod';
import { json, redirect, type ActionFunctionArgs, type LoaderFunctionArgs } from '@remix-run/node';
import {
json,
redirect,
type ActionFunctionArgs,
type LoaderFunctionArgs,
} from '@remix-run/node';
import { Form, useActionData, useNavigation } from '@remix-run/react';
import { z } from 'zod';
import { Field } from '~/components/forms';
import { Button } from '~/components/ui/button';
import { getOptionalUser } from '~/server/auth.server';

export const loader = async ({ context }: LoaderFunctionArgs) => {
const user = await getOptionalUser({ context })
const user = await getOptionalUser({ context });
if (user) {
return redirect('/')
return redirect('/');
}
return null;
};


const RegisterSchema = z.object({
email: z
.string({
Expand All @@ -24,15 +28,14 @@ const RegisterSchema = z.object({
.email({
message: 'Cet email est invalide.',
}),
name: z.string({
required_error: "Le prénom est obligatoire"
firstname: z.string({
required_error: 'Le prénom est obligatoire',
}),
password: z.string({ required_error: 'Le mot de passe est obligatoire.' }),
});

export const action = async ({ request, context }: ActionFunctionArgs) => {
const formData = await request.formData();

const submission = await parseWithZod(formData, {
async: true,
schema: RegisterSchema.superRefine(async (data, ctx) => {
Expand All @@ -48,7 +51,7 @@ export const action = async ({ request, context }: ActionFunctionArgs) => {
ctx.addIssue({
code: 'custom',
path: ['email'],
message: "Cet utilisateur existe déjà.",
message: 'Cet utilisateur existe déjà.',
});
}
}),
Expand All @@ -62,24 +65,24 @@ export const action = async ({ request, context }: ActionFunctionArgs) => {
}
);
}
const { email, name, password } = submission.value
const { email, firstname, password } = submission.value;

const { email: createdUserEmail } = await context.remixService.auth.createUser({
email,
name,
password,
});
const { email: createdUserEmail } =
await context.remixService.auth.createUser({
email,
name: firstname,
password,
});

const { sessionToken } = await context.remixService.auth.authenticateUser({
email: createdUserEmail
})
email: createdUserEmail,
});

// Connecter l'utilisateur associé à l'email
return redirect(`/authenticate?token=${sessionToken}`)
return redirect(`/authenticate?token=${sessionToken}`);
};

export default function Register() {

const actionData = useActionData<typeof action>();
const [form, fields] = useForm({
constraint: getZodConstraint(RegisterSchema),
Expand All @@ -88,23 +91,27 @@ export default function Register() {
schema: RegisterSchema,
});
},
lastResult: actionData?.result
lastResult: actionData?.result,
});

const isLoading = useNavigation().state === 'submitting';
return (
<div className='max-w-[600px] mx-auto'>
<h1>Création de compte</h1>
<Form {...getFormProps(form)} method='POST' reloadDocument className='flex flex-col gap-2'>

<Form
{...getFormProps(form)}
method='POST'
reloadDocument
className='flex flex-col gap-2'
>
<Field
inputProps={getInputProps(fields.name, {
inputProps={getInputProps(fields.firstname, {
type: 'text',
})}
labelsProps={{
children: 'Votre prénom',
}}
errors={fields.name.errors}
errors={fields.firstname.errors}
/>

<Field
Expand Down
11 changes: 0 additions & 11 deletions frontend/app/routes/api.tsx

This file was deleted.

0 comments on commit abad676

Please sign in to comment.