summaryrefslogtreecommitdiffstats
path: root/server/api/authentication.js
blob: 050031cba850505527a0c7b7758fdba7cf0cb845 (plain) (blame)
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
/* global __appdir */
const path = require('path')
var db = require(path.join(__appdir, 'lib', 'sequelize'))
var express = require('express')
var noAuthRouter = express.Router()
var authentication = require(path.join(__appdir, 'lib', 'authentication'))

noAuthRouter.post('/token', (req, res) => {
  authentication.loginToken(req, res)
})

noAuthRouter.post('/login', (req, res) => {
  authentication.loginCookie(req, res)
})

noAuthRouter.post('/logout', (req, res) => {
  authentication.logout(req, res)
})

// Setup method for creating the initial root account.
noAuthRouter.post('/setup', (req, res) => {
  db.user.findAll().then(users => {
    if (users.length > 0) res.send({ status: 'USERTABLE_NOT_EMPTY', error_message: 'The user table is not empty, unauthorized creation is forbidden.' })
    else if (req.body.username) authentication.signup(req, res)
    else res.send({ status: 'SUCCESS' })
  })
})

module.exports.noAuthRouter = noAuthRouter