ログイン後にユーザー情報を取得
This commit is contained in:
@@ -3,12 +3,19 @@ import { api } from 'boot/axios';
|
|||||||
import { router } from 'src/router';
|
import { router } from 'src/router';
|
||||||
import { IDomainInfo } from '../types/ActionTypes';
|
import { IDomainInfo } from '../types/ActionTypes';
|
||||||
import { jwtDecode } from 'jwt-decode';
|
import { jwtDecode } from 'jwt-decode';
|
||||||
|
interface UserInfo {
|
||||||
|
firstName: string;
|
||||||
|
lastName: string;
|
||||||
|
email: string;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IUserState {
|
export interface IUserState {
|
||||||
token?: string;
|
token?: string;
|
||||||
returnUrl: string;
|
returnUrl: string;
|
||||||
currentDomain: IDomainInfo;
|
currentDomain: IDomainInfo;
|
||||||
LeftDrawer: boolean;
|
LeftDrawer: boolean;
|
||||||
userId?: string;
|
userId?: string;
|
||||||
|
userInfo: UserInfo;
|
||||||
permissions: 'admin' | 'user';
|
permissions: 'admin' | 'user';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,7 +26,8 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
LeftDrawer: false,
|
LeftDrawer: false,
|
||||||
currentDomain: {} as IDomainInfo,
|
currentDomain: {} as IDomainInfo,
|
||||||
userId: '',
|
userId: '',
|
||||||
permissions: 'user'
|
userInfo: {} as UserInfo,
|
||||||
|
permissions: 'user',
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
toggleLeftDrawer(): boolean {
|
toggleLeftDrawer(): boolean {
|
||||||
@@ -38,11 +46,12 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
const result = await api.post(`api/token`, params);
|
const result = await api.post(`api/token`, params);
|
||||||
console.info(result);
|
console.info(result);
|
||||||
this.token = result.data.access_token;
|
this.token = result.data.access_token;
|
||||||
const tokenJson = jwtDecode(result.data.access_token)
|
const tokenJson = jwtDecode(result.data.access_token);
|
||||||
this.userId = tokenJson.sub;
|
this.userId = tokenJson.sub;
|
||||||
this.permissions = (tokenJson as any).permissions ?? 'user';
|
this.permissions = (tokenJson as any).permissions ?? 'user';
|
||||||
api.defaults.headers['Authorization'] = 'Bearer ' + this.token;
|
api.defaults.headers['Authorization'] = 'Bearer ' + this.token;
|
||||||
this.currentDomain = await this.getCurrentDomain();
|
this.currentDomain = await this.getCurrentDomain();
|
||||||
|
this.userInfo = await this.getUserInfo(this.userId!);
|
||||||
router.push(this.returnUrl || '/');
|
router.push(this.returnUrl || '/');
|
||||||
return true;
|
return true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -67,6 +76,14 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
kintoneUrl: data.url,
|
kintoneUrl: data.url,
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
async getUserInfo(id:string):Promise<UserInfo>{
|
||||||
|
const resp = (await api.get(`api/v1/users/${id}`)).data;
|
||||||
|
return {
|
||||||
|
firstName: resp.first_name,
|
||||||
|
lastName: resp.last_name,
|
||||||
|
email: resp.email,
|
||||||
|
}
|
||||||
|
},
|
||||||
logout() {
|
logout() {
|
||||||
this.token = '';
|
this.token = '';
|
||||||
this.currentDomain = {} as IDomainInfo; // 清空当前域
|
this.currentDomain = {} as IDomainInfo; // 清空当前域
|
||||||
|
|||||||
Reference in New Issue
Block a user