From ef0f1a29975f073454803d53e03d93c037f5bb28 Mon Sep 17 00:00:00 2001 From: Pierre Balzack Date: Tue, 27 Jun 2023 17:50:02 -0700 Subject: [PATCH] detect logged out state and redirect --- app/mobile/src/context/useAppContext.hook.js | 9 +++++++++ app/mobile/src/session/profile/useProfile.hook.js | 1 - net/server/internal/api_status.go | 1 + net/web/src/context/useAppContext.hook.js | 4 ++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/mobile/src/context/useAppContext.hook.js b/app/mobile/src/context/useAppContext.hook.js index a5109c15..e0588448 100644 --- a/app/mobile/src/context/useAppContext.hook.js +++ b/app/mobile/src/context/useAppContext.hook.js @@ -1,4 +1,5 @@ import { useEffect, useState, useRef, useContext } from 'react'; +import { useNavigate } from 'react-router-dom'; import { Alert } from 'react-native'; import { setLogin } from 'api/setLogin'; import { clearLogin } from 'api/clearLogin'; @@ -24,6 +25,9 @@ export function useAppContext() { adminToken: null, version: getVersion(), }); + + const navigate = useNavigate(); + const store = useContext(StoreContext); const account = useContext(AccountContext); const profile = useContext(ProfileContext); @@ -164,6 +168,7 @@ export function useAppContext() { await store.actions.clearSession(); await store.actions.clearFirstRun(); updateState({ loggingOut: false }); + navigate('/'); }, remove: async () => { if (!access.current) { @@ -179,6 +184,10 @@ export function useAppContext() { const setWebsocket = (session) => { ws.current = createWebsocket(`wss://${session.server}/status?mode=ring`); ws.current.onmessage = (ev) => { + if (ev.data == '') { + actions.logout(); + return; + } try { delay.current = 0; let activity = JSON.parse(ev.data); diff --git a/app/mobile/src/session/profile/useProfile.hook.js b/app/mobile/src/session/profile/useProfile.hook.js index 18a4a6a7..449b25c5 100644 --- a/app/mobile/src/session/profile/useProfile.hook.js +++ b/app/mobile/src/session/profile/useProfile.hook.js @@ -128,7 +128,6 @@ export function useProfile() { const actions = { logout: async () => { await app.actions.logout(); - navigate('/'); }, remove: async () => { await app.actions.remove(); diff --git a/net/server/internal/api_status.go b/net/server/internal/api_status.go index 6b2c167a..5dcf0301 100644 --- a/net/server/internal/api_status.go +++ b/net/server/internal/api_status.go @@ -57,6 +57,7 @@ func Status(w http.ResponseWriter, r *http.Request) { // retrieve reference account var session store.Session if err := store.DB.Preload("Account").Where("account_id = ? AND token = ?", target, access).First(&session).Error; err != nil { + conn.WriteMessage(websocket.TextMessage, []byte("")) ErrMsg(err) return } diff --git a/net/web/src/context/useAppContext.hook.js b/net/web/src/context/useAppContext.hook.js index 7b5bbfee..7284cc83 100644 --- a/net/web/src/context/useAppContext.hook.js +++ b/net/web/src/context/useAppContext.hook.js @@ -175,6 +175,10 @@ export function useAppContext(websocket) { ws.current = createWebsocket(protocol + window.location.host + "/status?mode=ring"); ws.current.onmessage = (ev) => { try { + if (ev.data == '') { + appLogout(false); + return; + } let activity = JSON.parse(ev.data); updateState({ status: 'connected' }); if (activity.revision) {