Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#!/usr/bin/env node
'use strict'
const Koa = require('koa')
const Router = require('koa-router')
const staticDir = require('koa-static')
const bodyParser = require('koa-bodyparser')
const koaBody = require('koa-body')({multipart: true, uploadDir: '.'})
const session = require('koa-session')
const sqlite = require('sqlite-async')
const bcrypt = require('bcrypt-promise')
const handlebars = require('koa-hbs-renderer')
const app = new Koa()
const router = new Router()
app.keys = ['darkSecret']
app.use(staticDir('public'))
app.use(bodyParser())
app.use(session(app))
app.use(handlebars({ paths: { views: `${__dirname}/views` } }))
app.use(router.routes())
const port = 8081
const saltRounds=10
router.get('/', async ctx => await ctx.render('main'))
router.get('/user-home-page', async ctx => await ctx.render('user-home-page'))
router.get('/login', async ctx => {
const data = {}
if(ctx.query.msg) data.msg = ctx.query.msg
if(ctx.query.user) data.user = ctx.query.user
await ctx.render('login', data)
})
router.post('/login', async ctx => {
try {
const body = ctx.request.body
const db = await sqlite.open('../5001.db')
// CHECKS IF THE USERNAME IS CORRECT
const records = await db.get(`SELECT count(user) AS count FROM users WHERE user="${body.user}";`)
if(!records.count) return ctx.redirect('/login?msg=invalid%20username')
// CHECKS IF THE PASSWORD IS CORRECT
const record = await db.get(`SELECT pass FROM users WHERE user = "${body.user}";`)
await db.close()
const valid = await bcrypt.compare(body.pass, record.pass)
//IF THE PASSWORD IS CORRECT MAKE THE USER AUTHENTICATED
if(valid === false) return ctx.redirect(`/login?user=${body.user}&msg=invalid%20password`)
ctx.session.authorised = true
ctx.session.user = body.user
return ctx.redirect('/user-home-page')
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
//LOGS OUT THE USER AND RETURNS TO THE MAIN PAGE
router.get('/logout', async ctx => {
ctx.session.authorised = null
ctx.session.user = null
ctx.redirect('/')
})
/**
* The user registration page.
*
* @name Register Page
* @route {GET} /register
*/
router.get('/register', async ctx => {
const data = {}
if(ctx.query.msg) data.msg = ctx.query.msg
await ctx.render('register', data)
})
/**s
* The script to process new user registrations.
*
* @name Register Script
* @route {POST} /register
*/
router.post('/register', koaBody, async ctx => {
try {
const body = ctx.request.body
console.log(body)
// ENCRYPTING PASSWORD AND BUILDING SQL
body.pass = await bcrypt.hash(body.pass, saltRounds)
//SENDS THE DATA TO THE DATABASE
const sql = `INSERT INTO users(user, pass) VALUES("${body.user}", "${body.pass}")`
//PRINTS THE DATA THAT IS ABOUT TO GO TO THE DB INTO THE CONSOLE
console.log(sql)
// DATABASE COMMANDS
const db = await sqlite.open('../5001.db')
await db.run(sql)
await db.close()
// REDIRECTING USER TO HOME PAGE
ctx.redirect(`/login?msg=new user "${body.user}" added`)
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/report', async ctx => {
try{
//IF THE USER TRIES TO ACCESS THIS PAGE THROUGH THE URL, IT DENIES THE ACCESS
if(ctx.session.authorised !== true) return ctx.redirect('/unauthorised')
const data = {}
if(ctx.query.msg) data.msg = ctx.query.msg
await ctx.render('report', data)
}catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.post('/report', koaBody, async ctx => {
try{
const body = ctx.request.body
console.log(body)
const db = await sqlite.open('../5001.db')
const sql = `INSERT INTO report
(id, appliance, age, manufacturer, fault)
VALUES("${body.id}", "${body.appliance}", "${body.age}", "${body.manufacturer}", "${body.fault}")`
await db.run(sql)
await db.close()
return ctx.redirect(`/user-home-page?msg=appliance "${body.appliance}" added`)
} catch(err) {
await ctx.render('error', {message: err.message})
}
})
router.get('/jobs', async ctx => {
try {
console.log('/jobs')
const sql = 'SELECT id, appliance, age, manufacturer, fault FROM report;'
const db = await sqlite.open('../5001.db')
const data = await db.all(sql)
await db.close()
console.log(data)
await ctx.render('jobs', {report: data })
} catch(err) {
ctx.body = err.message
}
})
module.exports = app.listen(port, () => console.log(`listening on port ${port}`))
//