Permalink
Cannot retrieve contributors at this time
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?
5001cem-DomesticRepairs/modules/user.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (56 sloc)
2.14 KB
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
const bcrypt = require('bcrypt-promise') | |
const sqlite = require('sqlite-async') | |
const saltRounds = 10 | |
module.exports = class User { | |
constructor(dbName = ':memory:') { | |
console.log("constructor") | |
return (async() => { | |
this.db = await sqlite.open(dbName) | |
// we need this table to store the user accounts | |
const sql = 'CREATE TABLE IF NOT EXISTS artist\ | |
(id INTEGER PRIMARY KEY AUTOINCREMENT, user TEXT, pass TEXT, email TEXT);' | |
await this.db.run(sql) | |
return this | |
})() | |
} | |
/** | |
* registers a new artist | |
* @param {String} user the chosen username | |
* @param {String} pass the chosen password | |
* @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 artist 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 artist 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 artist(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 artist WHERE user="${username}";` | |
const records = await this.db.get(sql) | |
if(!records.count) throw new Error(`username "${username}" not found`) | |
sql = `SELECT pass FROM artist 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 username | |
} | |
async tearDown() { | |
await this.db.close() | |
} | |
} |