1234567891011121314151617181920212223242526272829 |
- import { defineStore } from 'pinia'
- import { getToken,setToken,removeToken } from '@/utils/auth.js'
- export const useAuthStore = defineStore('auth', {
- state: () => ({
- token: null,
- }),
- actions: {
- setAuthToken(newToken) {
- this.token = newToken
- // 可以选择将 token 存储到 localStorage 或 sessionStorage
- setToken(newToken)
- },
- clearAuthToken() {
- this.token = null
- removeToken()
- },
- loadAuthToken() {
- const savedToken = getToken()
- if (savedToken) {
- this.token = savedToken
- }
- }
- },
- getters: {
- isAuthenticated(state) {
- return !!state.token; // 如果 token 存在,返回 true;否则返回 false
- }
- }
- })
|