added: login, logout and auto refresh token
This commit is contained in:
parent
72c796c429
commit
f66c014a36
9 changed files with 189 additions and 63 deletions
|
|
@ -2,10 +2,15 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<router-link :to="{ name: 'home' }"> Home </router-link>
|
<div>
|
||||||
<router-link :to="{ name: 'login' }"> Login </router-link>
|
<router-link :to="{ name: 'startPage' }"> Home </router-link>
|
||||||
|
<router-link :to="{ name: 'login' }"> Login </router-link>
|
||||||
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
|
Router view starts below
|
||||||
<router-view />
|
<router-view />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -3,12 +3,10 @@ import { createPinia } from 'pinia'
|
||||||
|
|
||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from './router'
|
import router from './router'
|
||||||
import { setupAuthInterceptor } from '@/services/setupInterceptors.js'
|
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
app.use(createPinia())
|
app.use(createPinia())
|
||||||
app.use(router)
|
app.use(router)
|
||||||
setupAuthInterceptor()
|
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import HomeView from '../views/HomeView.vue'
|
import StartPageView from '../views/StartPageView.vue'
|
||||||
import LoginView from '@/views/LoginView.vue'
|
import LoginView from '@/views/LoginView.vue'
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
|
|
@ -7,8 +7,8 @@ const router = createRouter({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: '/',
|
path: '/',
|
||||||
name: 'home',
|
name: 'startPage',
|
||||||
component: HomeView,
|
component: StartPageView,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/login',
|
path: '/login',
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,113 @@
|
||||||
import axios from 'axios'
|
import { useAuthStore } from '@/stores/authStore.js';
|
||||||
|
|
||||||
const apiClient = axios.create({
|
const BASE_URL = 'http://localhost:9000/api/v2';
|
||||||
baseURL: 'http://localhost:9000/api/v2',
|
|
||||||
timeout: 5000,
|
let isRefreshing = false;
|
||||||
headers: {
|
let refreshPromise = null;
|
||||||
|
|
||||||
|
function createConfig(options = {}) {
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
}
|
...options.headers,
|
||||||
})
|
};
|
||||||
|
|
||||||
export default apiClient
|
if (authStore.accessToken) {
|
||||||
|
headers['Authorization'] = `Bearer ${authStore.accessToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
headers,
|
||||||
|
credentials: 'include',
|
||||||
|
...options,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshAccessToken() {
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
|
||||||
|
if (isRefreshing) return refreshPromise;
|
||||||
|
|
||||||
|
isRefreshing = true;
|
||||||
|
refreshPromise = fetch(`${BASE_URL}/user/auth/refresh`, {
|
||||||
|
method: 'POST',
|
||||||
|
credentials: 'include',
|
||||||
|
})
|
||||||
|
.then(async (res) => {
|
||||||
|
if (!res.ok) throw new Error('Failed to refresh access token');
|
||||||
|
return res.json();
|
||||||
|
})
|
||||||
|
.then((data) => {
|
||||||
|
authStore.setToken(data.accessToken);
|
||||||
|
return data;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
throw error;
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
isRefreshing = false;
|
||||||
|
refreshPromise = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
return refreshPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function request(url, options = {}, isRetry = false) {
|
||||||
|
const config = createConfig(options);
|
||||||
|
|
||||||
|
const response = await fetch(`${BASE_URL}${url}`, config);
|
||||||
|
|
||||||
|
if (response.status === 401 && !isRetry) {
|
||||||
|
try {
|
||||||
|
const data = await refreshAccessToken();
|
||||||
|
|
||||||
|
const newOptions = {
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
...options.headers,
|
||||||
|
'Authorization': `Bearer ${data.accessToken}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
return await request(url, newOptions, true);
|
||||||
|
} catch (e) {
|
||||||
|
const authStore = useAuthStore();
|
||||||
|
authStore.forceLogout();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
let errorData;
|
||||||
|
try {
|
||||||
|
errorData = await response.json();
|
||||||
|
} catch {
|
||||||
|
errorData = {};
|
||||||
|
}
|
||||||
|
throw new Error(errorData.message || `HTTP Error: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const contentType = response.headers.get('content-type');
|
||||||
|
if (contentType?.includes('application/json')) {
|
||||||
|
try {
|
||||||
|
return await response.json();
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('Failed to parse JSON response', e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const apiClient = {
|
||||||
|
get: (url) => request(url, { method: 'GET' }),
|
||||||
|
post: (url, data) => request(url, {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
}),
|
||||||
|
put: (url, data) => request(url, {
|
||||||
|
method: 'PUT',
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
}),
|
||||||
|
delete: (url) => request(url, { method: 'DELETE' }),
|
||||||
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
import apiClient from '@/services/apiClient'
|
|
||||||
import { useAuthStore } from '@/stores/authStore'
|
|
||||||
|
|
||||||
export function setupAuthInterceptor() {
|
|
||||||
apiClient.interceptors.request.use((config) => {
|
|
||||||
const authStore = useAuthStore()
|
|
||||||
if (authStore.accessToken) {
|
|
||||||
config.headers.Authorization = `Bearer ${authStore.accessToken}`
|
|
||||||
}
|
|
||||||
return config
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
@ -1,41 +1,59 @@
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
import apiClient from '@/services/apiClient';
|
import { apiClient } from '@/services/apiClient';
|
||||||
|
import router from '@/router/index.js';
|
||||||
|
|
||||||
export const useAuthStore = defineStore('auth', () => {
|
export const useAuthStore = defineStore('auth', () => {
|
||||||
// state
|
// state
|
||||||
const accessToken = ref(null)
|
const accessToken = ref(null);
|
||||||
const user = ref(null)
|
const user = ref(null);
|
||||||
|
|
||||||
// getters
|
// getters
|
||||||
const isAuthenticated = computed(() => !!accessToken.value)
|
const isAuthenticated = computed(() => !!accessToken.value);
|
||||||
|
|
||||||
// actions
|
// actions
|
||||||
|
const setToken = (token) => {
|
||||||
|
accessToken.value = token;
|
||||||
|
};
|
||||||
|
|
||||||
const login = async (email, password) => {
|
const login = async (email, password) => {
|
||||||
try {
|
try {
|
||||||
const response = await apiClient.post(
|
const response = await apiClient.post('/user/auth/login', { email, password });
|
||||||
"/user/login",
|
const { access_token, user: userData } = response;
|
||||||
{ email, password }
|
|
||||||
)
|
|
||||||
const { access_token } = response.data
|
|
||||||
accessToken.value = access_token
|
|
||||||
|
|
||||||
console.log('Email', email)
|
setToken(access_token);
|
||||||
console.log('Password', password)
|
user.value = userData || null;
|
||||||
|
|
||||||
|
router.push('/');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error)
|
console.error('Login error:', error);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const logout = () => {
|
const logout = async () => {
|
||||||
console.log('logout placeholder')
|
accessToken.value = null;
|
||||||
}
|
user.value = null;
|
||||||
|
try {
|
||||||
|
await apiClient.post('/user/auth/logout');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Logout error:', error);
|
||||||
|
}
|
||||||
|
router.push('/startPage');
|
||||||
|
};
|
||||||
|
|
||||||
|
const forceLogout = () => {
|
||||||
|
accessToken.value = null;
|
||||||
|
user.value = null;
|
||||||
|
router.push('/startPage');
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
accessToken,
|
accessToken,
|
||||||
user,
|
user,
|
||||||
isAuthenticated,
|
isAuthenticated,
|
||||||
|
setToken,
|
||||||
login,
|
login,
|
||||||
logout
|
logout,
|
||||||
}
|
forceLogout,
|
||||||
})
|
};
|
||||||
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
<script setup>
|
|
||||||
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
Home view
|
|
||||||
</template>
|
|
||||||
|
|
@ -1,16 +1,16 @@
|
||||||
<script setup lang="js">
|
<script setup>
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue'
|
||||||
import { useAuthStore} from '@/stores/authStore.js';
|
import { useAuthStore} from '@/stores/authStore.js'
|
||||||
|
|
||||||
const store = useAuthStore();
|
const store = useAuthStore()
|
||||||
|
|
||||||
const email = ref('');
|
const email = ref('')
|
||||||
const password = ref('');
|
const password = ref('')
|
||||||
|
|
||||||
|
|
||||||
const onSubmit = () => {
|
const onSubmit = () => {
|
||||||
store.login(email.value, password.value);
|
store.login(email.value, password.value)
|
||||||
};
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|
@ -35,5 +35,7 @@
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
div {
|
||||||
|
background: red;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
20
src/views/StartPageView.vue
Normal file
20
src/views/StartPageView.vue
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
<script setup>
|
||||||
|
import { useAuthStore } from '@/stores/authStore.js'
|
||||||
|
|
||||||
|
const store = useAuthStore()
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
store.logout()
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<div>Home view</div>
|
||||||
|
<button type="button" @click="logout">no route</button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
div {
|
||||||
|
background: green;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue