Skip to content

Commit

Permalink
Added /verify#token=
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-robert committed Feb 29, 2020
1 parent 2d4cbac commit 1f91230
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
24 changes: 24 additions & 0 deletions client/src/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,30 @@ export const login = ({ teamToken }) => {
})
}

export const verify = ({ verifyToken }) => {
return request('POST', '/auth/verify', {
verifyToken
})
.then(resp => {
switch (resp.kind) {
case 'goodVerify':
localStorage.setItem('token', resp.data.authToken)
route('/challenges')

return {}
case 'badTokenVerification':
case 'badUnknownUser':
return {
verifyToken: resp.message
}
default:
return {
verifyToken: 'Unknown response from server, please contact ctf administrator'
}
}
})
}

export const register = ({ email, name, division }) => {
return request('POST', '/auth/register', {
email,
Expand Down
3 changes: 2 additions & 1 deletion client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Component } from 'preact'
import config from '../../config/client'

import Header from './components/header'
import { Home, Registration, Login, Profile, Challenges, Scoreboard, Error, Sponsors } from './pages'
import { Home, Registration, Login, Profile, Challenges, Scoreboard, Error, Sponsors, Verify } from './pages'
import 'cirrus-ui'

import util from './util'
Expand Down Expand Up @@ -36,6 +36,7 @@ class App extends Component {

let allPaths = [
<Profile key='multiProfile' path='/profile/:uuid' />,
<Verify key='verify' path='/verify' />,
<Error key='error' error='404' default />
]
allPaths = allPaths.concat(loggedInPaths).concat(loggedOutPaths)
Expand Down
4 changes: 2 additions & 2 deletions client/src/pages/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ export default withStyles({
document.title = 'Error' + config.ctfTitle
}

render ({ error }) {
render ({ error, message }) {
return (
<div class='row u-text-center u-center'>
<div class='col-4'>
<h1>{error}</h1>
<p class='font-thin'>There was an error</p>
<p class='font-thin'>{message || 'There was an error'}</p>
</div>
</div>
)
Expand Down
4 changes: 3 additions & 1 deletion client/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Challenges from './challenges'
import Scoreboard from './scoreboard'
import Error from './error'
import Sponsors from './sponsors'
import Verify from './verify'

export {
Home,
Expand All @@ -15,5 +16,6 @@ export {
Challenges,
Scoreboard,
Error,
Sponsors
Sponsors,
Verify
}
39 changes: 39 additions & 0 deletions client/src/pages/verify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component } from 'preact'
import Error from './error'
import config from '../../../config/client'
import 'linkstate/polyfill'
import withStyles from '../components/jss'

import { verify } from '../api/auth'
import { route } from 'preact-router'

export default withStyles({
}, class Verify extends Component {
state = {
errors: {}
}

componentDidMount () {
document.title = 'Verify' + config.ctfTitle

const prefix = '#token='
if (document.location.hash.startsWith(prefix)) {
route('/verify', true)

const verifyToken = document.location.hash.substring(prefix.length)

verify({ verifyToken })
.then(errors => {
this.setState({
errors
})
})
}
}

render (props, { errors }) {
if (errors.verifyToken === undefined) return <div />

return <Error error='401' message={errors.verifyToken} />
}
})
2 changes: 1 addition & 1 deletion test/integration/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test('fails with badUnknownEmail', async t => {
t.is(resp.body.kind, 'badUnknownEmail')
})

test.serial('when not verifyEmail, succeeds with goodVerify', async t => {
test.serial('when not verifyEmail, succeeds with goodRegister', async t => {
config.verifyEmail = false

let resp = await request(app)
Expand Down

0 comments on commit 1f91230

Please sign in to comment.