33 lines
796 B
JavaScript
33 lines
796 B
JavaScript
import { ref, computed } from 'vue'
|
|
import { defineStore } from 'pinia'
|
|
|
|
export const useAuthStore = defineStore('auth', () => {
|
|
const token = ref(localStorage.getItem('user_token') || '');
|
|
const userInfo = ref(null);
|
|
|
|
//判断是否已登录
|
|
const isLoggedIn = computed(() => !!token.value);
|
|
|
|
/**
|
|
* 登录成功保存状态
|
|
* @param {String} newToken 用户凭证
|
|
* @param {*} user 用户信息
|
|
*/
|
|
function setLoginInfo(newToken, user) {
|
|
token.value = newToken;
|
|
userInfo.value = user;
|
|
localStorage.setItem('user_token', newToken);
|
|
}
|
|
|
|
/**
|
|
* 退出登录
|
|
*/
|
|
function logout() {
|
|
token.value = '';
|
|
userInfo.value = null;
|
|
localStorage.removeItem('user_token');
|
|
}
|
|
|
|
return { token, userInfo, isLoggedIn, setLoginInfo, logout };
|
|
})
|