Skip to content

Commit

Permalink
fixed simple login
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyll committed Apr 19, 2024
1 parent 28fc3a1 commit 53b7ba3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
10 changes: 7 additions & 3 deletions assets/js/store.js
Original file line number Diff line number Diff line change
@@ -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: {},
Expand Down Expand Up @@ -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 })
Expand All @@ -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}`)
}
}

Expand Down
3 changes: 1 addition & 2 deletions bricks/x-login.html
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
</script>
Expand Down
5 changes: 3 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53b7ba3

Please sign in to comment.