feat: api implementation PoC

This commit is contained in:
Sebastian Pravda 2022-11-26 19:24:48 +01:00 committed by EETagent
parent 35543234ee
commit bb6eaf65a0
4 changed files with 61 additions and 1 deletions

View file

@ -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<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;
}
// TODO
export async function apiLogout(): Promise<boolean> {
try {
const res = await axios.get(API_URL + '/candidate/logout', {
withCredentials: true
});
return res.status === 200;
} catch (error) {
return false;
}
}

View file

@ -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] };
}

View file

@ -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(() => {

View file

@ -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<CandidateData>();
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({});
}