{{ book.title }}
-
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae dolores, possimus
pariatur animi temporibus nesciunt praesentium
diff --git a/src/components/NotFound.vue b/src/components/NotFound.vue
new file mode 100644
index 0000000..a1c3d75
--- /dev/null
+++ b/src/components/NotFound.vue
@@ -0,0 +1,12 @@
+
+
+
+ La ressource que vous cherchez n'existe pas !
+
+
+
\ No newline at end of file
diff --git a/src/composables/useAuth.ts b/src/composables/useAuth.ts
new file mode 100644
index 0000000..1850136
--- /dev/null
+++ b/src/composables/useAuth.ts
@@ -0,0 +1,24 @@
+import { ref } from 'vue';
+
+const isAuthenticated = ref(!!sessionStorage.getItem('access'));
+
+export function useAuth() {
+
+ function login(accessToken: string, refreshToken: string) {
+ sessionStorage.setItem('access', accessToken);
+ localStorage.setItem('refresh', refreshToken); // Il dure 1j, UN JOUR !!!
+ isAuthenticated.value = true;
+ }
+
+ function logout() {
+ sessionStorage.removeItem('access');
+ localStorage.removeItem('refresh');
+ isAuthenticated.value = false;
+ }
+
+ return {
+ isAuthenticated,
+ login,
+ logout
+ };
+}
\ No newline at end of file
diff --git a/src/models/user.ts b/src/models/user.ts
index 9715bb9..3439961 100644
--- a/src/models/user.ts
+++ b/src/models/user.ts
@@ -1,5 +1,11 @@
+import type { Book } from "./book";
+
export class User {
id: number = 0;
username: string = ""
+ first_name: string = ""
+ last_name: string = ""
email: string = ""
+ profile_picture: string = ""
+ books: Book[] = []
}
\ No newline at end of file
diff --git a/src/routes.ts b/src/routes.ts
index 7c1632e..2d462dc 100644
--- a/src/routes.ts
+++ b/src/routes.ts
@@ -1,32 +1,42 @@
-import { createWebHistory, createRouter } from 'vue-router'
-import Authenticate from './components/Authenticate.vue'
-import Home from './components/Home.vue'
+import { createWebHistory, createRouter } from "vue-router";
+import Authenticate from "./components/Authenticate.vue";
+import Home from "./components/Home.vue";
+import NotFound from "./components/NotFound.vue";
const routes = [
{
- path: '/',
+ path: "/",
component: Home,
name: "Home",
},
{
- path: '/authenticate',
+ path: "/login",
component: Authenticate,
- name: "Authenticate",
- meta: { guestOnly: true }
+ name: "Authenticate",
+ meta: { guestOnly: true },
},
-]
+ {
+ path: "/:pathMatch(.*)*",
+ component: NotFound,
+ name: "Not found",
+ },
+];
export const router = createRouter({
history: createWebHistory(),
routes,
-})
+});
router.beforeEach((to, from) => {
- const authenticated = sessionStorage.getItem('access');
+ const authenticated = sessionStorage.getItem("access");
- if (to.meta.guestOnly && authenticated) {
- return { name: 'Home' };
+ if (to.path === "/login" && authenticated) {
+ return { name: "Home " };
+ }
+
+ if (to.path === "/logout" && !authenticated) {
+ return { name: "Home" };
}
return true;
-})
\ No newline at end of file
+});
diff --git a/src/services/authentication.service.ts b/src/services/authentication.service.ts
index c53a5c1..173087f 100644
--- a/src/services/authentication.service.ts
+++ b/src/services/authentication.service.ts
@@ -13,6 +13,18 @@ export class AuthenticationService {
})
}
+ async findMe() {
+ const headers = { 'Authorization': `Bearer ${sessionStorage.getItem('access')}` };
+ return await axios
+ .get('profile', { headers })
+ .then((res) => {
+ return res.data as User;
+ })
+ .catch((error) => {
+ throw error;
+ })
+ }
+
async authenticate(username: string, password: string) {
return await axios
.post('token/', {
@@ -20,8 +32,7 @@ export class AuthenticationService {
password: password,
})
.then((res) => {
- localStorage.setItem('refresh', res.data.refresh)
- sessionStorage.setItem('access', res.data.access)
+ return res.data;
})
.catch((error) => {
throw error;