Fixed login step

This commit is contained in:
Amit Jakubowicz 2018-09-30 12:33:59 +02:00
parent 9c4df176b9
commit 237a44e87e
3 changed files with 32 additions and 17 deletions

View File

@ -6,9 +6,8 @@ import {domain} from './config'
import Repository from './repository'
import {DBEntity, User} from "./types";
export class SessionAlreadyValidatedError extends Error {
export class SessionAlreadyValidatedError extends Error {}
}
const generateHash = () => randomstring({
length: 48,
letters: true,
@ -68,7 +67,7 @@ export default class SessionManager {
await sendEmail({
to: user.email,
from: `signin@${domain}`,
text: `Follow this link to start a session: https://${domain}/signin?hash=${invite.hash}&email=${user.email}`,
text: `Follow this link to start a session: https://${domain}/login/${invite.hash}`,
subject: 'Invitation for session'
})
resolve(invite)

View File

@ -5,24 +5,39 @@ import {Link, match} from "react-router-dom";
interface Props {
match: match<{hash: string, email: string}>
}
type LoginStatus = 'success' | 'error' | 'failure' | 'loading'
interface State {
loginStatus: 'success' | 'error' | 'failure' | 'loading'
loginStatus: LoginStatus
}
const responseCodeToLoginStatus = {
200: 'success',
403: 'failure',
500: 'error'
}
class InitiateSession extends React.Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {loginStatus: 'loading'}
}
async componentDidMount() {
const { hash } = this.props.match.params
this.setState({loginStatus: 'loading'})
const response = await axios.post('/api/signin', {
hash,
})
const loginStatus = responseCodeToLoginStatus[response.status] || 'error';
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) {
loginStatus = 'error';
}
if (loginStatus === undefined) {
throw new Error('Could not determine login statue')
}
this.setState({loginStatus})
}
@ -33,7 +48,7 @@ class InitiateSession extends React.Component<Props, State> {
this.state.loginStatus === 'loading' && <div>Please wait ...</div>
}
{
this.state.loginStatus === 'success' && <div>You are now logged in. <Link to="/events/create">Create your event</Link></div>
this.state.loginStatus === 'success' && <div>You are now logged in. <Link to="/events/create">Create an event</Link></div>
}
{
this.state.loginStatus === 'failure' && <div>Could not log you in</div>

View File

@ -16,6 +16,7 @@
"object-literal-sort-keys":false,
"no-console": false,
"interface-name" : false,
"no-empty-interface" : false
"no-empty-interface" : false,
"max-classes-per-file" : false
}
}