mirror of
https://github.com/danbulant/Portfolio
synced 2026-06-08 17:20:52 +00:00
feat: API login
This commit is contained in:
parent
bb6eaf65a0
commit
cf234d2f73
3 changed files with 90 additions and 26 deletions
|
|
@ -1,26 +1,32 @@
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import type { CandidateLogin } from "src/stores/candidate";
|
import type { CandidateData, CandidateLogin } from "src/stores/candidate";
|
||||||
import { API_URL, errorHandler } from ".";
|
import { API_URL, errorHandler } from ".";
|
||||||
|
|
||||||
|
|
||||||
export async function apiLogin(data: CandidateLogin): Promise<boolean> {
|
export async function apiLogin(data: CandidateLogin): Promise<number> {
|
||||||
axios.post(API_URL + '/candidate/login', data, {withCredentials: true}).then((res) => {
|
try {
|
||||||
console.log(res);
|
let res = await axios.post(API_URL + '/candidate/login', data, {withCredentials: true});
|
||||||
return res.status === 200;
|
return data.application_id;
|
||||||
}).catch((err) => {
|
} catch (e: any) {
|
||||||
throw errorHandler(err, "Login failed");
|
throw errorHandler(e, "Login failed");
|
||||||
});
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
export async function apiLogout(): Promise<boolean> {
|
export async function apiLogout() {
|
||||||
try {
|
try {
|
||||||
const res = await axios.get(API_URL + '/candidate/logout', {
|
await axios.post(API_URL + '/candidate/logout', {withCredentials: true});
|
||||||
withCredentials: true
|
} catch (e: any) {
|
||||||
});
|
throw errorHandler(e, "Logout failed");
|
||||||
return res.status === 200;
|
}
|
||||||
} catch (error) {
|
}
|
||||||
return false;
|
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
import { login } from '../../../stores/candidate';
|
import { login } from '../../../stores/candidate';
|
||||||
|
import { goto } from '$app/navigation';
|
||||||
|
|
||||||
|
|
||||||
let applicationId = Number($page.params.code);
|
let applicationId = Number($page.params.code);
|
||||||
|
|
@ -38,10 +39,19 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
$: if (codeValueArray.length === 8) {
|
$: if (codeValueArray.length === 8) {
|
||||||
login({application_id: applicationId, password: codeValueMobile});
|
submit();
|
||||||
// alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
async function submit() {
|
||||||
|
try {
|
||||||
|
await login({application_id: applicationId, password: codeValueMobile});
|
||||||
|
goto("/register");
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
// alert('ApplicationId: ' + applicationId + '; Password: ' + codeValueMobile);
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
codeElementArray[0].focus();
|
codeElementArray[0].focus();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,23 @@
|
||||||
import { apiLogin, apiLogout } from "../api/candidate";
|
import { apiFillDetails, apiLogin, apiLogout } from "../api/candidate";
|
||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
|
|
||||||
export interface CandidateData {
|
export interface CandidateData {
|
||||||
id?: string;
|
// id?: string;
|
||||||
name?: string;
|
name: string;
|
||||||
surname?: string;
|
surname: string;
|
||||||
email?: 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 {
|
export interface CandidateLogin {
|
||||||
|
|
@ -17,11 +29,47 @@ export const candidateData = writable<CandidateData>();
|
||||||
|
|
||||||
export async function login(data: CandidateLogin) {
|
export async function login(data: CandidateLogin) {
|
||||||
// TODO: handle errors
|
// 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() {
|
export async function logout() {
|
||||||
// TOOD: handle errors
|
// TOOD: handle errors
|
||||||
await apiLogout();
|
try {
|
||||||
candidateData.set({});
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue