Adding and deleting users

This commit is contained in:
Mark Cooper 2021-08-27 07:01:47 -05:00
parent 88c7c1632e
commit 23f343f1df
15 changed files with 323 additions and 12 deletions

View file

@ -1,5 +1,6 @@
const express = require('express')
const Logger = require('./Logger')
const User = require('./User')
const { isObject } = require('./utils/index')
class ApiController {
@ -33,10 +34,14 @@ class ApiController {
this.router.patch('/match/:id', this.match.bind(this))
this.router.get('/users', this.getUsers.bind(this))
this.router.post('/user', this.createUser.bind(this))
this.router.delete('/user/:id', this.deleteUser.bind(this))
this.router.delete('/user/audiobook/:id', this.resetUserAudiobookProgress.bind(this))
this.router.patch('/user/password', this.userChangePassword.bind(this))
this.router.patch('/user/settings', this.userUpdateSettings.bind(this))
this.router.post('/authorize', this.authorize.bind(this))
this.router.get('/genres', this.getGenres.bind(this))
@ -255,6 +260,53 @@ class ApiController {
})
}
async createUser(req, res) {
var account = req.body
account.id = (Math.trunc(Math.random() * 1000) + Date.now()).toString(36)
account.pash = await this.auth.hashPass(account.password)
delete account.password
account.token = await this.auth.generateAccessToken({ userId: account.id })
account.createdAt = Date.now()
var newUser = new User(account)
var success = await this.db.insertUser(newUser)
if (success) {
this.emitter('user_added', newUser)
res.json({
user: newUser.toJSONForBrowser()
})
} else {
res.json({
error: 'Failed to save new user'
})
}
}
async deleteUser(req, res) {
if (req.params.id === 'root') {
return res.sendStatus(500)
}
if (req.user.id === req.params.id) {
Logger.error('Attempting to delete themselves...')
return res.sendStatus(500)
}
var user = this.db.users.find(u => u.id === req.params.id)
if (!user) {
Logger.error('User not found')
return res.json({
error: 'User not found'
})
}
// Todo: check if user is logged in and cancel streams
var userJson = user.toJSONForBrowser()
await this.db.removeEntity('user', user.id)
this.emitter('user_removed', userJson)
res.json({
success: true
})
}
getGenres(req, res) {
res.json({
genres: this.db.getGenres()

View file

@ -2,7 +2,6 @@ const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
const Logger = require('./Logger')
class Auth {
constructor(db) {
this.db = db
@ -90,7 +89,7 @@ class Auth {
var password = req.body.password || ''
Logger.debug('Check Auth', username, !!password)
var user = this.users.find(u => u.id === username)
var user = this.users.find(u => u.username === username)
if (!user) {
return res.json({ error: 'User not found' })

View file

@ -58,6 +58,7 @@ class Db {
pash: '',
stream: null,
token,
isActive: true,
createdAt: Date.now()
})
}
@ -115,8 +116,10 @@ class Db {
return this.usersDb.insert([user]).then((results) => {
Logger.debug(`[DB] Inserted user ${results.inserted}`)
this.users.push(user)
return true
}).catch((error) => {
Logger.error(`[DB] Insert user Failed ${error}`)
return false
})
}

View file

@ -6,6 +6,7 @@ class User {
this.type = null
this.stream = null
this.token = null
this.isActive = true
this.createdAt = null
this.audiobooks = null
this.settings = {}
@ -34,6 +35,7 @@ class User {
stream: this.stream,
token: this.token,
audiobooks: this.audiobooks,
isActive: this.isActive,
createdAt: this.createdAt,
settings: this.settings
}
@ -47,6 +49,7 @@ class User {
stream: this.stream,
token: this.token,
audiobooks: this.audiobooks,
isActive: this.isActive,
createdAt: this.createdAt,
settings: this.settings
}
@ -57,10 +60,11 @@ class User {
this.username = user.username
this.pash = user.pash
this.type = user.type
this.stream = user.stream
this.stream = user.stream || null
this.token = user.token
this.audiobooks = user.audiobooks || null
this.createdAt = user.createdAt
this.isActive = (user.isActive === undefined || user.id === 'root') ? true : !!user.isActive
this.createdAt = user.createdAt || Date.now()
this.settings = user.settings || this.getDefaultUserSettings()
}