ログイン後にユーザー情報を取得

This commit is contained in:
Mouriya
2024-08-19 21:22:12 +09:00
parent 22a8bf99ca
commit 8b9f83ab25

View File

@@ -3,13 +3,20 @@ import { api } from 'boot/axios';
import { router } from 'src/router';
import { IDomainInfo } from '../types/ActionTypes';
import { jwtDecode } from 'jwt-decode';
interface UserInfo {
firstName: string;
lastName: string;
email: string;
}
export interface IUserState {
token?: string;
returnUrl: string;
currentDomain: IDomainInfo;
LeftDrawer: boolean;
userId?: string;
permissions: 'admin'|'user';
userInfo: UserInfo;
permissions: 'admin' | 'user';
}
export const useAuthStore = defineStore('auth', {
@@ -19,7 +26,8 @@ export const useAuthStore = defineStore('auth', {
LeftDrawer: false,
currentDomain: {} as IDomainInfo,
userId: '',
permissions: 'user'
userInfo: {} as UserInfo,
permissions: 'user',
}),
getters: {
toggleLeftDrawer(): boolean {
@@ -38,11 +46,12 @@ export const useAuthStore = defineStore('auth', {
const result = await api.post(`api/token`, params);
console.info(result);
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.permissions = (tokenJson as any).permissions ?? 'user';
api.defaults.headers['Authorization'] = 'Bearer ' + this.token;
this.currentDomain = await this.getCurrentDomain();
this.userInfo = await this.getUserInfo(this.userId!);
router.push(this.returnUrl || '/');
return true;
} catch (e) {
@@ -67,6 +76,14 @@ export const useAuthStore = defineStore('auth', {
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() {
this.token = '';
this.currentDomain = {} as IDomainInfo; // 清空当前域