Skip to content

Commit

Permalink
better handling of stale sessions when reconnecting later
Browse files Browse the repository at this point in the history
  • Loading branch information
tangentaudio committed Jan 11, 2023
1 parent facae57 commit edebc88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
45 changes: 24 additions & 21 deletions flask-backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,31 @@
def login_required(f):
@wraps(f)
def wrapper(*args, **kwargs):
auth_header=request.headers.get("Authorization")
if auth_header is not None and "Bearer " in auth_header:
junk, encoded_jwt=auth_header.split("Bearer ")
if encoded_jwt==None:
return abort(401)
try:
auth_header=request.headers.get("Authorization")
if auth_header is not None and "Bearer " in auth_header:
junk, encoded_jwt=auth_header.split("Bearer ")
if encoded_jwt==None:
return abort(401)
else:
sub = session["google_id"]
if sub is not None:
now = datetime.now().strftime('%s')
when = r.hget('session_time', sub)
if (when is not None):
age = int(now) - int(when)

if age > MAX_SESSION_LENGTH:
return abort(429)

r.hset('session_last', sub, now)


return f()
else:
sub = session["google_id"]
if sub is not None:
now = datetime.now().strftime('%s')
when = r.hget('session_time', sub)
if (when is not None):
age = int(now) - int(when)

if age > MAX_SESSION_LENGTH:
return abort(429)

r.hset('session_last', sub, now)


return f()
else:
return abort(401)
return abort(401)
except:
return abort(500)
return wrapper

def Generate_JWT(payload):
Expand Down
10 changes: 7 additions & 3 deletions react-frontend/src/components/Devices.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function useAreas() {
return [areas, setAreas];
}

const Devices = ( { setDevices, setAreas, setSelectedDevice, setSelectedArea }) => {
const Devices = ( { setDevices, setAreas, setSelectedDevice, setSelectedArea, handleLogout }) => {

useEffect(() => {
Axios.get(`${BACKEND_URL}/devices`, {
Expand Down Expand Up @@ -54,9 +54,13 @@ const Devices = ( { setDevices, setAreas, setSelectedDevice, setSelectedArea })
let selArea = getLocalItem("selectedArea", arealist[0]);
setSelectedArea(selArea);
})
.catch((err) => console.log(err));
.catch((err) => {
console.log(err);
handleLogout();

}, [setDevices, setAreas, setSelectedDevice, setSelectedArea] )
})

}, [setDevices, setAreas, setSelectedDevice, setSelectedArea, handleLogout] )

return null;
}
Expand Down
6 changes: 4 additions & 2 deletions react-frontend/src/components/Thumb.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CircularProgress from '@mui/material/CircularProgress';
import Typography from '@mui/material/Typography';
import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom';

function Thumb({ zoomable, smallThumb, id, name, imgWidth, imgHeight, interval, handleTimeout, onClick }) {
function Thumb({ zoomable, smallThumb, id, name, imgWidth, imgHeight, interval, handleTimeout, handleLogout, onClick }) {
const [thumb, setThumb] = useState(null);
const [reloading, setReloading] = useState(false);
const [zoomMode, setZoomMode] = useState(false);
Expand Down Expand Up @@ -62,6 +62,8 @@ function Thumb({ zoomable, smallThumb, id, name, imgWidth, imgHeight, interval,
.catch((err) => {
if (err.response && err.response.status === 429) {
handleTimeout();
} else {
handleLogout();
}
})
}
Expand Down Expand Up @@ -92,7 +94,7 @@ function Thumb({ zoomable, smallThumb, id, name, imgWidth, imgHeight, interval,
reqCopy.cancel();
clearInterval(tidRef.current);
}
}, [id, interval, imgWidth, imgHeight, zoomable, handleTimeout]);
}, [id, interval, imgWidth, imgHeight, zoomable, handleTimeout, handleLogout]);

var now = new Date().toLocaleString();

Expand Down

0 comments on commit edebc88

Please sign in to comment.