From 8b9f83ab25326e0ac27513f62b8fab16cd3ce8f0 Mon Sep 17 00:00:00 2001 From: Mouriya Date: Mon, 19 Aug 2024 21:22:12 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3=E5=BE=8C?= =?UTF-8?q?=E3=81=AB=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E6=83=85=E5=A0=B1?= =?UTF-8?q?=E3=82=92=E5=8F=96=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/stores/useAuthStore.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/frontend/src/stores/useAuthStore.ts b/frontend/src/stores/useAuthStore.ts index 73fe7c3..7deb2cb 100644 --- a/frontend/src/stores/useAuthStore.ts +++ b/frontend/src/stores/useAuthStore.ts @@ -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{ + 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; // 清空当前域