Skip to content
Snippets Groups Projects
Commit 248d85bc authored by scmoritz's avatar scmoritz
Browse files

Changed exports and requires to import

parent adf03e45
No related branches found
No related tags found
1 merge request!16Change exports and requires to import
import { apiUrl } from './config';
import { log } from './log';
const m = require('mithril'); const m = require('mithril');
const Config = require('./config');
const log = require('./log');
const auth = { const auth = {
username: '', username: '',
...@@ -14,11 +15,11 @@ const auth = { ...@@ -14,11 +15,11 @@ const auth = {
this.reloadLocalStorage(); this.reloadLocalStorage();
return m.request({ return m.request({
method: 'POST', method: 'POST',
url: `${Config.api_url}/sessions`, url: `${apiUrl}/sessions`,
data: { username, password }, data: { username, password },
}).then((result) => { }).then((result) => {
const dt = new Date(); const dt = new Date();
log.log('logged in!'); log('logged in!');
this.token = result.token; this.token = result.token;
this.etag = result._etag; this.etag = result._etag;
this.id = result._id; this.id = result._id;
...@@ -39,13 +40,13 @@ const auth = { ...@@ -39,13 +40,13 @@ const auth = {
this.authenticated = false; this.authenticated = false;
return m.request({ return m.request({
method: 'DELETE', method: 'DELETE',
url: `${Config.api_url}/sessions/${this.id}`, url: `${apiUrl}/sessions/${this.id}`,
headers: { headers: {
Authorization: `Token ${this.token}`, Authorization: `Token ${this.token}`,
'If-Match': this.etag, 'If-Match': this.etag,
}, },
}).then(() => { }).then(() => {
log.log('logged out!'); log('logged out!');
this.token = ''; this.token = '';
this.authenticated = false; this.authenticated = false;
this.error = ''; this.error = '';
...@@ -64,23 +65,23 @@ const auth = { ...@@ -64,23 +65,23 @@ const auth = {
const dt = new Date(); const dt = new Date();
auth.reloadLocalStorage(); auth.reloadLocalStorage();
if (this.authenticated === true) { if (this.authenticated === true) {
log.log('no session found'); log('no session found');
m.route.set('/login'); m.route.set('/login');
return new Promise(() => { }); return new Promise(() => { });
} }
if (dt.getTime() > this.lastChecked + 5000) { if (dt.getTime() > this.lastChecked + 5000) {
return m.request({ return m.request({
method: 'GET', method: 'GET',
url: `${Config.api_url}/sessions/${this.token}`, url: `${apiUrl}/sessions/${this.token}`,
}).then((result) => { }).then((result) => {
const dt2 = new Date(); const dt2 = new Date();
log.log('session is still valid!'); log('session is still valid!');
this.authenticated = true; this.authenticated = true;
this.etag = result._etag; this.etag = result._etag;
this.lastChecked = dt2.getTime(); this.lastChecked = dt2.getTime();
}).catch((e) => { }).catch((e) => {
log.log('token is not valid'); log('token is not valid');
log.log(e); log(e);
this.authenticated = false; this.authenticated = false;
localStorage.removeItem('session'); localStorage.removeItem('session');
localStorage.removeItem('username'); localStorage.removeItem('username');
...@@ -92,7 +93,7 @@ const auth = { ...@@ -92,7 +93,7 @@ const auth = {
return new Promise(() => { }); return new Promise(() => { });
}, },
reloadLocalStorage() { reloadLocalStorage() {
log.log('checking stored session'); log('checking stored session');
if (localStorage.getItem('token') !== null) { if (localStorage.getItem('token') !== null) {
this.token = localStorage.token; this.token = localStorage.token;
this.id = localStorage.id; this.id = localStorage.id;
......
const Config = { export const apiUrl = 'https://amiv-api.ethz.ch';
api_url: 'https://amiv-api.ethz.ch', export const verbose = true;
verbose: true,
};
module.exports = Config;
const config = require('./config.js'); import { verbose } from './config';
const log = { export function log(message) {
log(message) { if (verbose === true) console.log(message);
if (config.verbose === true) console.log(message); }
},
};
module.exports = log; export function error(message) {
if (verbose === true) console.error(message);
}
import * as auth from '../models/auth';
const m = require('mithril'); const m = require('mithril');
const auth = require('../models/auth');
module.exports = { module.exports = {
oninit: auth.checkLogin, oninit: auth.checkLogin,
......
import * as auth from '../models/auth';
const m = require('mithril'); const m = require('mithril');
const Auth = require('../models/auth');
module.exports = { module.exports = {
username: '', username: '',
...@@ -10,11 +11,11 @@ module.exports = { ...@@ -10,11 +11,11 @@ module.exports = {
'form', { 'form', {
onsubmit: (e) => { onsubmit: (e) => {
e.preventDefault(); e.preventDefault();
Auth.login(this.username, this.password); auth.login(this.username, this.password);
}, },
}, },
m('h3', 'Login'), [ m('h3', 'Login'), [
m('p', Auth.error), m('p', auth.error),
m('input.input[type=text][placeholder=Username]', { m('input.input[type=text][placeholder=Username]', {
oninput: m.withAttr('value', (value) => { this.username = value; }), oninput: m.withAttr('value', (value) => { this.username = value; }),
value: this.username, value: this.username,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment