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?
Forum-Website/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.
76 lines (63 sloc)
2.19 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
'use strict' | |
const bcrypt = require('bcrypt-promise') | |
//const fs = require('fs-extra') | |
//const mime = require('mime-types') | |
const sqlite = require('sqlite-async') | |
const saltRounds = 10 | |
module.exports = class User { | |
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, profile TEXT);` | |
await this.db.run(sql) | |
return this | |
})() | |
} | |
async register(user, pass, path) { | |
try { | |
if(user.length === 0) throw new Error('missing username') | |
if(pass.length === 0) throw new Error('missing password') | |
//const len =3 | |
//if(pass.length < len) throw new Error('weak password') | |
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`) | |
pass = await bcrypt.hash(pass, saltRounds) | |
sql = `INSERT INTO users(user, pass, profile) VALUES("${user}", "${pass}","${path}")` | |
await this.db.run(sql) | |
return true | |
} catch(err) { | |
throw err | |
} | |
} | |
/*async uploadPicture(path, mimeType) { | |
const extension = mime.extension(mimeType) | |
console.log(`path: ${path}`) | |
console.log(`extension: ${extension}`) | |
//await fs.copy(path, `public/avatars/${username}.${fileExtension}`) | |
}*/ | |
async login(username, password) { | |
try { | |
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 * 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}"`) | |
delete record.pass | |
return record | |
} catch(err) { | |
throw err | |
} | |
} | |
async getUserDetails(userData) { | |
const userSql = `SELECT * FROM users WHERE user = "${userData.user}";` | |
const selectedUserData = await this.db.get(userSql) | |
delete selectedUserData.pass | |
return selectedUserData | |
} | |
} |