Add server api instance
authorDaniel Kos <daniel.kos@contractors.roche.com>
Tue, 5 Jun 2018 06:34:59 +0000 (08:34 +0200)
committerDaniel Kos <daniel.kos@contractors.roche.com>
Tue, 5 Jun 2018 06:34:59 +0000 (08:34 +0200)
Feature #13563

Arvados-DCO-1.1-Signed-off-by: Daniel Kos <daniel.kos@contractors.roche.com>:

src/common/server-api.ts [new file with mode: 0644]
src/services/auth-service/auth-service.ts
src/store/auth-reducer.ts

diff --git a/src/common/server-api.ts b/src/common/server-api.ts
new file mode 100644 (file)
index 0000000..9078b73
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import Axios, { AxiosInstance } from "axios";
+
+export const API_HOST = 'https://qr1hi.arvadosapi.com/arvados/v1';
+
+export const serverApi: AxiosInstance = Axios.create({
+    baseURL: API_HOST
+});
+
+export function setServerApiAuthorizationHeader(token: string) {
+    serverApi.defaults.headers.common = {
+        'Authorization': `OAuth2 ${token}`
+    };
+}
+
+export function removeServerApiAuthorizationHeader() {
+    delete serverApi.defaults.headers.common.Authorization;
+}
index 11594c980d667381726fcbd35867d94704625809..672a619410ba9c57274ab55605243e5af68c5ec0 100644 (file)
@@ -2,8 +2,10 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
+import Axios from "axios";
+import { API_HOST, serverApi } from "../../common/server-api";
+
 const API_TOKEN_KEY = 'api_token';
-const API_HOST = 'https://qr1hi.arvadosapi.com';
 
 export default class AuthService {
 
@@ -28,7 +30,8 @@ export default class AuthService {
         window.location.href = `${API_HOST}/login?return_to=${currentUrl}`;
     }
 
-    public logout() {
-        this.removeApiToken();
+    public logout(): Promise<any> {
+        const currentUrl = `${window.location.protocol}//${window.location.host}`;
+        return Axios.get(`${API_HOST}/logout?return_to=${currentUrl}`);
     }
 }
index ec12d6539222614167f5ea7d5457d6e6138294a1..8e9eb4f7ba3a05169e60fe76fb2db222e7f02f6a 100644 (file)
@@ -6,6 +6,7 @@ import { getType } from "typesafe-actions";
 import actions, { AuthAction } from "./auth-action";
 import { User } from "../models/user";
 import { authService } from "../services/services";
+import { removeServerApiAuthorizationHeader, serverApi, setServerApiAuthorizationHeader } from "../common/server-api";
 
 type AuthState = User | {};
 
@@ -13,6 +14,8 @@ const authReducer = (state: AuthState = {}, action: AuthAction) => {
     switch (action.type) {
         case getType(actions.saveApiToken): {
             authService.saveApiToken(action.payload);
+            setServerApiAuthorizationHeader(action.payload);
+            serverApi.get('/users/current');
             return {...state, apiToken: action.payload};
         }
         case getType(actions.login): {
@@ -20,6 +23,8 @@ const authReducer = (state: AuthState = {}, action: AuthAction) => {
             return state;
         }
         case getType(actions.logout): {
+            authService.removeApiToken();
+            removeServerApiAuthorizationHeader();
             authService.logout();
             return {...state, apiToken: null };
         }