modify(login): 修改登录返回对象,新增返回用户信息
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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 };
|
||||
})
|
||||
Reference in New Issue
Block a user