diff --git a/backend/db.py b/backend/db.py index 8d7a5be..fed13b5 100644 --- a/backend/db.py +++ b/backend/db.py @@ -106,8 +106,15 @@ def migrate_db(current_db_version: int) -> None: BEGIN TRANSACTION; PRAGMA defer_foreign_keys = ON; + CREATE TEMPORARY TABLE temp_reminder_services( + reminder_id, + static_reminder_id, + template_id, + notification_service_id + ); + -- Reminders - INSERT INTO reminder_services(reminder_id, notification_service_id) + INSERT INTO temp_reminder_services(reminder_id, notification_service_id) SELECT id, notification_service FROM reminders; @@ -132,31 +139,9 @@ def migrate_db(current_db_version: int) -> None: ); INSERT INTO reminders SELECT * FROM temp_reminders; - - -- Static reminders - INSERT INTO reminder_services(static_reminder_id, notification_service_id) - SELECT id, notification_service - FROM static_reminders; - - CREATE TEMPORARY TABLE temp_static_reminders AS - SELECT id, user_id, title, text, color - FROM static_reminders; - DROP TABLE static_reminders; - CREATE TABLE static_reminders( - id INTEGER PRIMARY KEY, - user_id INTEGER NOT NULL, - title VARCHAR(255) NOT NULL, - text TEXT, - - color VARCHAR(7), - - FOREIGN KEY (user_id) REFERENCES users(id) - ); - INSERT INTO static_reminders - SELECT * FROM temp_static_reminders; -- Templates - INSERT INTO reminder_services(template_id, notification_service_id) + INSERT INTO temp_reminder_services(template_id, notification_service_id) SELECT id, notification_service FROM templates; @@ -177,6 +162,9 @@ def migrate_db(current_db_version: int) -> None: INSERT INTO templates SELECT * FROM temp_templates; + INSERT INTO reminder_services + SELECT * FROM temp_reminder_services; + COMMIT; """) current_db_version = 5 diff --git a/frontend/static/js/general.js b/frontend/static/js/general.js index 2481954..32ffd2e 100644 --- a/frontend/static/js/general.js +++ b/frontend/static/js/general.js @@ -47,7 +47,7 @@ function logout() { 'method': 'POST' }) .then(response => { - sessionStorage.removeItem('api_key'); + localStorage.removeItem('MIND_api_key'); window.location.href = `${url_prefix}/`; }); }; @@ -55,7 +55,7 @@ function logout() { // code run on load const url_prefix = document.getElementById('url_prefix').dataset.value; -const api_key = sessionStorage.getItem('api_key'); +const api_key = localStorage.getItem('MIND_api_key'); if (api_key === null) { window.location.href = `${url_prefix}/`; }; diff --git a/frontend/static/js/login.js b/frontend/static/js/login.js index 9e1bb09..2adae30 100644 --- a/frontend/static/js/login.js +++ b/frontend/static/js/login.js @@ -44,7 +44,7 @@ function login(data=null) { return response.json(); }) .then(json => { - sessionStorage.setItem('api_key', json.result.api_key); + localStorage.setItem('MIND_api_key', json.result.api_key); window.location.href = `${url_prefix}/reminders`; }) .catch(e => { @@ -92,10 +92,26 @@ function create() { }); }; +function checkLogin() { + fetch(`${url_prefix}/api/auth/status?api_key=${localStorage.getItem('MIND_api_key')}`) + .then(response => { + if (!response.ok) return Promise.reject(response.status); + window.location.href = '/reminders'; + }) + .catch(e => { + if (e === 401) + console.log('API key expired') + else + console.log(e); + }); +}; + // code run on load const url_prefix = document.getElementById('url_prefix').dataset.value; +checkLogin(); + document.getElementById('login-form').setAttribute('action', 'javascript:login();'); document.getElementById('create-form').setAttribute('action', 'javascript:create();'); document.querySelectorAll('.switch-button').forEach(e => e.addEventListener('click', e => toggleWindow()));