|
@@ -1,6 +1,7 @@
|
|
|
import { defineStore } from "pinia";
|
|
import { defineStore } from "pinia";
|
|
|
import { ref } from "vue";
|
|
import { ref } from "vue";
|
|
|
import { getCurrentUser } from "@/lib/api";
|
|
import { getCurrentUser } from "@/lib/api";
|
|
|
|
|
+import { toast } from "vue-sonner";
|
|
|
|
|
|
|
|
export const useAuthStore = defineStore("auth", () => {
|
|
export const useAuthStore = defineStore("auth", () => {
|
|
|
const user = ref<any>(null);
|
|
const user = ref<any>(null);
|
|
@@ -65,7 +66,7 @@ export const useAuthStore = defineStore("auth", () => {
|
|
|
const token = localStorage.getItem("token");
|
|
const token = localStorage.getItem("token");
|
|
|
if (!token || wsAuthFailed.value) return;
|
|
if (!token || wsAuthFailed.value) return;
|
|
|
|
|
|
|
|
- globalWs = new WebSocket(`${WS_BASE_URL}/api/auth/ws/global?token=${encodeURIComponent(token)}`);
|
|
|
|
|
|
|
+ globalWs = new WebSocket(`${WS_BASE_URL}/auth/ws/global?token=${encodeURIComponent(token)}`);
|
|
|
|
|
|
|
|
globalWs.onopen = () => {
|
|
globalWs.onopen = () => {
|
|
|
// Send ping every 25 seconds to keep connection alive and update online status in Redis
|
|
// Send ping every 25 seconds to keep connection alive and update online status in Redis
|
|
@@ -84,6 +85,9 @@ export const useAuthStore = defineStore("auth", () => {
|
|
|
playNotificationSound();
|
|
playNotificationSound();
|
|
|
}
|
|
}
|
|
|
unreadMessagesCount.value = msg.count;
|
|
unreadMessagesCount.value = msg.count;
|
|
|
|
|
+ } else if (msg.type === "account_suspended") {
|
|
|
|
|
+ toast.error("Your account has been suspended by an administrator.", { duration: 10000 });
|
|
|
|
|
+ logout();
|
|
|
}
|
|
}
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
console.error("WS Parse error", e);
|
|
console.error("WS Parse error", e);
|
|
@@ -93,11 +97,14 @@ export const useAuthStore = defineStore("auth", () => {
|
|
|
globalWs.onclose = (event) => {
|
|
globalWs.onclose = (event) => {
|
|
|
if (pingInterval) clearInterval(pingInterval);
|
|
if (pingInterval) clearInterval(pingInterval);
|
|
|
|
|
|
|
|
- // 4001 is our custom code for Auth failure
|
|
|
|
|
- // 1008 is Policy Violation (often used for Auth fail in generic WS)
|
|
|
|
|
- if (event.code === 4001 || event.code === 1008) {
|
|
|
|
|
- console.warn("WS Authentication failed (403/401). Stopping reconnection until next login.");
|
|
|
|
|
|
|
+ // 4001: Auth fail, 4003: Suspended, 1008: Policy
|
|
|
|
|
+ if (event.code === 4001 || event.code === 1008 || event.code === 4003) {
|
|
|
|
|
+ console.warn(`WS Connection terminated (code ${event.code}).`);
|
|
|
wsAuthFailed.value = true;
|
|
wsAuthFailed.value = true;
|
|
|
|
|
+ if (event.code === 4003) {
|
|
|
|
|
+ user.value = null; // Instant clear
|
|
|
|
|
+ localStorage.removeItem("token");
|
|
|
|
|
+ }
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|