X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/6e4ce0e45818dac7d6abc6456e85d49798d16458..ced45767b046dd910422934a3d1afa9f4aabc2ec:/src/services/auth-service/auth-service.ts diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts index 11594c980d..80d13e3c64 100644 --- a/src/services/auth-service/auth-service.ts +++ b/src/services/auth-service/auth-service.ts @@ -2,8 +2,22 @@ // // SPDX-License-Identifier: AGPL-3.0 -const API_TOKEN_KEY = 'api_token'; -const API_HOST = 'https://qr1hi.arvadosapi.com'; +import { API_HOST, serverApi } from "../../common/server-api"; +import { User } from "../../models/user"; +import { Dispatch } from "redux"; +import actions from "../../store/auth/auth-action"; + +export const API_TOKEN_KEY = 'apiToken'; +export const USER_EMAIL_KEY = 'userEmail'; +export const USER_FIRST_NAME_KEY = 'userFirstName'; +export const USER_LAST_NAME_KEY = 'userLastName'; + +export interface UserDetailsResponse { + email: string; + first_name: string; + last_name: string; + is_admin: boolean; +} export default class AuthService { @@ -16,19 +30,48 @@ export default class AuthService { } public getApiToken() { - return localStorage.getItem(API_TOKEN_KEY); + return localStorage.getItem(API_TOKEN_KEY) || undefined; + } + + public getUser(): User | undefined { + const email = localStorage.getItem(USER_EMAIL_KEY); + const firstName = localStorage.getItem(USER_FIRST_NAME_KEY); + const lastName = localStorage.getItem(USER_LAST_NAME_KEY); + return email && firstName && lastName + ? { email, firstName, lastName } + : undefined; + } + + public saveUser(user: User) { + localStorage.setItem(USER_EMAIL_KEY, user.email); + localStorage.setItem(USER_FIRST_NAME_KEY, user.firstName); + localStorage.setItem(USER_LAST_NAME_KEY, user.lastName); } - public isUserLoggedIn() { - return this.getApiToken() !== null; + public removeUser() { + localStorage.removeItem(USER_EMAIL_KEY); + localStorage.removeItem(USER_FIRST_NAME_KEY); + localStorage.removeItem(USER_LAST_NAME_KEY); } public login() { const currentUrl = `${window.location.protocol}//${window.location.host}/token`; - window.location.href = `${API_HOST}/login?return_to=${currentUrl}`; + window.location.assign(`${API_HOST}/login?return_to=${currentUrl}`); } public logout() { - this.removeApiToken(); + const currentUrl = `${window.location.protocol}//${window.location.host}`; + window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`); } + + public getUserDetails = () => (dispatch: Dispatch) => { + dispatch(actions.USER_DETAILS_REQUEST()); + serverApi + .get('/users/current') + .then(resp => { + dispatch(actions.USER_DETAILS_SUCCESS(resp.data)); + }) + // .catch(err => { + // }); + }; }