mirror of
https://github.com/danbulant/Portfolio
synced 2026-05-24 12:35:31 +00:00
feat: api implementation PoC
This commit is contained in:
parent
35543234ee
commit
bb6eaf65a0
4 changed files with 61 additions and 1 deletions
26
frontend/src/api/candidate.ts
Normal file
26
frontend/src/api/candidate.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
5
frontend/src/api/index.ts
Normal file
5
frontend/src/api/index.ts
Normal 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] };
|
||||
}
|
||||
|
|
@ -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(() => {
|
||||
|
|
|
|||
27
frontend/src/stores/candidate.ts
Normal file
27
frontend/src/stores/candidate.ts
Normal 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({});
|
||||
}
|
||||
Loading…
Reference in a new issue