Initial commit: XDL Twitter/X video downloader
This commit is contained in:
25
frontend/src/stores/auth.js
Normal file
25
frontend/src/stores/auth.js
Normal file
@@ -0,0 +1,25 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import axios from 'axios'
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = ref(localStorage.getItem('token') || '')
|
||||
const isLoggedIn = computed(() => !!token.value)
|
||||
|
||||
async function login(username, password) {
|
||||
const res = await axios.post('/api/auth/login', { username, password })
|
||||
token.value = res.data.access_token
|
||||
localStorage.setItem('token', token.value)
|
||||
}
|
||||
|
||||
function logout() {
|
||||
token.value = ''
|
||||
localStorage.removeItem('token')
|
||||
}
|
||||
|
||||
function getHeaders() {
|
||||
return token.value ? { Authorization: `Bearer ${token.value}` } : {}
|
||||
}
|
||||
|
||||
return { token, isLoggedIn, login, logout, getHeaders }
|
||||
})
|
||||
Reference in New Issue
Block a user