summaryrefslogblamecommitdiffstats
path: root/server/api/authentication.js
blob: 60b04f9917446fde4e7d4ca695d489596d7484f8 (plain) (tree)
1
2
3
4
5
6
7






                                                                          







                                                                                                                                                                    














                                                      

                                                                                                                                                                    



                                          
/* 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'))

// Setup method for checking if setup is possible.
noAuthRouter.get('/setup', (req, res) => {
  db.user.findAll().then(users => {
    if (users.length > 0) res.status(403).send({ status: 'USERTABLE_NOT_EMPTY', error_message: 'The user table is not empty, unauthorized creation is forbidden.' })
    else res.send({ status: 'SUCCESS' })
  })
})

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.status(403).send({ status: 'USERTABLE_NOT_EMPTY', error_message: 'The user table is not empty, unauthorized creation is forbidden.' })
    else authentication.signup(req, res)
  })
})

module.exports.noAuthRouter = noAuthRouter