mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-02-12 08:19:07 +00:00
New database structure with users support
This commit is contained in:
parent
b3cdc406d2
commit
13b2242bdf
@ -1,9 +1,9 @@
|
|||||||
|
let init = function(db){
|
||||||
let init = function(db, config){
|
|
||||||
|
|
||||||
// Create the tables we need to store galleries and files
|
// Create the tables we need to store galleries and files
|
||||||
db.schema.createTableIfNotExists('albums', function (table) {
|
db.schema.createTableIfNotExists('albums', function (table) {
|
||||||
table.increments()
|
table.increments()
|
||||||
|
table.integer('userid')
|
||||||
table.string('name')
|
table.string('name')
|
||||||
table.integer('enabled')
|
table.integer('enabled')
|
||||||
table.integer('timestamp')
|
table.integer('timestamp')
|
||||||
@ -11,6 +11,7 @@ let init = function(db, config){
|
|||||||
|
|
||||||
db.schema.createTableIfNotExists('files', function (table) {
|
db.schema.createTableIfNotExists('files', function (table) {
|
||||||
table.increments()
|
table.increments()
|
||||||
|
table.integer('userid')
|
||||||
table.string('name')
|
table.string('name')
|
||||||
table.string('original')
|
table.string('original')
|
||||||
table.string('type')
|
table.string('type')
|
||||||
@ -21,48 +22,28 @@ let init = function(db, config){
|
|||||||
table.integer('timestamp')
|
table.integer('timestamp')
|
||||||
}).then(() => {})
|
}).then(() => {})
|
||||||
|
|
||||||
db.schema.createTableIfNotExists('tokens', function (table) {
|
db.schema.createTableIfNotExists('users', function (table) {
|
||||||
table.string('name')
|
table.increments()
|
||||||
table.string('value')
|
table.string('username')
|
||||||
|
table.string('password')
|
||||||
|
table.string('token')
|
||||||
table.integer('timestamp')
|
table.integer('timestamp')
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
|
db.table('users').where({username: 'root'}).then((user) => {
|
||||||
|
if(user.length > 0) return
|
||||||
|
|
||||||
// == Generate a 1 time token == //
|
require('bcrypt').hash('root', 10, function(err, hash) {
|
||||||
db.table('tokens').then((tokens) => {
|
if(err) console.error('Error generating password hash for root')
|
||||||
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').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
|
module.exports = init
|
Loading…
Reference in New Issue
Block a user