Skip to content

Commit

Permalink
feat: add functional authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriansaliou committed Nov 10, 2024
1 parent f4398cc commit b9c1c7b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
27 changes: 26 additions & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@
* ************************************************************************* */

// NPM
import { default as axios, AxiosInstance } from "axios";
import { default as axios, AxiosInstance, AxiosHeaderValue } from "axios";

// PROJECT: COMMONS
import CONFIG from "@/commons/config";

/**************************************************************************
* CONSTANTS
***************************************************************************/

const HTTP_TIMEOUT = 10000; // 10 seconds

/**************************************************************************
* STORE
* ************************************************************************* */
Expand All @@ -26,15 +32,34 @@ class API {
this.client = this.__createClient();
}

authenticate(token: string | null): void {
// Assign token to common authorization
this.__setCommonHeader("Authorization", `Bearer ${token}`);
}

private __createClient(): AxiosInstance {
return axios.create({
baseURL: `${CONFIG.api.endpoint.local}/${CONFIG.api.version}`,
timeout: HTTP_TIMEOUT,

headers: {
"Content-Type": "application/json"
}
});
}

private __setCommonHeader(
name: string,
value: AxiosHeaderValue | null
): void {
const commonHeaders = this.client.defaults.headers.common;

if (value === null) {
delete commonHeaders[name];
} else {
commonHeaders[name] = value;
}
}
}

/**************************************************************************
Expand Down
39 changes: 31 additions & 8 deletions src/store/tables/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
// NPM
import { defineStore } from "pinia";

// PROJECT: STORES
// PROJECT: API
import Api from "@/api";
import APILogin from "@/api/providers/login";

/**************************************************************************
* INTERFACES
* ************************************************************************* */

interface Account {
token: string | null;
session: {
token: string | null;
};
}

/**************************************************************************
Expand All @@ -29,21 +32,41 @@ interface Account {
const $account = defineStore("account", {
state: (): Account => {
return {
token: null
session: {
token: null
}
};
},

getters: {
getSessionToken: function () {
return (): string | null => {
return this.session.token;
};
}
},

actions: {
async login(username: string, password: string): Promise<void> {
const { token } = await APILogin.login(username, password);

// TODO
console.error("==> got token = " + token);
// Save session token
this.setSessionToken(token);
},

async logout(purge = false) {
// TODO: clear cookies et al
throw new Error("Not implemented");
async logout() {
// Clear session token
this.setSessionToken(null);
},

setSessionToken(token: string | null): void {
// Update saved token
this.$patch(() => {
this.session.token = token;
});

// Re-authenticate to API client (or de-authenticate)
Api.authenticate(token);
}
}
});
Expand Down

0 comments on commit b9c1c7b

Please sign in to comment.