feat: API login

This commit is contained in:
Sebastian Pravda 2022-11-29 11:00:14 +01:00 committed by EETagent
parent bb6eaf65a0
commit cf234d2f73
3 changed files with 90 additions and 26 deletions

View file

@ -1,26 +1,32 @@
import axios from "axios";
import type { CandidateLogin } from "src/stores/candidate";
import type { CandidateData, CandidateLogin } from "src/stores/candidate";
import { API_URL, errorHandler } from ".";
export async function apiLogin(data: CandidateLogin): Promise<boolean> {
axios.post(API_URL + '/candidate/login', data, {withCredentials: true}).then((res) => {
console.log(res);
return res.status === 200;
}).catch((err) => {
throw errorHandler(err, "Login failed");
});
return false;
export async function apiLogin(data: CandidateLogin): Promise<number> {
try {
let res = await axios.post(API_URL + '/candidate/login', data, {withCredentials: true});
return data.application_id;
} catch (e: any) {
throw errorHandler(e, "Login failed");
}
}
// TODO
export async function apiLogout(): Promise<boolean> {
export async function apiLogout() {
try {
const res = await axios.get(API_URL + '/candidate/logout', {
withCredentials: true
});
return res.status === 200;
} catch (error) {
return false;
await axios.post(API_URL + '/candidate/logout', {withCredentials: true});
} catch (e: any) {
throw errorHandler(e, "Logout failed");
}
}
export async function apiFillDetails(data: CandidateData): Promise<CandidateData> {
console.log(data);
try {
let res = await axios.post(API_URL + '/candidate/details', data, {withCredentials: true});
return res.data;
} catch (e: any) {
throw errorHandler(e, "Failed to fill details");
}
}

View file

@ -5,6 +5,7 @@
import { onMount } from 'svelte';
import { page } from '$app/stores';
import { login } from '../../../stores/candidate';
import { goto } from '$app/navigation';
let applicationId = Number($page.params.code);
@ -38,10 +39,19 @@
};
$: if (codeValueArray.length === 8) {
login({application_id: applicationId, password: codeValueMobile});
// alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile);
submit();
};
async function submit() {
try {
await login({application_id: applicationId, password: codeValueMobile});
goto("/register");
} catch (e) {
console.error(e);
}
// alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile);
}
onMount(() => {
codeElementArray[0].focus();
});

View file

@ -1,11 +1,23 @@
import { apiLogin, apiLogout } from "../api/candidate";
import { apiFillDetails, apiLogin, apiLogout } from "../api/candidate";
import { writable } from "svelte/store";
export interface CandidateData {
id?: string;
name?: string;
surname?: string;
email?: string;
// id?: string;
name: string;
surname: string;
birthplace: string;
birthdate: string;
address: string;
telephone: string;
citizenship: string;
email: string;
sex: string;
study: string;
personalIdNumber: string;
parentName: string;
parentSurname: string;
parentTelephone: string;
parentEmail: string;
}
export interface CandidateLogin {
@ -17,11 +29,47 @@ export const candidateData = writable<CandidateData>();
export async function login(data: CandidateLogin) {
// TODO: handle errors
let res = await apiLogin(data); // TODO: set candidate data from response to store
try {
let applicationId = await apiLogin(data); // TODO: set candidate data from response to store
console.log("login result: " + applicationId);
} catch (e) {
console.error("login failed");
throw e;
}
}
export async function logout() {
// TOOD: handle errors
await apiLogout();
candidateData.set({});
try {
await apiLogout();
candidateData.set({
name: "",
surname: "",
birthplace: "",
birthdate: "",
address: "",
telephone: "",
citizenship: "",
email: "",
sex: "",
study: "",
personalIdNumber: "",
parentName: "",
parentSurname: "",
parentTelephone: "",
parentEmail: ""
});
} catch (e) {
console.error(e);
}
}
export async function fillDetails(data: CandidateData) {
try {
let res = await apiFillDetails(data);
candidateData.set(res);
} catch (e) {
console.error(e);
throw e;
}
}