From 6152a57ffce95d4f7146aeb1a8327f148a69c63f Mon Sep 17 00:00:00 2001
From: Sandro Lutz <sandro.lutz@temparus.ch>
Date: Sun, 31 Dec 2017 21:02:06 +0100
Subject: [PATCH] Add Error 401 page when unauthorized

---
 src/views/errors.js       | 13 +++++++++++++
 src/views/studydocList.js |  4 ++++
 src/views/studydocNew.js  |  4 ++++
 3 files changed, 21 insertions(+)
 create mode 100644 src/views/errors.js

diff --git a/src/views/errors.js b/src/views/errors.js
new file mode 100644
index 00000000..8cd0124e
--- /dev/null
+++ b/src/views/errors.js
@@ -0,0 +1,13 @@
+const m = require('mithril');
+
+export class Error401 {
+  static view() {
+    return m('div', 'This page is only accessible for authenticated users. Please log in.');
+  }
+}
+
+export class Error404 {
+  static view() {
+    return m('div', 'This page does not exist.');
+  }
+}
diff --git a/src/views/studydocList.js b/src/views/studydocList.js
index eac34013..0dcf95a3 100644
--- a/src/views/studydocList.js
+++ b/src/views/studydocList.js
@@ -1,5 +1,7 @@
 import * as studydocs from '../models/studydocs';
 import { apiUrl } from '../models/config';
+import { isLoggedIn } from '../models/auth';
+import { Error401 } from './errors';
 
 const m = require('mithril');
 
@@ -18,6 +20,8 @@ export default class studydocList {
   }
 
   static view() {
+    if (!isLoggedIn()) return m(Error401);
+
     return m('div', [
       m('form', {
         onsubmit: (e) => {
diff --git a/src/views/studydocNew.js b/src/views/studydocNew.js
index 10abec5a..1b76b579 100644
--- a/src/views/studydocNew.js
+++ b/src/views/studydocNew.js
@@ -1,7 +1,9 @@
 // import Ajv from 'ajv';
 import * as studydocs from '../models/studydocs';
 // import { apiUrl } from '../models/config';
+import { isLoggedIn } from '../models/auth';
 import { log } from '../models/log';
+import { Error401 } from './errors';
 
 const m = require('mithril');
 
@@ -25,6 +27,8 @@ export default class studydocNew {
   }
 
   view() {
+    if (!isLoggedIn()) return m(Error401);
+
     return m('div', [
       m('form', {
         onsubmit: (e) => {
-- 
GitLab