113 lines
4.1 KiB
Vue
113 lines
4.1 KiB
Vue
<template>
|
|
<Head>
|
|
<Title>Inscription</Title>
|
|
</Head>
|
|
<div class="flex flex-1 flex-col justify-center items-center">
|
|
<ProseH4>Inscription</ProseH4>
|
|
<form @submit.prevent="() => submit()" class="flex flex-1 flex-col justify-center items-stretch p-4">
|
|
<TextInput type="text" label="Nom d'utilisateur" autocomplete="username" v-model="state.username"/>
|
|
<TextInput type="email" label="Email" autocomplete="email" v-model="state.email"/>
|
|
<TextInput type="password" label="Mot de passe" autocomplete="new-password" v-model="state.password"/>
|
|
<div class="flex flex-col font-light border border-light-35 dark:border-dark-35 px-4 py-2 me-4">
|
|
<span class="">Votre mot de passe doit respecter les critères de sécurité suivants
|
|
:</span>
|
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLength}">Entre 8 et 128
|
|
caractères</span>
|
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedLowerUpper}">Au moins
|
|
une minuscule et une majuscule</span>
|
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedDigit}">Au moins un
|
|
chiffre</span>
|
|
<span class="px-4" :class="{'text-light-red dark:text-dark-red': !checkedSymbol}">Au moins un
|
|
caractère spécial parmis la liste suivante:
|
|
<pre class="text-wrap">! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ ] ^ _ ` { | } ~</pre>
|
|
</span>
|
|
</div>
|
|
<TextInput type="password" label="Confirmation du mot de passe" autocomplete="new-password" v-model="confirmPassword"/>
|
|
<Button class="border border-light-35 dark:border-dark-35 self-center" :loading="status === 'pending'">Se connecter</Button>
|
|
<NuxtLink class="mt-4 text-center block text-sm font-semibold tracking-wide hover:text-accent-blue" :to="{ path: `/user/register`, force: true }">Pas de compte ?</NuxtLink>
|
|
</form>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ZodError } from 'zod';
|
|
import { schema, type Registration } from '~/schemas/registration';
|
|
|
|
definePageMeta({
|
|
layout: 'login',
|
|
});
|
|
|
|
const state = reactive<Registration>({
|
|
username: '',
|
|
email: '',
|
|
password: ''
|
|
});
|
|
|
|
const confirmPassword = ref("");
|
|
|
|
const checkedLength = computed(() => state.password.length >= 8 && state.password.length <= 128);
|
|
const checkedLowerUpper = computed(() => state.password.toLowerCase() !== state.password && state.password.toUpperCase() !== state.password);
|
|
const checkedDigit = computed(() => /[0-9]/.test(state.password));
|
|
const checkedSymbol = computed(() => " !\"#$%&'()*+,-./:;<=>?@[]^_`{|}~".split("").some(e => state.password.includes(e)));
|
|
|
|
const usernameError = ref("");
|
|
const emailError = ref("");
|
|
const generalError = ref("");
|
|
|
|
const { data: result, status, error, execute } = await useFetch('/api/auth/register', {
|
|
body: state,
|
|
immediate: false,
|
|
method: 'POST',
|
|
watch: false,
|
|
ignoreResponseError: true,
|
|
})
|
|
|
|
async function submit()
|
|
{
|
|
if(state.password === "" || state.password !== confirmPassword.value)
|
|
return;
|
|
|
|
const data = schema.safeParse(state);
|
|
|
|
if(data.success)
|
|
{
|
|
await execute();
|
|
debugger;
|
|
|
|
const login = result.value;
|
|
if(!login || !login.success)
|
|
{
|
|
handleErrors(login?.error ?? error.value!);
|
|
}
|
|
else if(status.value === 'success' && login.success)
|
|
{
|
|
await navigateTo('/user/profile');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
handleErrors(data.error);
|
|
}
|
|
}
|
|
function handleErrors(error: Error | ZodError)
|
|
{
|
|
if(error.hasOwnProperty('issues'))
|
|
{
|
|
for(const err of (error as ZodError).issues)
|
|
{
|
|
if(err.path.includes('username'))
|
|
{
|
|
usernameError.value = err.message;
|
|
}
|
|
if(err.path.includes('email'))
|
|
{
|
|
emailError.value = err.message;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
generalError.value = error?.message ?? 'Erreur inconnue.';
|
|
}
|
|
}
|
|
</script> |