Skip to content
Permalink
d1d2a8181a
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
100 lines (92 sloc) 2.92 KB
'use strict'
const bcrypt = require('bcryptjs')
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 (user TEXT PRIMARY KEY, pass TEXT);'
await this.db.run(sql)
await this.db.run(`CREATE TABLE IF NOT EXISTS "uploads" (
"uploadID" INTEGER,
"user" TEXT,
"name" TEXT,
"artist" TEXT,
"genre" TEXT,
"album" TEXT,
"playlist" TEXT,
FOREIGN KEY("user") REFERENCES "users"("user"),
PRIMARY KEY("uploadID" AUTOINCREMENT)
);`)
await this.db.run(`CREATE TABLE IF NOT EXISTS "playlists" (
"playlistID" INTEGER,
"user" TEXT,
"name" TEXT,
"imageName" TEXT,
FOREIGN KEY("user") REFERENCES "users"("user"),
PRIMARY KEY("playlistID")
);`)
await this.db.run(`CREATE TABLE IF NOT EXISTS "audio_ratings" (
"value" TINYINT,
"user" TEXT,
"uploadId" INTEGER
);`)
return this
})()
}
async register(user, pass) {
try {
if(user.length === 0) throw new Error('missing username')
if(pass.length === 0) throw new Error('missing password')
let sql = `SELECT COUNT(user) 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) VALUES("${user}", "${pass}")`
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(user) 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
} catch(err) {
throw err
}
}
//by watrasm - for dynamically loaded login page
async checkLogin(username, password) {
try {
let sql = `SELECT count(user) AS count FROM users WHERE user="${username}";`;
const records = await this.db.get(sql)
let result = records.count > 0;
if (result) {
sql = `SELECT pass FROM users WHERE user = "${username}";`
const record = await this.db.get(sql)
result = await bcrypt.compare(password, record.pass)
}
return result;
} catch(err) {
return false;
}
}
}