qpa-client/webapp/src/InitiateSession.tsx

65 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-09-29 15:03:31 +02:00
import axios from 'axios';
import * as React from 'react';
2018-09-30 09:36:17 +02:00
import {Link, match} from "react-router-dom";
2018-09-29 15:03:31 +02:00
interface Props {
match: match<{hash: string, email: string}>
}
2018-09-30 12:33:59 +02:00
type LoginStatus = 'success' | 'error' | 'failure' | 'loading'
2018-09-30 09:36:17 +02:00
interface State {
2018-09-30 12:33:59 +02:00
loginStatus: LoginStatus
2018-09-30 09:36:17 +02:00
}
2018-09-29 15:03:31 +02:00
2018-09-30 09:36:17 +02:00
class InitiateSession extends React.Component<Props, State> {
2018-09-30 12:33:59 +02:00
constructor(props: Props) {
super(props);
this.state = {loginStatus: 'loading'}
}
2018-09-30 09:36:17 +02:00
async componentDidMount() {
const { hash } = this.props.match.params
2018-09-30 12:33:59 +02:00
let loginStatus: LoginStatus = 'loading';
this.setState({loginStatus})
try {
const response = await axios.post('/api/signin', {
hash,
})
if (response.status === 200) {
loginStatus = 'success'
} else if (response.status === 403) {
loginStatus = 'failure'
} else if (response.status === 401) {
loginStatus = 'failure';
}
} catch (e) {
2018-10-01 07:46:47 +02:00
const err = e
console.log('caught error', err)
2018-09-30 12:33:59 +02:00
loginStatus = 'error';
}
if (loginStatus === undefined) {
throw new Error('Could not determine login statue')
}
2018-09-30 09:36:17 +02:00
this.setState({loginStatus})
2018-09-29 15:03:31 +02:00
}
render() {
2018-09-30 09:36:17 +02:00
return <div>
<h1>Thanks for coming back, we will log you in now.</h1>
{
this.state.loginStatus === 'loading' && <div>Please wait ...</div>
}
{
2018-09-30 12:33:59 +02:00
this.state.loginStatus === 'success' && <div>You are now logged in. <Link to="/events/create">Create an event</Link></div>
2018-09-30 09:36:17 +02:00
}
{
this.state.loginStatus === 'failure' && <div>Could not log you in</div>
}
<h1>Thanks for coming back, we will log you in now.</h1>
</div>
2018-09-29 15:03:31 +02:00
}
}
export default InitiateSession