From abab4587d163307577993b2a3c423dc5a2c1cdd7 Mon Sep 17 00:00:00 2001
From: Kamigen <46357922+Edouard127@users.noreply.github.com>
Date: Sat, 30 Mar 2024 21:35:19 -0400
Subject: [PATCH] Refactor!: Codebase
---
.env.development | 1 +
.env.production | 1 +
src/layouts/DefaultLayout.vue | 10 +++---
src/main.js | 2 +-
src/plugins/api.js | 35 +++++++++++++++++++
src/plugins/clientPlugin.js | 34 ------------------
.../store/authStore.js => stores/auth.js} | 7 ++--
src/views/LoginView.vue | 16 +++++----
src/views/StripePayment.vue | 8 ++---
src/views/main/HomeView.vue | 13 ++++---
tailwind.config.js | 3 --
11 files changed, 64 insertions(+), 66 deletions(-)
create mode 100644 .env.development
create mode 100644 .env.production
create mode 100644 src/plugins/api.js
delete mode 100644 src/plugins/clientPlugin.js
rename src/{plugins/store/authStore.js => stores/auth.js} (97%)
diff --git a/.env.development b/.env.development
new file mode 100644
index 0000000..d823d9f
--- /dev/null
+++ b/.env.development
@@ -0,0 +1 @@
+VITE_API_URL=http://localhost:5001/api/v1
diff --git a/.env.production b/.env.production
new file mode 100644
index 0000000..5a800a5
--- /dev/null
+++ b/.env.production
@@ -0,0 +1 @@
+VITE_API_URL=todo
diff --git a/src/layouts/DefaultLayout.vue b/src/layouts/DefaultLayout.vue
index 59379ed..7690bad 100644
--- a/src/layouts/DefaultLayout.vue
+++ b/src/layouts/DefaultLayout.vue
@@ -118,18 +118,18 @@
-
\ No newline at end of file
+
diff --git a/src/main.js b/src/main.js
index e55acf4..39f62fb 100644
--- a/src/main.js
+++ b/src/main.js
@@ -8,7 +8,7 @@ import 'vuetify/styles'
import { createVuetify } from 'vuetify'
import * as components from 'vuetify/components'
import * as directives from 'vuetify/directives'
-import clientPlugin from './plugins/clientPlugin'
+import clientPlugin from './plugins/api.js'
const vuetify = createVuetify({
components,
diff --git a/src/plugins/api.js b/src/plugins/api.js
new file mode 100644
index 0000000..ee75e5b
--- /dev/null
+++ b/src/plugins/api.js
@@ -0,0 +1,35 @@
+import axios from "axios";
+import {inject} from "vue";
+
+const key = Symbol("api");
+
+export default function(app) {
+ if (!import.meta.env.VITE_API_URL) throw new Error("VITE_API_URL is not provided")
+
+ // You create a .env.development file and a .env file
+ // depending on the environment, the correct file will be used
+ const api = axios.create({
+ baseURL: import.meta.env.VITE_API_URL,
+ timeout: 2000,
+ });
+
+ const requestInterceptor = (config) => {
+ const token = localStorage.getItem("jwt");
+ if (token) config.headers["Authorization"] = `Bearer ${token}`;
+ return config;
+ }
+
+ api.interceptors.request.use(requestInterceptor);
+
+ // This is a local injection, to use it in your components you can do this:
+ // const api = inject("api")
+ // api.get("/some-endpoint")
+ app.provide(key, api)
+}
+
+export function useClient() {
+ const api = inject(key)
+ if (!api) throw new Error("api is not provided")
+ return api;
+}
+
diff --git a/src/plugins/clientPlugin.js b/src/plugins/clientPlugin.js
deleted file mode 100644
index c5faed0..0000000
--- a/src/plugins/clientPlugin.js
+++ /dev/null
@@ -1,34 +0,0 @@
-import axios from "axios";
-import { inject } from 'vue';
-
-const clientKey = Symbol('client');
-
-export default {
- //todo: Need to have the baseURL in the config for later ( dev and prod env. )
- install: (app) => {
- const axiosInstance = axios.create({
- baseURL: 'https://localhost:5001/',
- timeout: 2000,
- });
-
- axiosInstance.interceptors.request.use((config) => {
- const token = localStorage.getItem('jwt');
- if (token) {
- config.headers.Authorization = `Bearer ${token}`;
- }
- return config;
- }, (error) => {
- return Promise.reject(error);
- });
-
- app.provide(clientKey, axiosInstance);
- }
-};
-
-export function useClient() {
- const client = inject(clientKey);
- if (!client) {
- throw new Error('Axios instance is not provided');
- }
- return client;
-}
\ No newline at end of file
diff --git a/src/plugins/store/authStore.js b/src/stores/auth.js
similarity index 97%
rename from src/plugins/store/authStore.js
rename to src/stores/auth.js
index cf0fef0..bc2c12f 100644
--- a/src/plugins/store/authStore.js
+++ b/src/stores/auth.js
@@ -2,7 +2,7 @@ import { defineStore } from 'pinia';
const baseUrl = '/api/Users';
-export const useAuthStore = defineStore({
+export const auth = defineStore({
id: 'auth',
state: () => ({
user: null,
@@ -26,9 +26,6 @@ export const useAuthStore = defineStore({
} catch (error) {
throw new Error('Login failed.')
}
-
-
-
},
logout() {
localStorage.setItem('jwt', '');
@@ -52,4 +49,4 @@ export const useAuthStore = defineStore({
clearTimeout(this.refreshTokenTimeout);
}
}
-});
\ No newline at end of file
+});
diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue
index 701d1bf..8218cd6 100644
--- a/src/views/LoginView.vue
+++ b/src/views/LoginView.vue
@@ -96,13 +96,15 @@
\ No newline at end of file
+
diff --git a/src/views/main/HomeView.vue b/src/views/main/HomeView.vue
index d7e7f91..57b3ca5 100644
--- a/src/views/main/HomeView.vue
+++ b/src/views/main/HomeView.vue
@@ -15,7 +15,7 @@
Connexion
@@ -193,14 +193,14 @@
+ style="margin-bottom: 4%; border-radius: 30px; width: 80vw;">
+ style="border-radius: 30px; min-width: 250px; max-height: 350px;">
@@ -408,10 +408,9 @@