From c7d4211b02f5931382fed6e9d1009074256d6003 Mon Sep 17 00:00:00 2001
From: 劉鈞霖 <benson@gmail.com>
Date: 星期一, 13 十二月 2021 14:23:44 +0800
Subject: [PATCH] [ Update ] : 修正 http header 統一由攔截器去加入以及回傳資料改為 response.data

---
 PAMapp/assets/ts/api/share.ts |   29 ++++++++++++++++++-----------
 1 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/PAMapp/assets/ts/api/share.ts b/PAMapp/assets/ts/api/share.ts
index a119703..aca9155 100644
--- a/PAMapp/assets/ts/api/share.ts
+++ b/PAMapp/assets/ts/api/share.ts
@@ -1,9 +1,9 @@
-import { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios';
+import { AxiosRequestConfig, AxiosError, AxiosResponse} from 'axios';
 import ErrorMessageBox from '../errorService';
 import axios from 'axios';
 import _ from 'lodash';
 
-const notRequireInterceptorErrorUrl=[
+const notRequireInterceptorErrorUrl = [
   '/otp/verify',
   '/eService/authenticate',
   '/login/validate/get_img_code',
@@ -16,38 +16,45 @@
 });
 
 service.interceptors.request.use(
-  (config:AxiosRequestConfig)=>{
+  (config: AxiosRequestConfig) => {
     loadingStart();
+    addHttpHeader(config);
     return config;
   }
 );
 
 service.interceptors.response.use(
-  (response:AxiosResponse)=>{
+  (response: AxiosResponse) => {
     loadingFinish();
-    return response;  // maybe can use response.data
+    return response.data;
   },
-  (error:AxiosError)=>{
+  (error: AxiosError) => {
     loadingFinish();
     showErrorMessageBox(error)
     return Promise.reject(error);
   }
 );
 
+function addHttpHeader(config: AxiosRequestConfig): void {
+  config.headers = {
+    Authorization: 'Bearer ' + localStorage.getItem('id_token')
+  }
+}
+
 function loadingStart(): void {
-    window.$nuxt.$loading.start();
+  window.$nuxt.$loading.start();
 };
 
 function loadingFinish(): void {
-    window.$nuxt.$loading.finish();
+  window.$nuxt.$loading.finish();
 };
 
-function showErrorMessageBox(error:any):void{
+function showErrorMessageBox(error: any): void {
   // console.log('error', error, error.response);
-  if(!_.includes(notRequireInterceptorErrorUrl,error.config.url)){
+  if (!_.includes(notRequireInterceptorErrorUrl, error.config.url)) {
     switch (error.response.status) {
       case 401:
-        Promise.all([ErrorMessageBox('���暹��'),window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(()=>{
+        Promise.all([ErrorMessageBox('���暹��'), window.$nuxt.$store.dispatch('localStorage/actionStorageClear')]).then(() => {
           _.isEqual(window.$nuxt.$route.name, 'index') ? location.reload() : window.$nuxt.$router.push('/');
         });
         break;

--
Gitblit v1.8.0