diff --git a/assets/js/store.js b/assets/js/store.js index 19c25bc..92ad7ca 100644 --- a/assets/js/store.js +++ b/assets/js/store.js @@ -1,5 +1,7 @@ import { LegoStore } from '../vendors/store.js' import { api } from '../js/core.js' +import page from "/assets/vendors/page.js" + const state = { user: {}, @@ -30,6 +32,7 @@ const state = { const actions = { async login(email) { const response = await api('/video/auth', { data: { email } }) + if(!response.ok) return console.error(response.data) const user = response.data localStorage.setItem('user', JSON.stringify(user)) this.setState({ user }) @@ -39,17 +42,18 @@ const actions = { async autoLogin() { try { - const email = JSON.parse(localStorage.getItem('user')).email + const email = JSON.parse(localStorage.getItem("user")).email const user = await this.actions.login(email) - return user + if(!user) this.actions.redirect("login") } catch(e) { console.error(e) + this.actions.redirect("login") return null } }, redirect(url) { - window.location.hash = url + page.redirect(url.startsWith('/') ? url : `/${url}`) } } diff --git a/bricks/x-login.html b/bricks/x-login.html index a015e01..1066fb7 100644 --- a/bricks/x-login.html +++ b/bricks/x-login.html @@ -3,8 +3,7 @@ async submit(event) { event.preventDefault() const email = event.target.email.value - const response = await api('/video/auth', { data: { email } }) - store.actions.login(response.data) + store.actions.login(email) } } diff --git a/config.js b/config.js index 7b1b6ca..0b57a1a 100644 --- a/config.js +++ b/config.js @@ -1,6 +1,7 @@ const config = { - apiUrl: 'http://localhost:3579' - //apiUrl: 'https://api.tci.anthropedia.org' + apiUrl: document.location.host.includes('localhost') + ? "http://localhost:3579" + : "https://api.tci.anthropedia.org" } export default config