detect logged out state and redirect

This commit is contained in:
Pierre Balzack 2023-06-27 17:50:02 -07:00
parent ce0c1ad75f
commit ef0f1a2997
4 changed files with 14 additions and 1 deletions

View File

@ -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);

View File

@ -128,7 +128,6 @@ export function useProfile() {
const actions = {
logout: async () => {
await app.actions.logout();
navigate('/');
},
remove: async () => {
await app.actions.remove();

View File

@ -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
}

View File

@ -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) {