Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
27 changed files
with
10,708 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# BlogSpot | ||
This is 5001 CEM Coursework | ||
# dungranij-sem1 | ||
my topic is news (SID - 9963148) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import Koa from 'koa' | ||
import serve from 'koa-static' | ||
import views from 'koa-views' | ||
import session from 'koa-session' | ||
|
||
import router from './routes/routes.js' | ||
|
||
const app = new Koa() | ||
app.keys = ['darkSecret'] | ||
|
||
const defaultPort = 8080 | ||
const port = process.env.PORT || defaultPort | ||
|
||
async function getHandlebarData(ctx, next) { | ||
console.log(`${ctx.method} ${ctx.path}`) | ||
ctx.hbs = { | ||
authorised: ctx.session.authorised, | ||
host: `https://${ctx.host}` | ||
} | ||
for(const key in ctx.query) ctx.hbs[key] = ctx.query[key] | ||
await next() | ||
} | ||
|
||
app.use(serve('public')) | ||
app.use(session(app)) | ||
app.use(views('views', { extension: 'handlebars' }, {map: { handlebars: 'handlebars' }})) | ||
|
||
app.use(getHandlebarData) | ||
|
||
app.use(router.routes()) | ||
app.use(router.allowedMethods()) | ||
|
||
app.listen(port, async() => console.log(`listening on port ${port}`)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
{ | ||
"tags": { | ||
"allowUnknownTags": true, | ||
"dictionaries": ["jsdoc","closure"] | ||
}, | ||
"source": { | ||
"include": [ "." ], | ||
"exclude": [ "node_modules", "routes" ], | ||
"includePattern": ".+\\.js(doc|x)?$", | ||
"excludePattern": "(^|\\/|\\\\)_" | ||
}, | ||
"plugins": ["jsdoc-route-plugin"], | ||
"templates": { | ||
"cleverLinks": false, | ||
"monospaceLinks": false | ||
}, | ||
"opts": { | ||
"destination": "docs/jsdoc", | ||
"recurse": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
|
||
/** @module Accounts */ | ||
|
||
import bcrypt from 'bcrypt-promise' | ||
import sqlite from 'sqlite-async' | ||
|
||
const saltRounds = 10 | ||
|
||
/** | ||
* Accounts | ||
* ES6 module that handles registering accounts and logging in. | ||
*/ | ||
class Accounts { | ||
/** | ||
* Create an account object | ||
* @param {String} [dbName=":memory:"] - The name of the database file to use. | ||
*/ | ||
constructor(dbName = ':memory:') { | ||
return (async() => { | ||
this.db = await sqlite.open(dbName) | ||
// we need this table to store the user accounts | ||
const sql = 'CREATE TABLE IF NOT EXISTS users\ | ||
(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT, email TEXT);' | ||
await this.db.run(sql) | ||
return this | ||
})() | ||
} | ||
|
||
/** | ||
* registers a new user | ||
* @param {String} user the chosen username | ||
* @param {String} pass the chosen password | ||
* @param {String} email the chosen email | ||
* @returns {Boolean} returns true if the new user has been added | ||
*/ | ||
async register(user, pass, email) { | ||
Array.from(arguments).forEach( val => { | ||
if(val.length === 0) throw new Error('missing field') | ||
}) | ||
let sql = `SELECT COUNT(id) as records FROM users WHERE user="${user}";` | ||
const data = await this.db.get(sql) | ||
if(data.records !== 0) throw new Error(`username "${user}" already in use`) | ||
sql = `SELECT COUNT(id) as records FROM users WHERE email="${email}";` | ||
const emails = await this.db.get(sql) | ||
if(emails.records !== 0) throw new Error(`email address "${email}" is already in use`) | ||
pass = await bcrypt.hash(pass, saltRounds) | ||
sql = `INSERT INTO users(user, pass, email) VALUES("${user}", "${pass}", "${email}")` | ||
await this.db.run(sql) | ||
return true | ||
} | ||
|
||
/** | ||
* checks to see if a set of login credentials are valid | ||
* @param {String} username the username to check | ||
* @param {String} password the password to check | ||
* @returns {Boolean} returns true if credentials are valid | ||
*/ | ||
async login(username, password) { | ||
let sql = `SELECT count(id) AS count FROM users WHERE user="${username}";` | ||
const records = await this.db.get(sql) | ||
if(!records.count) throw new Error(`username "${username}" not found`) | ||
sql = `SELECT pass FROM users WHERE user = "${username}";` | ||
const record = await this.db.get(sql) | ||
const valid = await bcrypt.compare(password, record.pass) | ||
if(valid === false) throw new Error(`invalid password for account "${username}"`) | ||
return true | ||
} | ||
|
||
async close() { | ||
await this.db.close() | ||
} | ||
} | ||
|
||
export default Accounts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
|
||
|
||
import sqlite from 'sqlite-async' | ||
|
||
|
||
class newsinfo { | ||
|
||
|
||
constructor(dbName = ':memory:') { | ||
return (async() => { | ||
this.db = await sqlite.open(dbName) | ||
//table to store user account | ||
const sql ='CREATE TABLE IF NOT EXISTS newsinfo(\ | ||
id INTEGER PRIMARY KEY AUTOINCREMENT,\ | ||
userid INTEGER,\ | ||
title TEXT,\ | ||
headline TEXT,\ | ||
lastAdded INTEGER,\ | ||
FOREIGN KEY(userid) REFERENCES users(id)\ | ||
);' | ||
await this.db.run(sql) | ||
return this | ||
})() | ||
} | ||
|
||
|
||
async all() { | ||
const sql ='SELECT users.user, newsinfo.* FROM newsinfo, users\ | ||
WHERE newsinfo.userid = users.id;' | ||
const newsinfo = await this.db.all(sql) | ||
for (const index in newsinfo) { | ||
if (newsinfo[index].photo === null ) newsinfo[index].photo === 'avatar.jpg' | ||
const dateTime = new Date(newsinfo[index].lastAdded) | ||
const date = `${dateTime.getDate()}/${dateTime.getMonth()+1}/${dateTime.getFullYear()}` | ||
newsinfo[index].lastAdded = date | ||
} | ||
return newsinfo | ||
} | ||
async getByID(id) { | ||
try{ | ||
const sql = `SELECT users.user, newsinfo.* FROM newsinfo, users\ | ||
WHERE newsinfo.userid = users.id AND newsinfo.id = ${id};` | ||
console.log(sql) | ||
const newsinfo =await this.db.get(sql) | ||
if(newsinfo.photo === null) newsinfo.photo = 'avatar.jpg' | ||
const dateTime = new Date(newsinfo.lastAdded ) | ||
const date = `${dateTime.getDate()}/${dateTime.getMonth()+1}/${dateTime.getFullYear()}` | ||
newsinfo.lastAdded = date | ||
return newsinfo | ||
} catch(err) { | ||
console.log(err) | ||
throw err | ||
} | ||
} | ||
|
||
} | ||
|
||
export default newsinfo |
Oops, something went wrong.