Skip to content
Extraits de code Groupes Projets
Valider 6f8718fb rédigé par DavePk04's avatar DavePk04
Parcourir les fichiers

fix tutor registration flow

parent 74db42cb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -8,8 +8,10 @@ export const load: ServerLoad = async ({ locals, url }) => {
redirect(303, '/login');
}
const user = JSON.parse(locals.user);
// const user = JSON.parse(locals.user);
const user = typeof locals.user === 'string' ? JSON.parse(locals.user) : locals.user;
console.log('Parsed user: ', user);
if (user == null || user == undefined || user.type > 1) {
error(403, 'Forbidden');
}
};
};
\ No newline at end of file
......@@ -5,7 +5,7 @@
import { onMount } from 'svelte';
import Timeslots from '$lib/components/users/timeslots.svelte';
import User from '$lib/types/user';
import { getUsersAPI, patchUserAPI, getUserContactsAPI } from '$lib/api/users';
import { getUsersAPI, patchUserAPI, getUserContactsAPI, createUserAPI} from '$lib/api/users';
import { Icon, Envelope, Key, UserCircle, Calendar, QuestionMarkCircle } from 'svelte-hero-icons';
import Typingtest from '$lib/components/tests/typingtest.svelte';
import { formatToUTCDate } from '$lib/utils/date';
......@@ -54,44 +54,57 @@
let timeslots = 0n;
async function onRegister() {
if (nickname == '' || email == '' || password == '' || confirmPassword == '') {
message = $t('register.error.emptyFields');
return;
}
if (password.length < 8) {
message = $t('register.error.passwordRules');
return;
}
if (password != confirmPassword) {
message = $t('register.error.differentPasswords');
return;
}
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailRegex.test(email)) {
message = $t('register.error.emailRules');
return;
}
message = '';
const registerRes = await registerAPI(email, password, nickname, true);
if (registerRes !== 'OK') {
message = registerRes;
// Basic validation
if (nickname == '' || email == '' || password == '' || confirmPassword == '') {
message = $t('register.error.emptyFields');
return;
}
if (password.length < 8) {
message = $t('register.error.passwordRules');
return;
}
if (password != confirmPassword) {
message = $t('register.error.differentPasswords');
return;
}
const emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/;
if (!emailRegex.test(email)) {
message = $t('register.error.emailRules');
return;
}
message = '';
try {
const response = await fetch('http://127.0.0.1:8000/tmp-api/v1/auth/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
email,
password,
nickname,
is_tutor: true
})
});
if (response.status === 201) {
const userId = await response.text();
console.log('User created successfully with ID:', userId);
message = $t('register.success');
return;
}
} else {
const errorData = await response.json();
console.error('Registration failed:', errorData);
message = errorData.detail || $t('register.error.generic');
}
} catch (error) {
console.error('Error during registration:', error);
message = $t('register.error.generic');
}
}
const loginRes = await loginAPI(email, password);
if (loginRes !== 'OK') {
toastAlert('Failed to login: ' + loginRes);
document.location.href = '/login';
return;
}
document.location.href = '/tutor/register';
message = 'OK';
}
async function onData() {
const user_id = user.id;
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter