bc0caf56dff4e976a2952e185c3cf766f607001a
[arvados.git] / src / components / api-token / api-token.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Redirect, RouteProps } from "react-router";
6 import * as React from "react";
7 import { connect, DispatchProp } from "react-redux";
8 import authActions, { getUserDetails } from "../../store/auth-action";
9
10 interface ApiTokenProps {
11 }
12
13 class ApiToken extends React.Component<ApiTokenProps & RouteProps & DispatchProp<any>, {}> {
14     static getUrlParameter(search: string, name: string) {
15         const safeName = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
16         const regex = new RegExp('[\\?&]' + safeName + '=([^&#]*)');
17         const results = regex.exec(search);
18         return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
19     };
20
21     componentDidMount() {
22         const search = this.props.location ? this.props.location.search : "";
23         const apiToken = ApiToken.getUrlParameter(search, 'api_token');
24         this.props.dispatch(authActions.SAVE_API_TOKEN(apiToken));
25         this.props.dispatch(getUserDetails());
26     }
27     render() {
28         return <Redirect to="/"/>
29     }
30 }
31
32 export default connect()(ApiToken);