update widget refresh token
This commit is contained in:
parent
0fe3f88a4a
commit
a658d7540d
@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { RefreshCw } from "lucide-react";
|
import { RefreshCw } from "lucide-react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
||||||
|
import { signIn, useSession } from "next-auth/react";
|
||||||
|
|
||||||
interface Message {
|
interface Message {
|
||||||
_id: string;
|
_id: string;
|
||||||
@ -26,6 +27,7 @@ export function Parole() {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [refreshing, setRefreshing] = useState(false);
|
const [refreshing, setRefreshing] = useState(false);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const { data: session, status } = useSession();
|
||||||
|
|
||||||
const fetchMessages = async (isRefresh = false) => {
|
const fetchMessages = async (isRefresh = false) => {
|
||||||
try {
|
try {
|
||||||
@ -33,11 +35,16 @@ export function Parole() {
|
|||||||
setRefreshing(true);
|
setRefreshing(true);
|
||||||
}
|
}
|
||||||
const response = await fetch('/api/rocket-chat/messages', {
|
const response = await fetch('/api/rocket-chat/messages', {
|
||||||
// Prevent caching
|
|
||||||
cache: 'no-store',
|
cache: 'no-store',
|
||||||
next: { revalidate: 0 },
|
next: { revalidate: 0 },
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (response.status === 401) {
|
||||||
|
// Handle authentication error
|
||||||
|
setError('Session expired. Please sign in again.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json();
|
const errorData = await response.json();
|
||||||
throw new Error(errorData.error || 'Failed to fetch messages');
|
throw new Error(errorData.error || 'Failed to fetch messages');
|
||||||
@ -53,7 +60,13 @@ export function Parole() {
|
|||||||
setError(null);
|
setError(null);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching messages:', err);
|
console.error('Error fetching messages:', err);
|
||||||
setError(err instanceof Error ? err.message : 'Failed to fetch messages');
|
const errorMessage = err instanceof Error ? err.message : 'Failed to fetch messages';
|
||||||
|
setError(errorMessage);
|
||||||
|
|
||||||
|
// Clear polling if we have an authentication error
|
||||||
|
if (errorMessage.includes('Session expired')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setRefreshing(false);
|
setRefreshing(false);
|
||||||
@ -61,11 +74,44 @@ export function Parole() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (status === 'authenticated') {
|
||||||
fetchMessages();
|
fetchMessages();
|
||||||
// Set up polling every 30 seconds
|
// Set up polling every 30 seconds
|
||||||
const interval = setInterval(() => fetchMessages(), 30000);
|
const interval = setInterval(() => fetchMessages(), 30000);
|
||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}, []);
|
}
|
||||||
|
}, [status]);
|
||||||
|
|
||||||
|
if (status === 'loading') {
|
||||||
|
return (
|
||||||
|
<Card className="bg-white/50 backdrop-blur-sm">
|
||||||
|
<CardContent className="p-6">
|
||||||
|
<p className="text-center text-muted-foreground">Loading...</p>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status === 'unauthenticated' || (error && error.includes('Session expired'))) {
|
||||||
|
return (
|
||||||
|
<Card className="bg-white/50 backdrop-blur-sm">
|
||||||
|
<CardContent className="p-6">
|
||||||
|
<div className="text-center">
|
||||||
|
<p className="text-muted-foreground mb-4">Please sign in to view messages</p>
|
||||||
|
<Button
|
||||||
|
onClick={(e) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
signIn('keycloak');
|
||||||
|
}}
|
||||||
|
variant="default"
|
||||||
|
>
|
||||||
|
Sign In
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card
|
<Card
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user