mirror of
https://github.com/BobbyWibowo/lolisafe.git
synced 2025-01-19 01:31:34 +00:00
Changed request system and post data
Changed from XMLHttpRequest to Axiios and made every POST call to look for params or json and not pass the values as headers. Token is still a header though
This commit is contained in:
parent
93891ae1e5
commit
83aaef0f82
@ -6,16 +6,16 @@ let albumsController = {}
|
|||||||
albumsController.list = function(req, res, next){
|
albumsController.list = function(req, res, next){
|
||||||
|
|
||||||
if(req.headers.auth !== config.adminToken)
|
if(req.headers.auth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
let fields = ['id', 'name']
|
let fields = ['id', 'name']
|
||||||
|
|
||||||
if(req.headers.extended !== undefined)
|
if(req.params.sidebar === undefined)
|
||||||
fields.push('timestamp')
|
fields.push('timestamp')
|
||||||
|
|
||||||
db.table('albums').select(fields).where('enabled', 1).then((albums) => {
|
db.table('albums').select(fields).where('enabled', 1).then((albums) => {
|
||||||
|
|
||||||
if(req.headers.extended === undefined)
|
if(req.params.sidebar !== undefined)
|
||||||
return res.json({ success: true, albums })
|
return res.json({ success: true, albums })
|
||||||
|
|
||||||
let ids = []
|
let ids = []
|
||||||
@ -42,9 +42,9 @@ albumsController.list = function(req, res, next){
|
|||||||
albumsController.create = function(req, res, next){
|
albumsController.create = function(req, res, next){
|
||||||
|
|
||||||
if(req.headers.auth !== config.adminToken)
|
if(req.headers.auth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
let name = req.headers.name
|
let name = req.body.name
|
||||||
if(name === undefined || name === '')
|
if(name === undefined || name === '')
|
||||||
return res.json({ success: false, description: 'No album name specified' })
|
return res.json({ success: false, description: 'No album name specified' })
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ const db = require('knex')(config.database)
|
|||||||
let tokenController = {}
|
let tokenController = {}
|
||||||
|
|
||||||
tokenController.verify = function(req, res, next){
|
tokenController.verify = function(req, res, next){
|
||||||
let type = req.headers.type
|
let type = req.body.type
|
||||||
let token = req.headers.token
|
let token = req.body.token
|
||||||
|
|
||||||
if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
|
if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
|
||||||
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
|
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
|
||||||
@ -26,7 +26,7 @@ tokenController.verify = function(req, res, next){
|
|||||||
|
|
||||||
tokenController.list = function(req, res, next){
|
tokenController.list = function(req, res, next){
|
||||||
if(req.headers.auth !== config.adminToken)
|
if(req.headers.auth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
return res.json({
|
return res.json({
|
||||||
clientToken: config.clientToken,
|
clientToken: config.clientToken,
|
||||||
@ -36,10 +36,10 @@ tokenController.list = function(req, res, next){
|
|||||||
|
|
||||||
tokenController.change = function(req, res, next){
|
tokenController.change = function(req, res, next){
|
||||||
if(req.headers.auth !== config.adminToken)
|
if(req.headers.auth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
let type = req.headers.type
|
let type = req.body.type
|
||||||
let token = req.headers.token
|
let token = req.body.token
|
||||||
|
|
||||||
if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
|
if(type === undefined) return res.json({ success: false, description: 'No type provided.' })
|
||||||
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
|
if(token === undefined) return res.json({ success: false, description: 'No token provided.' })
|
||||||
|
@ -24,13 +24,13 @@ uploadsController.upload = function(req, res, next){
|
|||||||
|
|
||||||
if(config.private === true)
|
if(config.private === true)
|
||||||
if(req.headers.auth !== config.clientToken)
|
if(req.headers.auth !== config.clientToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
let album = req.headers.album
|
let album = req.body.album
|
||||||
|
|
||||||
if(album !== undefined)
|
if(album !== undefined)
|
||||||
if(req.headers.adminauth !== config.adminToken)
|
if(req.headers.adminauth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
upload(req, res, function (err) {
|
upload(req, res, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
@ -81,14 +81,14 @@ uploadsController.upload = function(req, res, next){
|
|||||||
uploadsController.list = function(req, res){
|
uploadsController.list = function(req, res){
|
||||||
|
|
||||||
if(req.headers.auth !== config.adminToken)
|
if(req.headers.auth !== config.adminToken)
|
||||||
return res.status(401).send('not-authorized')
|
return res.status(401).json({ success: false, description: 'not-authorized'})
|
||||||
|
|
||||||
db.table('files')
|
db.table('files')
|
||||||
.where(function(){
|
.where(function(){
|
||||||
if(req.headers.albumid === undefined)
|
if(req.params.id === undefined)
|
||||||
this.where('id', '<>', '')
|
this.where('id', '<>', '')
|
||||||
else
|
else
|
||||||
this.where('albumid', req.headers.albumid)
|
this.where('albumid', req.params.id)
|
||||||
})
|
})
|
||||||
.then((files) => {
|
.then((files) => {
|
||||||
db.table('albums').then((albums) => {
|
db.table('albums').then((albums) => {
|
||||||
@ -114,7 +114,10 @@ uploadsController.list = function(req, res){
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.json(files)
|
return res.json({
|
||||||
|
success: true,
|
||||||
|
files
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
const config = require('./config.js')
|
const config = require('./config.js')
|
||||||
const api = require('./routes/api.js')
|
const api = require('./routes/api.js')
|
||||||
const express = require('express')
|
const express = require('express')
|
||||||
|
const bodyParser = require('body-parser')
|
||||||
const db = require('knex')(config.database)
|
const db = require('knex')(config.database)
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const safe = express()
|
const safe = express()
|
||||||
@ -10,6 +11,9 @@ require('./database/db.js')(db, config)
|
|||||||
fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder)
|
fs.existsSync('./' + config.uploads.folder) || fs.mkdirSync('./' + config.uploads.folder)
|
||||||
fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder)
|
fs.existsSync('./' + config.logsFolder) || fs.mkdirSync('./' + config.logsFolder)
|
||||||
|
|
||||||
|
safe.use(bodyParser.urlencoded({ extended: true }))
|
||||||
|
safe.use(bodyParser.json())
|
||||||
|
|
||||||
safe.enable('trust proxy')
|
safe.enable('trust proxy')
|
||||||
|
|
||||||
safe.use('/', express.static('./uploads'))
|
safe.use('/', express.static('./uploads'))
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.16.0",
|
||||||
"express": "^4.14.0",
|
"express": "^4.14.0",
|
||||||
"knex": "^0.12.6",
|
"knex": "^0.12.6",
|
||||||
"multer": "^1.2.1",
|
"multer": "^1.2.1",
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="/css/style.css">
|
<link rel="stylesheet" type="text/css" href="/css/style.css">
|
||||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
|
||||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/min/dropzone.min.js"></script>
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.3.0/min/dropzone.min.js"></script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.3/axios.min.js"></script>
|
||||||
<script type="text/javascript" src="/js/upload.js"></script>
|
<script type="text/javascript" src="/js/upload.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
|
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
|
||||||
<link rel="stylesheet" type="text/css" href="/css/style.css">
|
<link rel="stylesheet" type="text/css" href="/css/style.css">
|
||||||
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.15.3/axios.min.js"></script>
|
||||||
<script type="text/javascript" src="/js/panel.js"></script>
|
<script type="text/javascript" src="/js/panel.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
@ -15,38 +15,38 @@ panel.preparePage = function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
panel.verifyToken = function(token, reloadOnError = false){
|
panel.verifyToken = function(token, reloadOnError = false){
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
axios.post('/api/tokens/verify', {
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
type: 'admin',
|
||||||
|
token: token
|
||||||
|
})
|
||||||
|
.then(function (response) {
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
if(response.data.success === false){
|
||||||
if(json.success === false){
|
swal({
|
||||||
|
title: "An error ocurred",
|
||||||
|
text: response.data.description,
|
||||||
|
type: "error"
|
||||||
|
}, function(){
|
||||||
|
if(reloadOnError){
|
||||||
|
localStorage.removeItem("admintoken");
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
swal({
|
axios.defaults.headers.common['auth'] = token;
|
||||||
title: "An error ocurred",
|
localStorage.admintoken = token;
|
||||||
text: json.description,
|
panel.token = token;
|
||||||
type: "error"
|
return panel.prepareDashboard();
|
||||||
}, function(){
|
|
||||||
if(reloadOnError){
|
|
||||||
localStorage.removeItem("admintoken");
|
|
||||||
location.reload();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return;
|
})
|
||||||
}
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
localStorage.admintoken = token;
|
|
||||||
panel.token = token;
|
|
||||||
return panel.prepareDashboard();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xhr.open('GET', '/api/tokens/verify', true);
|
|
||||||
xhr.setRequestHeader('type', 'admin');
|
|
||||||
xhr.setRequestHeader('token', token);
|
|
||||||
xhr.send(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.prepareDashboard = function(){
|
panel.prepareDashboard = function(){
|
||||||
@ -70,195 +70,183 @@ panel.prepareDashboard = function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
panel.getUploads = function(album = undefined){
|
panel.getUploads = function(album = undefined){
|
||||||
panel.page.innerHTML = '';
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
let url = '/api/uploads'
|
||||||
if(xhr.readyState == XMLHttpRequest.DONE){
|
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
|
||||||
return panel.verifyToken(panel.token);
|
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
|
||||||
|
|
||||||
if(json.success === false)
|
|
||||||
return swal("An error ocurred", json.description, "error");
|
|
||||||
|
|
||||||
var container = document.createElement('div');
|
|
||||||
container.innerHTML = `
|
|
||||||
<table class="table is-striped is-narrow">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>File</th>
|
|
||||||
<th>Album</th>
|
|
||||||
<th>Date</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody id="table">
|
|
||||||
</tbody>
|
|
||||||
</table>`;
|
|
||||||
panel.page.appendChild(container);
|
|
||||||
|
|
||||||
var table = document.getElementById('table');
|
|
||||||
|
|
||||||
for(var item of json){
|
|
||||||
|
|
||||||
var tr = document.createElement('tr');
|
|
||||||
tr.innerHTML = `
|
|
||||||
<tr>
|
|
||||||
<th><a href="${item.file}" target="_blank">${item.file}</a></th>
|
|
||||||
<th>${item.album}</th>
|
|
||||||
<td>${item.date}</td>
|
|
||||||
</tr>
|
|
||||||
`;
|
|
||||||
|
|
||||||
table.appendChild(tr);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xhr.open('GET', '/api/uploads', true);
|
|
||||||
if(album !== undefined)
|
if(album !== undefined)
|
||||||
xhr.setRequestHeader('albumid', album);
|
url = '/api/album/' + album
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
|
||||||
xhr.send(null);
|
axios.get(url)
|
||||||
|
.then(function (response) {
|
||||||
|
if(response.data.success === false){
|
||||||
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
|
panel.page.innerHTML = '';
|
||||||
|
var container = document.createElement('div');
|
||||||
|
container.innerHTML = `
|
||||||
|
<table class="table is-striped is-narrow">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>File</th>
|
||||||
|
<th>Album</th>
|
||||||
|
<th>Date</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="table">
|
||||||
|
</tbody>
|
||||||
|
</table>`;
|
||||||
|
panel.page.appendChild(container);
|
||||||
|
|
||||||
|
var table = document.getElementById('table');
|
||||||
|
|
||||||
|
for(var item of response.data.files){
|
||||||
|
|
||||||
|
var tr = document.createElement('tr');
|
||||||
|
tr.innerHTML = `
|
||||||
|
<tr>
|
||||||
|
<th><a href="${item.file}" target="_blank">${item.file}</a></th>
|
||||||
|
<th>${item.album}</th>
|
||||||
|
<td>${item.date}</td>
|
||||||
|
</tr>
|
||||||
|
`;
|
||||||
|
|
||||||
|
table.appendChild(tr);
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.getAlbums = function(){
|
panel.getAlbums = function(){
|
||||||
panel.page.innerHTML = '';
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
var container = document.createElement('div');
|
axios.get('/api/albums')
|
||||||
container.className = "container";
|
.then(function (response) {
|
||||||
container.innerHTML = `
|
if(response.data.success === false){
|
||||||
<h2 class="subtitle">Create new album</h2>
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
<p class="control has-addons has-addons-centered">
|
panel.page.innerHTML = '';
|
||||||
<input id="albumName" class="input" type="text" placeholder="Name">
|
var container = document.createElement('div');
|
||||||
<a id="submitAlbum" class="button is-primary">Submit</a>
|
container.className = "container";
|
||||||
</p>
|
container.innerHTML = `
|
||||||
|
<h2 class="subtitle">Create new album</h2>
|
||||||
|
|
||||||
<h2 class="subtitle">List of albums</h2>
|
<p class="control has-addons has-addons-centered">
|
||||||
|
<input id="albumName" class="input" type="text" placeholder="Name">
|
||||||
|
<a id="submitAlbum" class="button is-primary">Submit</a>
|
||||||
|
</p>
|
||||||
|
|
||||||
<table class="table is-striped is-narrow">
|
<h2 class="subtitle">List of albums</h2>
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>Name</th>
|
|
||||||
<th>Files</th>
|
|
||||||
<th>Created At</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody id="table">
|
|
||||||
</tbody>
|
|
||||||
</table>`;
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
<table class="table is-striped is-narrow">
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Files</th>
|
||||||
|
<th>Created At</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody id="table">
|
||||||
|
</tbody>
|
||||||
|
</table>`;
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
panel.page.appendChild(container);
|
||||||
return panel.verifyToken(panel.token);
|
var table = document.getElementById('table');
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
for(var item of response.data.albums){
|
||||||
|
|
||||||
if(json.success === false)
|
var tr = document.createElement('tr');
|
||||||
return swal("An error ocurred", json.description, "error");
|
tr.innerHTML = `
|
||||||
|
<tr>
|
||||||
panel.page.appendChild(container);
|
<th>${item.name}</th>
|
||||||
var table = document.getElementById('table');
|
<th>${item.files}</th>
|
||||||
|
<td>${item.date}</td>
|
||||||
for(var item of json.albums){
|
</tr>
|
||||||
|
`;
|
||||||
var tr = document.createElement('tr');
|
|
||||||
tr.innerHTML = `
|
|
||||||
<tr>
|
|
||||||
<th>${item.name}</th>
|
|
||||||
<th>${item.files}</th>
|
|
||||||
<td>${item.date}</td>
|
|
||||||
</tr>
|
|
||||||
`;
|
|
||||||
|
|
||||||
table.appendChild(tr);
|
|
||||||
}
|
|
||||||
|
|
||||||
document.getElementById('submitAlbum').addEventListener('click', function(){
|
|
||||||
panel.submitAlbum();
|
|
||||||
});
|
|
||||||
|
|
||||||
|
table.appendChild(tr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('GET', '/api/albums', true);
|
document.getElementById('submitAlbum').addEventListener('click', function(){
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
panel.submitAlbum();
|
||||||
xhr.setRequestHeader('extended', '');
|
});
|
||||||
xhr.send(null);
|
|
||||||
|
|
||||||
|
})
|
||||||
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.submitAlbum = function(){
|
panel.submitAlbum = function(){
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
axios.post('/api/albums', {
|
||||||
|
name: document.getElementById('albumName').value
|
||||||
|
})
|
||||||
|
.then(function (response) {
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
if(response.data.success === false){
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
swal("Woohoo!", "Album was added successfully", "success");
|
||||||
return panel.verifyToken(panel.token);
|
panel.getAlbumsSidebar();
|
||||||
|
panel.getAlbums();
|
||||||
|
return;
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
})
|
||||||
if(json.success === false)
|
.catch(function (error) {
|
||||||
return swal("An error ocurred", json.description, "error");
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
swal("Woohoo!", "Album was added successfully", "success");
|
});
|
||||||
panel.getAlbumsSidebar();
|
|
||||||
panel.getAlbums();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('POST', '/api/albums', true);
|
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
|
||||||
xhr.setRequestHeader('name', document.getElementById('albumName').value);
|
|
||||||
xhr.send(null);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.getAlbumsSidebar = function(){
|
panel.getAlbumsSidebar = function(){
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
axios.get('/api/albums/sidebar')
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
.then(function (response) {
|
||||||
|
if(response.data.success === false){
|
||||||
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
var albumsContainer = document.getElementById('albumsContainer');
|
||||||
return panel.verifyToken(panel.token);
|
albumsContainer.innerHTML = '';
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
if(response.data.albums === undefined) return;
|
||||||
if(json.success === false)
|
|
||||||
return swal("An error ocurred", json.description, "error");
|
|
||||||
|
|
||||||
var albumsContainer = document.getElementById('albumsContainer');
|
for(var album of response.data.albums){
|
||||||
albumsContainer.innerHTML = '';
|
|
||||||
|
|
||||||
if(json.albums === undefined) return;
|
li = document.createElement('li');
|
||||||
|
a = document.createElement('a');
|
||||||
|
a.id = album.id;
|
||||||
|
a.innerHTML = album.name;
|
||||||
|
|
||||||
for(var album of json.albums){
|
a.addEventListener('click', function(){
|
||||||
|
panel.getAlbum(this);
|
||||||
|
});
|
||||||
|
|
||||||
li = document.createElement('li');
|
li.appendChild(a);
|
||||||
a = document.createElement('a');
|
albumsContainer.appendChild(li);
|
||||||
a.id = album.id;
|
|
||||||
a.innerHTML = album.name;
|
|
||||||
|
|
||||||
a.addEventListener('click', function(){
|
|
||||||
panel.getAlbum(this);
|
|
||||||
});
|
|
||||||
|
|
||||||
li.appendChild(a);
|
|
||||||
albumsContainer.appendChild(li);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('GET', '/api/albums', true);
|
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
})
|
||||||
xhr.send(null);
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.getAlbum = function(item){
|
panel.getAlbum = function(item){
|
||||||
@ -266,99 +254,89 @@ panel.getAlbum = function(item){
|
|||||||
}
|
}
|
||||||
|
|
||||||
panel.changeTokens = function(){
|
panel.changeTokens = function(){
|
||||||
panel.page.innerHTML = '';
|
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
var container = document.createElement('div');
|
axios.get('/api/tokens')
|
||||||
container.className = "container";
|
.then(function (response) {
|
||||||
container.innerHTML = `
|
if(response.data.success === false){
|
||||||
<h2 class="subtitle">Manage your tokens</h2>
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
<label class="label">Client token:</label>
|
panel.page.innerHTML = '';
|
||||||
<p class="control has-addons">
|
var container = document.createElement('div');
|
||||||
<input id="clientToken" class="input is-expanded" type="text" placeholder="Your client token">
|
container.className = "container";
|
||||||
<a id="submitClientToken" class="button is-primary">Save</a>
|
container.innerHTML = `
|
||||||
</p>
|
<h2 class="subtitle">Manage your tokens</h2>
|
||||||
|
|
||||||
<label class="label">Admin token:</label>
|
<label class="label">Client token:</label>
|
||||||
<p class="control has-addons">
|
<p class="control has-addons">
|
||||||
<input id="adminToken" class="input is-expanded" type="text" placeholder="Your admin token">
|
<input id="clientToken" class="input is-expanded" type="text" placeholder="Your client token">
|
||||||
<a id="submitAdminToken" class="button is-primary">Save</a>
|
<a id="submitClientToken" class="button is-primary">Save</a>
|
||||||
</p>
|
</p>
|
||||||
`;
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
<label class="label">Admin token:</label>
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
<p class="control has-addons">
|
||||||
|
<input id="adminToken" class="input is-expanded" type="text" placeholder="Your admin token">
|
||||||
|
<a id="submitAdminToken" class="button is-primary">Save</a>
|
||||||
|
</p>
|
||||||
|
`;
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
panel.page.appendChild(container);
|
||||||
return panel.verifyToken(panel.token);
|
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
document.getElementById('clientToken').value = response.data.clientToken;
|
||||||
|
document.getElementById('adminToken').value = response.data.adminToken;
|
||||||
|
|
||||||
console.log(json);
|
document.getElementById('submitClientToken').addEventListener('click', function(){
|
||||||
|
panel.submitToken('client', document.getElementById('clientToken').value);
|
||||||
|
});
|
||||||
|
|
||||||
if(json.success === false)
|
document.getElementById('submitAdminToken').addEventListener('click', function(){
|
||||||
return swal("An error ocurred", json.description, "error");
|
panel.submitToken('admin', document.getElementById('adminToken').value);
|
||||||
|
});
|
||||||
|
|
||||||
panel.page.appendChild(container);
|
|
||||||
|
|
||||||
document.getElementById('clientToken').value = json.clientToken;
|
})
|
||||||
document.getElementById('adminToken').value = json.adminToken;
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
document.getElementById('submitClientToken').addEventListener('click', function(){
|
|
||||||
panel.submitToken('client', document.getElementById('clientToken').value);
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('submitAdminToken').addEventListener('click', function(){
|
|
||||||
panel.submitToken('admin', document.getElementById('adminToken').value);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('GET', '/api/tokens', true);
|
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
|
||||||
xhr.send(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
panel.submitToken = function(type, token){
|
panel.submitToken = function(type, token){
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
axios.post('/api/tokens/change', {
|
||||||
xhr.onreadystatechange = function() {
|
type: type,
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
token: token
|
||||||
|
})
|
||||||
|
.then(function (response) {
|
||||||
|
|
||||||
if(xhr.responseText === 'not-authorized')
|
if(response.data.success === false){
|
||||||
return panel.verifyToken(panel.token);
|
if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token);
|
||||||
|
else return swal("An error ocurred", response.data.description, "error");
|
||||||
|
}
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
swal({
|
||||||
|
title: "Woohoo!",
|
||||||
|
text: 'Your token was changed successfully.',
|
||||||
|
type: "success"
|
||||||
|
}, function(){
|
||||||
|
|
||||||
console.log(json);
|
if(type === 'client')
|
||||||
|
localStorage.token = token;
|
||||||
|
else if(type === 'admin')
|
||||||
|
localStorage.admintoken = token
|
||||||
|
|
||||||
if(json.success === false)
|
location.reload();
|
||||||
return swal("An error ocurred", json.description, "error");
|
|
||||||
|
|
||||||
swal({
|
})
|
||||||
title: "Woohoo!",
|
|
||||||
text: 'Your token was changed successfully.',
|
|
||||||
type: "success"
|
|
||||||
}, function(){
|
|
||||||
|
|
||||||
if(type === 'client')
|
})
|
||||||
localStorage.token = token;
|
.catch(function (error) {
|
||||||
else if(type === 'admin')
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
localStorage.admintoken = token
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
location.reload();
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
xhr.open('POST', '/api/tokens/change', true);
|
|
||||||
xhr.setRequestHeader('auth', panel.token);
|
|
||||||
xhr.setRequestHeader('type', type);
|
|
||||||
xhr.setRequestHeader('token', token);
|
|
||||||
xhr.send(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
|
@ -5,16 +5,18 @@ upload.token = localStorage.token;
|
|||||||
upload.maxFileSize;
|
upload.maxFileSize;
|
||||||
|
|
||||||
upload.checkIfPublic = function(){
|
upload.checkIfPublic = function(){
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
xhr.onreadystatechange = function() {
|
axios.get('/api/check')
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
.then(function (response) {
|
||||||
upload.isPublic = JSON.parse(xhr.responseText).private;
|
upload.isPublic = response.data.private;
|
||||||
upload.maxFileSize = JSON.parse(xhr.responseText).maxFileSize;
|
upload.maxFileSize = response.data.maxFileSize;
|
||||||
upload.preparePage();
|
upload.preparePage();
|
||||||
}
|
})
|
||||||
}
|
.catch(function (error) {
|
||||||
xhr.open('GET', '/api/check', true);
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
xhr.send(null);
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
upload.preparePage = function(){
|
upload.preparePage = function(){
|
||||||
@ -30,38 +32,37 @@ upload.preparePage = function(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
upload.verifyToken = function(token, reloadOnError = false){
|
upload.verifyToken = function(token, reloadOnError = false){
|
||||||
var xhr = new XMLHttpRequest();
|
|
||||||
|
|
||||||
xhr.onreadystatechange = function() {
|
axios.post('/api/tokens/verify', {
|
||||||
if (xhr.readyState == XMLHttpRequest.DONE) {
|
type: 'client',
|
||||||
|
token: token
|
||||||
|
})
|
||||||
|
.then(function (response) {
|
||||||
|
|
||||||
var json = JSON.parse(xhr.responseText);
|
if(response.data.success === false){
|
||||||
if(json.success === false){
|
swal({
|
||||||
|
title: "An error ocurred",
|
||||||
|
text: response.data.description,
|
||||||
|
type: "error"
|
||||||
|
}, function(){
|
||||||
|
if(reloadOnError){
|
||||||
|
localStorage.removeItem("token");
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
swal({
|
localStorage.token = token;
|
||||||
title: "An error ocurred",
|
upload.token = token;
|
||||||
text: json.description,
|
return upload.prepareUpload();
|
||||||
type: "error"
|
|
||||||
}, function(){
|
|
||||||
if(reloadOnError){
|
|
||||||
localStorage.removeItem("token");
|
|
||||||
location.reload();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return;
|
})
|
||||||
}
|
.catch(function (error) {
|
||||||
|
return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error");
|
||||||
|
console.log(error);
|
||||||
|
});
|
||||||
|
|
||||||
localStorage.token = token;
|
|
||||||
upload.token = token;
|
|
||||||
return upload.prepareUpload();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
xhr.open('GET', '/api/tokens/verify', true);
|
|
||||||
xhr.setRequestHeader('type', 'client');
|
|
||||||
xhr.setRequestHeader('token', token);
|
|
||||||
xhr.send(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
upload.prepareUpload = function(){
|
upload.prepareUpload = function(){
|
||||||
|
@ -13,11 +13,15 @@ routes.get ('/check', (req, res, next) => {
|
|||||||
|
|
||||||
routes.get ('/uploads', (req, res, next) => uploadController.list(req, res))
|
routes.get ('/uploads', (req, res, next) => uploadController.list(req, res))
|
||||||
routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, next))
|
routes.post ('/upload', (req, res, next) => uploadController.upload(req, res, next))
|
||||||
|
|
||||||
|
routes.get ('/album/:id', (req, res, next) => uploadController.list(req, res, next))
|
||||||
routes.get ('/albums', (req, res, next) => albumsController.list(req, res, next))
|
routes.get ('/albums', (req, res, next) => albumsController.list(req, res, next))
|
||||||
|
routes.get ('/albums/:sidebar', (req, res, next) => albumsController.list(req, res, next))
|
||||||
routes.post ('/albums', (req, res, next) => albumsController.create(req, res, next))
|
routes.post ('/albums', (req, res, next) => albumsController.create(req, res, next))
|
||||||
routes.get ('/albums/test', (req, res, next) => albumsController.test(req, res, next))
|
routes.get ('/albums/test', (req, res, next) => albumsController.test(req, res, next))
|
||||||
routes.get ('/tokens/verify', (req, res, next) => tokenController.verify(req, res))
|
|
||||||
routes.get ('/tokens', (req, res, next) => tokenController.list(req, res))
|
routes.get ('/tokens', (req, res, next) => tokenController.list(req, res))
|
||||||
|
routes.post ('/tokens/verify', (req, res, next) => tokenController.verify(req, res))
|
||||||
routes.post ('/tokens/change', (req, res, next) => tokenController.change(req, res))
|
routes.post ('/tokens/change', (req, res, next) => tokenController.change(req, res))
|
||||||
|
|
||||||
module.exports = routes
|
module.exports = routes
|
||||||
|
Loading…
Reference in New Issue
Block a user