//
// SPDX-License-Identifier: AGPL-3.0
-import { API_HOST } from "../../common/server-api";
+import { API_HOST, serverApi } from "../../common/api/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 {
public saveApiToken(token: string) {
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() {
const currentUrl = `${window.location.protocol}//${window.location.host}`;
- window.location.href = `${API_HOST}/logout?return_to=${currentUrl}`;
+ window.location.assign(`${API_HOST}/logout?return_to=${currentUrl}`);
}
+
+ public getUserDetails = () => (dispatch: Dispatch) => {
+ dispatch(actions.USER_DETAILS_REQUEST());
+ serverApi
+ .get<UserDetailsResponse>('/users/current')
+ .then(resp => {
+ dispatch(actions.USER_DETAILS_SUCCESS(resp.data));
+ })
+ // .catch(err => {
+ // });
+ };
}