import React, { Fragment } from 'react'; import { Dialog, DialogTitle, DialogContent, DialogActions, Box, LinearProgress, Typography, TextField } from '@material-ui/core'; import { FormButton } from '../components'; import { redirectingAuthorizedFetch } from '../authentication'; import { GENERATE_TOKEN_ENDPOINT } from '../api'; import { withSnackbar, WithSnackbarProps } from 'notistack'; interface GenerateTokenProps extends WithSnackbarProps { username: string; onClose: () => void; } interface GenerateTokenState { token?: string; } class GenerateToken extends React.Component { state: GenerateTokenState = {}; componentDidMount() { const { username } = this.props; redirectingAuthorizedFetch(GENERATE_TOKEN_ENDPOINT + "?" + new URLSearchParams({ username }), { method: 'GET' }) .then(response => { if (response.status === 200) { return response.json(); } else { throw Error("Error generating token: " + response.status); } }).then(generatedToken => { console.log(generatedToken); this.setState({ token: generatedToken.token }); }) .catch(error => { this.props.enqueueSnackbar(error.message || "Problem generating token", { variant: 'error' }); }); } render() { const { onClose, username } = this.props; const { token } = this.state; return ( Token for: {username} {token ? The token below may be used to access the secured APIs. This may be used for bearer authentication with the "Authorization" header or using the "access_token" query paramater. : Generating token… } Close ); } } export default withSnackbar(GenerateToken);