aboutsummaryrefslogtreecommitdiff
path: root/database
diff options
context:
space:
mode:
authorPitu <[email protected]>2017-01-29 04:19:02 -0300
committerPitu <[email protected]>2017-01-29 04:19:02 -0300
commit13b2242bdf4c33f120f51f09b8a69b940d55aabe (patch)
tree11833c1ac6ae2cefbea98da672cd6cf29d5f4b7d /database
parentRemoved blank lines (diff)
downloadhost.fuwn.me-13b2242bdf4c33f120f51f09b8a69b940d55aabe.tar.xz
host.fuwn.me-13b2242bdf4c33f120f51f09b8a69b940d55aabe.zip
New database structure with users support
Diffstat (limited to 'database')
-rw-r--r--database/db.js63
1 files changed, 22 insertions, 41 deletions
diff --git a/database/db.js b/database/db.js
index 551c8a5..12638fa 100644
--- a/database/db.js
+++ b/database/db.js
@@ -1,9 +1,9 @@
-
-let init = function(db, config){
+let init = function(db){
// Create the tables we need to store galleries and files
db.schema.createTableIfNotExists('albums', function (table) {
table.increments()
+ table.integer('userid')
table.string('name')
table.integer('enabled')
table.integer('timestamp')
@@ -11,6 +11,7 @@ let init = function(db, config){
db.schema.createTableIfNotExists('files', function (table) {
table.increments()
+ table.integer('userid')
table.string('name')
table.string('original')
table.string('type')
@@ -21,48 +22,28 @@ let init = function(db, config){
table.integer('timestamp')
}).then(() => {})
- db.schema.createTableIfNotExists('tokens', function (table) {
- table.string('name')
- table.string('value')
+ db.schema.createTableIfNotExists('users', function (table) {
+ table.increments()
+ table.string('username')
+ table.string('password')
+ table.string('token')
table.integer('timestamp')
}).then(() => {
-
- // == Generate a 1 time token == //
- db.table('tokens').then((tokens) => {
- if(tokens.length !== 0) return printAndSave(config, tokens[0].value, tokens[1].value)
-
- // This is the first launch of the app
- let clientToken = require('randomstring').generate()
- let adminToken = require('randomstring').generate()
- let now = Math.floor(Date.now() / 1000)
-
- db.table('tokens').insert(
- [
- {
- name: 'client',
- value: clientToken,
- timestamp: now
- },
- {
- name: 'admin',
- value: adminToken,
- timestamp: now
- }
- ]
- ).then(() => {
- printAndSave(config, clientToken, adminToken)
- }).catch(function(error) { console.log(error) })
- }).catch(function(error) { console.log(error) })
-
+ db.table('users').where({username: 'root'}).then((user) => {
+ if(user.length > 0) return
+
+ require('bcrypt').hash('root', 10, function(err, hash) {
+ if(err) console.error('Error generating password hash for root')
+
+ db.table('users').insert({
+ username: 'root',
+ password: hash,
+ token: require('randomstring').generate(64),
+ timestamp: Math.floor(Date.now() / 1000)
+ }).then(() => {})
+ })
+ })
})
-
-}
-
-function printAndSave(config, clientToken, adminToken){
- console.log('Your client token is: ' + clientToken)
- console.log('Your admin token is: ' + adminToken)
- config.clientToken = clientToken
- config.adminToken = adminToken
}
module.exports = init \ No newline at end of file