Redirect to / if session already exists
* also fix usage of stale isAuthenticated
This commit is contained in:
parent
d5b432a9d2
commit
0e49eb73ee
|
@ -7,6 +7,7 @@
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useSession } from '@/stores/session'
|
import { useSession } from '@/stores/session'
|
||||||
|
import { useRouter } from 'vue-router'
|
||||||
|
|
||||||
const qr = ref(null)
|
const qr = ref(null)
|
||||||
const lnurl = ref(null)
|
const lnurl = ref(null)
|
||||||
|
@ -17,17 +18,24 @@ const login = async () => {
|
||||||
lnurl.value = s.lnurl
|
lnurl.value = s.lnurl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const router = useRouter()
|
||||||
const session = useSession()
|
const session = useSession()
|
||||||
// wait until session is initialized
|
|
||||||
if (session.initialized && !session.isAuthenticated) {
|
await (async () => {
|
||||||
await login()
|
// redirect to / if session already exists
|
||||||
} else {
|
if (session.initialized) {
|
||||||
|
if (session.isAuthenticated) return router.push('/')
|
||||||
|
return login()
|
||||||
|
}
|
||||||
// else subscribe to changes
|
// else subscribe to changes
|
||||||
session.$subscribe(async (_, s) => {
|
return session.$subscribe(() => {
|
||||||
if (s.initialized && !s.isAuthenticated) {
|
if (session.initialized) {
|
||||||
await login()
|
// for some reason, computed property is only updated when accessing the store directly
|
||||||
|
// it is not updated inside the second argument
|
||||||
|
if (session.isAuthenticated) return router.push('/')
|
||||||
|
return login()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
})()
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in New Issue