refactor: logic and routing folders on new-admin

This commit is contained in:
Sérgio Salgado 2021-01-22 15:36:40 +00:00 committed by Josh Harvey
parent 0c7c7ccf42
commit d9e3a9e61f
10 changed files with 94 additions and 87 deletions

View file

@ -11,14 +11,13 @@ const cookieParser = require('cookie-parser')
const { ApolloServer, AuthenticationError } = require('apollo-server-express')
const _ = require('lodash/fp')
const T = require('../time')
const { typeDefs, resolvers } = require('./graphql/schema')
const login = require('./modules/login')
const register = require('./routes/authentication')
const options = require('../options')
const login = require('./login')
const { typeDefs, resolvers } = require('./graphql/schema')
const devMode = require('minimist')(process.argv.slice(2)).dev
const NEVER = new Date(Date.now() + 100 * T.years)
const idPhotoCardBasedir = _.get('idPhotoCardDir', options)
const frontCameraBasedir = _.get('frontCameraDir', options)
@ -66,35 +65,7 @@ app.use(cors({ credentials: true, origin: devMode && 'https://localhost:3001' })
app.use('/id-card-photo', serveStatic(idPhotoCardBasedir, { index: false }))
app.use('/front-camera-photo', serveStatic(frontCameraBasedir, { index: false }))
app.get('/api/register', (req, res, next) => {
const otp = req.query.otp
const ua = req.headers['user-agent']
const ip = req.ip
if (!otp) return next()
return login.register(otp, ua, ip)
.then(r => {
if (r.expired) return res.status(401).send('OTP expired, generate new registration link')
// Maybe user is using old registration key, attempt to authenticate
if (!r.success) return next()
const cookieOpts = {
httpOnly: true,
secure: true,
domain: hostname,
sameSite: true,
expires: NEVER
}
const token = r.token
req.token = token
res.cookie('token', token, cookieOpts)
res.sendStatus(200)
})
})
app.use('/', register)
// Everything not on graphql or api/register is redirected to the front-end
app.get('*', (req, res) => res.sendFile(path.resolve(__dirname, '..', '..', 'public', 'index.html')))