diff --git a/frontend/src/api/candidate.ts b/frontend/src/api/candidate.ts new file mode 100644 index 0000000..d57f460 --- /dev/null +++ b/frontend/src/api/candidate.ts @@ -0,0 +1,26 @@ +import axios from "axios"; +import type { CandidateLogin } from "src/stores/candidate"; +import { API_URL, errorHandler } from "."; + + +export async function apiLogin(data: CandidateLogin): Promise { + 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; +} + +// TODO +export async function apiLogout(): Promise { + try { + const res = await axios.get(API_URL + '/candidate/logout', { + withCredentials: true + }); + return res.status === 200; + } catch (error) { + return false; + } +} \ No newline at end of file diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts new file mode 100644 index 0000000..fed5732 --- /dev/null +++ b/frontend/src/api/index.ts @@ -0,0 +1,5 @@ +export const API_URL = "http://localhost:8000"; + +export function errorHandler(error: { data?: { [key: string]: string[] } }, fallbackMessage: string) { + return error?.data?.errors ? error.data.errors : { unknown: [fallbackMessage] }; +} \ No newline at end of file diff --git a/frontend/src/routes/login/[code=application]/+page.svelte b/frontend/src/routes/login/[code=application]/+page.svelte index f854a7b..1e61dc7 100644 --- a/frontend/src/routes/login/[code=application]/+page.svelte +++ b/frontend/src/routes/login/[code=application]/+page.svelte @@ -4,6 +4,7 @@ import woman from '$lib/assets/woman.png'; import { onMount } from 'svelte'; import { page } from '$app/stores'; + import { login } from '../../../stores/candidate'; let applicationId = Number($page.params.code); @@ -37,7 +38,8 @@ }; $: if (codeValueArray.length === 8) { - alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile); + login({application_id: applicationId, password: codeValueMobile}); + // alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile); }; onMount(() => { diff --git a/frontend/src/stores/candidate.ts b/frontend/src/stores/candidate.ts new file mode 100644 index 0000000..d12f3ac --- /dev/null +++ b/frontend/src/stores/candidate.ts @@ -0,0 +1,27 @@ +import { apiLogin, apiLogout } from "../api/candidate"; +import { writable } from "svelte/store"; + +export interface CandidateData { + id?: string; + name?: string; + surname?: string; + email?: string; +} + +export interface CandidateLogin { + application_id: number; + password: string; +} + +export const candidateData = writable(); + +export async function login(data: CandidateLogin) { + // TODO: handle errors + let res = await apiLogin(data); // TODO: set candidate data from response to store +} + +export async function logout() { + // TOOD: handle errors + await apiLogout(); + candidateData.set({}); +} \ No newline at end of file