-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
59 lines (50 loc) · 1.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const choo = require('choo')
const http = require('choo/http')
const qs = require('sheet-router/qs')
const Layout = require('./views/layout')
const CaseView = require('./views/case')
const CaseListView = require('./views/case-list')
const HomeView = require('./views/home')
const SignInView = require('./views/sign-in')
const VerifyView = require('./views/verify')
const config = require('./config')
const app = choo()
if (process.env.NODE_ENV !== 'production') {
const log = require('choo-log')
app.use(log())
}
// If token present, authenticate before firing any other requests.
// Not very chooey but no other option comes to mind
const token = qs(window.location.search).token
if (token) {
authenticate(token, initialize)
} else {
initialize()
}
function initialize () {
app.model(require('./models/ui'))
app.model(require('./models/user'))
app.model(require('./models/case'))
app.model(require('./models/case-list'))
app.router((route) => [
route('/', Layout(HomeView)),
route('/:workflowSlug', Layout(CaseView), [
route('/:caseId', Layout(CaseView))
]),
route('/cases', Layout(CaseListView)),
route('/sign-in', Layout(SignInView)),
route('/verify', Layout(VerifyView))
])
const tree = app.start()
document.body.appendChild(tree)
}
function authenticate (token, callback) {
const uri = `${config.endpoint}authenticate/?token=${token}`
const opts = { json: true, withCredentials: true }
http(uri, opts, (err, response, body) => {
if (err || response.statusCode !== 200) {
console.error('Error authenticating')
}
callback()
})
}