feat: redirect unauthenticated users from profile pages to home, fix auth timing on page refresh
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { i18n } from './i18n.js'
|
||||
import { auth } from './services/auth.js'
|
||||
import { setupGlobalErrorHandler } from './components/error-boundary.js'
|
||||
|
||||
async function initApp() {
|
||||
@@ -12,6 +13,9 @@ async function initApp() {
|
||||
|
||||
await i18n.init()
|
||||
|
||||
// Restore auth session before loading components
|
||||
await auth.tryRestoreSession()
|
||||
|
||||
await import('./components/app-shell.js')
|
||||
|
||||
document.getElementById('app').innerHTML = '<app-shell></app-shell>'
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { t, i18n } from '../../i18n.js'
|
||||
import { directus } from '../../services/directus.js'
|
||||
import { auth } from '../../services/auth.js'
|
||||
import '../listing-card.js'
|
||||
import '../skeleton-card.js'
|
||||
|
||||
@@ -12,13 +13,24 @@ class PageFavorites extends HTMLElement {
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
if (!auth.isLoggedIn()) {
|
||||
window.location.hash = '#/'
|
||||
return
|
||||
}
|
||||
|
||||
this.render()
|
||||
this.loadFavorites()
|
||||
this.unsubscribe = i18n.subscribe(() => this.render())
|
||||
this.authUnsubscribe = auth.subscribe(() => {
|
||||
if (!auth.isLoggedIn()) {
|
||||
window.location.hash = '#/'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
disconnectedCallback() {
|
||||
if (this.unsubscribe) this.unsubscribe()
|
||||
if (this.authUnsubscribe) this.authUnsubscribe()
|
||||
}
|
||||
|
||||
getFavoriteIds() {
|
||||
|
||||
@@ -13,27 +13,21 @@ class PageMessages extends HTMLElement {
|
||||
|
||||
connectedCallback() {
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
this.render()
|
||||
|
||||
if (this.isLoggedIn) {
|
||||
this.loadConversations()
|
||||
} else {
|
||||
this.loading = false
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
return
|
||||
}
|
||||
|
||||
this.render()
|
||||
this.loadConversations()
|
||||
|
||||
this.unsubscribe = i18n.subscribe(() => this.render())
|
||||
this.authUnsubscribe = auth.subscribe(() => {
|
||||
const wasLoggedIn = this.isLoggedIn
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
|
||||
if (!wasLoggedIn && this.isLoggedIn) {
|
||||
this.loading = true
|
||||
this.render()
|
||||
this.loadConversations()
|
||||
} else if (wasLoggedIn && !this.isLoggedIn) {
|
||||
this.conversations = []
|
||||
this.loading = false
|
||||
this.render()
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,27 +15,21 @@ class PageMyListings extends HTMLElement {
|
||||
|
||||
connectedCallback() {
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
this.render()
|
||||
|
||||
if (this.isLoggedIn) {
|
||||
this.loadMyListings()
|
||||
} else {
|
||||
this.loading = false
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
return
|
||||
}
|
||||
|
||||
this.render()
|
||||
this.loadMyListings()
|
||||
|
||||
this.unsubscribe = i18n.subscribe(() => this.render())
|
||||
this.authUnsubscribe = auth.subscribe(() => {
|
||||
const wasLoggedIn = this.isLoggedIn
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
|
||||
if (!wasLoggedIn && this.isLoggedIn) {
|
||||
this.loading = true
|
||||
this.render()
|
||||
this.loadMyListings()
|
||||
} else if (wasLoggedIn && !this.isLoggedIn) {
|
||||
this.listings = []
|
||||
this.loading = false
|
||||
this.render()
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -47,7 +41,7 @@ class PageMyListings extends HTMLElement {
|
||||
|
||||
async loadMyListings() {
|
||||
try {
|
||||
const user = auth.getUser()
|
||||
const user = await auth.getUser()
|
||||
if (!user) {
|
||||
this.loading = false
|
||||
this.updateContent()
|
||||
|
||||
@@ -9,6 +9,12 @@ class PageSettings extends HTMLElement {
|
||||
|
||||
connectedCallback() {
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
return
|
||||
}
|
||||
|
||||
this.render()
|
||||
this.setupEventListeners()
|
||||
|
||||
@@ -19,8 +25,10 @@ class PageSettings extends HTMLElement {
|
||||
|
||||
this.authUnsubscribe = auth.subscribe(() => {
|
||||
this.isLoggedIn = auth.isLoggedIn()
|
||||
this.render()
|
||||
this.setupEventListeners()
|
||||
|
||||
if (!this.isLoggedIn) {
|
||||
window.location.hash = '#/'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user