feat: Enhance logout functionality and routing in authentication system

- Integrate `useRouter` in `UserProfile.vue` and `useAuth.ts` to manage navigation after logout.
- Update `handleLogout` in `Navbar.vue` to reflect that redirection is now handled within the `auth.logout()` function.
This commit is contained in:
padreug 2025-07-30 00:13:44 +02:00
parent 31fe244089
commit 6e653d584c
3 changed files with 7 additions and 0 deletions

View file

@ -1,5 +1,6 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { Button } from '@/components/ui/button' import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Badge } from '@/components/ui/badge' import { Badge } from '@/components/ui/badge'
@ -7,6 +8,7 @@ import { User, LogOut, Settings } from 'lucide-vue-next'
import { auth } from '@/composables/useAuth' import { auth } from '@/composables/useAuth'
import { toast } from 'vue-sonner' import { toast } from 'vue-sonner'
const router = useRouter()
const userDisplay = computed(() => auth.userDisplay.value) const userDisplay = computed(() => auth.userDisplay.value)
async function handleLogout() { async function handleLogout() {

View file

@ -43,6 +43,7 @@ const handleLogout = async () => {
try { try {
await auth.logout() await auth.logout()
isOpen.value = false isOpen.value = false
// The redirect is now handled in the auth.logout() function
} catch (error) { } catch (error) {
console.error('Logout failed:', error) console.error('Logout failed:', error)
} }

View file

@ -1,4 +1,5 @@
import { ref, computed } from 'vue' import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { lnbitsAPI, type User, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits' import { lnbitsAPI, type User, type LoginCredentials, type RegisterData } from '@/lib/api/lnbits'
const currentUser = ref<User | null>(null) const currentUser = ref<User | null>(null)
@ -6,6 +7,7 @@ const isLoading = ref(false)
const error = ref<string | null>(null) const error = ref<string | null>(null)
export function useAuth() { export function useAuth() {
const router = useRouter()
const isAuthenticated = computed(() => !!currentUser.value) const isAuthenticated = computed(() => !!currentUser.value)
/** /**
@ -82,6 +84,8 @@ export function useAuth() {
} finally { } finally {
currentUser.value = null currentUser.value = null
error.value = null error.value = null
// Redirect to login page after logout
router.push('/login')
} }
} }