import React from 'react'; import superagent from 'superagent'; import BigApiErrorMessage from '../CloudApi/BigApiErrorMessage.jsx'; import { Container, Menu, Segment, Grid, Header, Image, Icon, Button, Form, Divider } from 'semantic-ui-react' export default class Login extends React.Component { constructor(props) { super(props); this.state = { email: '', password: '', loading: false }; } onChange(e, { name, value }) { this.setState({ [name]: value }) } getReturnToFromUrl() { // Plan 14 Phase B: /oauth/authorize redirects here with ?returnTo=… // so that after login the user lands back at the consent screen. try { const params = new URLSearchParams(window.location.search); return params.get('returnTo') || ''; } catch (e) { return ''; } } async onLogin(event) { event.preventDefault(); let payload = { email: this.state.email, password: this.state.password, returnTo: this.getReturnToFromUrl() }; this.setState({ loading: true }); try { const res = await superagent.post('/api/auth/login') .accept('json') .send(payload); // Server validates returnTo same-origin and echoes back the // sanitised value (or '/'). Trust the server's answer. const target = (res.body && res.body.returnTo) || '/'; window.location.href = target; } catch (e) { this.setState({ error: e }); } this.setState({ loading: false }); } render() { return (
{this.props.title}