Skip to content

Commit

Permalink
Authenticate user prior to creating a new challenge
Browse files Browse the repository at this point in the history
Fixes that users who tried to create a new challenge without having
played the game would receive an authentication error from Firebase.

Closes #110
  • Loading branch information
antw committed Aug 26, 2020
1 parent ee49eb7 commit b1fdf09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
15 changes: 9 additions & 6 deletions src/components/NewChallenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import injectIntl from '../utils/injectIntl';

import Header from './Header';

import authenticate from '../utils/authenticate';
import randomId from '../utils/randomId';
import challengeExpiry from '../utils/challengeExpiry';

Expand Down Expand Up @@ -37,12 +38,14 @@ class NewChallenge extends React.Component {
const expires = expiryFromSelect(this.expiresField);
const mode = this.difficultyField.value;

this.props.base.post(
`challenges/${challengeId}`, { data: { name, expires, mode } }
).then(
() => this.setState({ challengeId, name }),
() => this.setState(this.errorFor('challenges.errors.starting'))
);
authenticate(this.props.base, () => {
this.props.base.post(
`challenges/${challengeId}`, { data: { name, expires, mode } }
).then(
() => this.setState({ challengeId, name }),
() => this.setState(this.errorFor('challenges.errors.starting'))
);
});
} else {
this.setState(this.errorFor('challenges.errors.missingName'));
}
Expand Down
10 changes: 8 additions & 2 deletions src/components/__tests__/NewChallenge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ it('render a name field', () => {

it('triggers onSubmit when submitting', () => {
const promise = Promise.resolve();
const base = { post: jest.fn().mockReturnValue(promise) };
const base = {
post: jest.fn().mockReturnValue(promise),
onAuth: cb => cb({ uid: 'abc' })
};

const wrapper = mountWithIntl(
<MemoryRouter initialEntries={['/new-challenge']}>
Expand Down Expand Up @@ -62,7 +65,10 @@ it('does not submit if the name is blank', () => {

it('shows an error when the post fails', () => {
const promise = Promise.reject();
const base = { post: jest.fn().mockReturnValue(promise) };
const base = {
post: jest.fn().mockReturnValue(promise),
onAuth: cb => cb({ uid: 'abc' })
};

const wrapper = mountWithIntl(
<NewChallenge base={base} defaultName="Hi" />
Expand Down

0 comments on commit b1fdf09

Please sign in to comment.