add auth flux

This commit is contained in:
Robin COuret
2026-03-06 17:42:04 +01:00
parent a06e9c3633
commit 34845b9696
7 changed files with 60 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
import { BField, BInput, BButton, useToast } from "buefy"; import { BField, BInput, BButton, useToast } from "buefy";
//import { apiClient } from "@/services/api"; //import { apiClient } from "@/services/api";
import api from "@/services/apiAxios" import api from "@/services/apiAxios"
import { AxiosResponse } from "axios"; import type { AxiosResponse } from "axios";
import { ref } from "vue"; import { ref } from "vue";
import type { Knowledge, KnowledgeCreate } from "@/types/types"; import type { Knowledge, KnowledgeCreate } from "@/types/types";

View File

@@ -8,6 +8,7 @@
import { useItemStore } from '@/stores/item' import { useItemStore } from '@/stores/item'
import api from "@/services/apiAxios" import api from "@/services/apiAxios"
import type { AxiosResponse } from "axios";
//const stepStore = useStepStore() //const stepStore = useStepStore()
const itemStore = useItemStore() const itemStore = useItemStore()
@@ -32,7 +33,7 @@
}) })
async function getQuestions(knowledge: Knowledge): Promise<Question[]>{ async function getQuestions(knowledge: Knowledge): Promise<Question[]>{
const response = await api.get(`api/v1/knowledges/${knowledge.id}/questions`) const response: AxiosResponse = await api.get(`api/v1/knowledges/${knowledge.id}/questions`)
const questions: Question[] = response.data const questions: Question[] = response.data
return questions return questions
} }

View File

@@ -1,4 +1,7 @@
import { createRouter, createWebHistory } from 'vue-router' import { createRouter, createWebHistory } from 'vue-router'
import { isAuthenticated } from '@/services/apiAxios'
const pagesWithoutGuard = ['login', 'app']
const router = createRouter({ const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL), history: createWebHistory(import.meta.env.BASE_URL),
@@ -7,6 +10,12 @@ const router = createRouter({
path: '/', path: '/',
alias: '/app', alias: '/app',
name: 'app', name: 'app',
component: () => import('@/views/WelcomeView.vue'),
},
{
path: '/experiment',
alias: '/experiment',
name: 'experiment',
component: () => import('@/views/ExperimentView.vue'), component: () => import('@/views/ExperimentView.vue'),
}, },
{ {
@@ -18,4 +27,11 @@ const router = createRouter({
], ],
}) })
router.beforeEach(async (to, from) => {
const isAuth = await isAuthenticated()
if (!isAuth && !pagesWithoutGuard.includes(to.name!.toString())) {
return { name: 'login' }
}
})
export default router export default router

View File

@@ -1,4 +1,5 @@
import axios from "axios" import axios from "axios"
import type { AxiosResponse } from "axios";
const api = axios.create({ const api = axios.create({
baseURL: import.meta.env.VITE_API_URL baseURL: import.meta.env.VITE_API_URL
@@ -20,7 +21,21 @@ export const authAPI = {
new URLSearchParams({ username, password }), new URLSearchParams({ username, password }),
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' }} { headers: { 'Content-Type': 'application/x-www-form-urlencoded' }}
), ),
getMe: () => api.get('/auth/me') getMe: () => api.get('/api/v1/auth/me')
}
export const isAuthenticated = async () => {
try {
const response: AxiosResponse = await authAPI.getMe()
if (response.status==200)
return true
else
return false
}
catch{
return false
}
} }
export default api; export default api;

View File

@@ -1,4 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import router from '@/router/index'
import { BField, BInput, BButton, useToast } from "buefy"; import { BField, BInput, BButton, useToast } from "buefy";
import { ref } from "vue" import { ref } from "vue"
import { authAPI } from '@/services/apiAxios' import { authAPI } from '@/services/apiAxios'
@@ -16,6 +17,7 @@
localStorage.setItem('access_token', access_token) localStorage.setItem('access_token', access_token)
localStorage.setItem('refresh_token', refresh_token) localStorage.setItem('refresh_token', refresh_token)
router.push({ path: '/experiment' })
} catch (err){ } catch (err){
console.log(err) console.log(err)
Toast.open({message: "Login failed", type: "is-danger"}) Toast.open({message: "Login failed", type: "is-danger"})

View File

@@ -13,9 +13,6 @@
<section id="section-central"> <section id="section-central">
<component :is="stepStore.getCurrentComponent"></component> <component :is="stepStore.getCurrentComponent"></component>
</section> </section>
<!-- <section>
<EvaluateQuestion/>
</section> -->
</div> </div>
</template> </template>

View File

@@ -0,0 +1,23 @@
<script setup lang="ts">
import { BButton } from "buefy"
</script>
<template>
<section>
<b-button type="is-primary" size="is-medium" rounded>
<RouterLink to="/experiment"><span>Lancer l'expérience</span></RouterLink>
</b-button>
</section>
</template>
<style scoped>
section{
display: flex;
height: 80vh;
justify-content: center;
align-items: center;
}
span{
color: black;
}
</style>