aboutsummaryrefslogtreecommitdiff
path: root/modern/src/SocketController.js
diff options
context:
space:
mode:
Diffstat (limited to 'modern/src/SocketController.js')
-rw-r--r--modern/src/SocketController.js35
1 files changed, 24 insertions, 11 deletions
diff --git a/modern/src/SocketController.js b/modern/src/SocketController.js
index 13ff0a9d..94be52a6 100644
--- a/modern/src/SocketController.js
+++ b/modern/src/SocketController.js
@@ -1,7 +1,8 @@
import { useEffect } from 'react';
-import { useDispatch } from 'react-redux';
+import { useDispatch, useSelector } from 'react-redux';
import { connect } from 'react-redux';
-import { positionsActions, devicesActions } from './store';
+import { positionsActions, devicesActions, sessionActions } from './store';
+import { useHistory } from 'react-router-dom';
const displayNotifications = events => {
if ("Notification" in window) {
@@ -22,6 +23,8 @@ const displayNotifications = events => {
const SocketController = () => {
const dispatch = useDispatch();
+ const history = useHistory();
+ const authenticated = useSelector(state => state.session.authenticated);
const connectSocket = () => {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
@@ -46,15 +49,25 @@ const SocketController = () => {
}
useEffect(() => {
- fetch('/api/devices').then(response => {
- if (response.ok) {
- response.json().then(devices => {
- dispatch(devicesActions.update(devices));
- });
- }
- connectSocket();
- });
- }, []);
+ if (authenticated) {
+ fetch('/api/devices').then(response => {
+ if (response.ok) {
+ response.json().then(devices => {
+ dispatch(devicesActions.update(devices));
+ });
+ }
+ connectSocket();
+ });
+ } else {
+ fetch('/api/session').then(response => {
+ if (response.ok) {
+ dispatch(sessionActions.authenticated(true));
+ } else {
+ history.push('/login');
+ }
+ });
+ }
+ }, [authenticated]);
return null;
}