nuvira/util/auth.ts
2025-02-27 02:56:21 +01:00

21 lines
579 B
TypeScript

// util/auth.ts
import axios from 'axios';
import { useRouter } from 'vue-router'; // Import useRouter
export async function checkAuth(router: any) {
try {
const response = await axios.get('/api/auth', {
withCredentials: true, // Ensure cookies are sent with the request
});
if (!response.data.success) {
router.push('/login');
}
return response.data.success;
} catch (error) {
console.log("Not authenticated, redirecting to /login");
router.push('/login');
return false;
}
}