Browse Source

check in the initial page if the token is valid

Sven Czarnian 2 years ago
parent
commit
47c09f2fdd
1 changed files with 20 additions and 4 deletions
  1. 20 4
      src/components/login.tsx

+ 20 - 4
src/components/login.tsx

@@ -1,11 +1,27 @@
-import React, { useEffect } from 'react';
+import React, { useEffect, useState } from 'react';
 import { Button } from 'primereact/button';
 import { Card } from 'primereact/card';
-import { Configuration, Session } from '../services';
+import { useNavigate } from 'react-router-dom';
+import { Auth, Configuration, Session } from '../services';
 
 export const Login: React.FC = () => {
-  // reset every old token
-  useEffect(() => Session.reset(), []);
+  const [loading, setLoading] = useState<boolean>(true);
+  const navigate = useNavigate();
+
+  // check if the known bearer token is still valid
+  useEffect(() => {
+    Auth.tokenIsValid().then((valid) => {
+      if (!valid) {
+        Session.reset();
+        setLoading(false);
+      } else {
+        navigate('/overview');
+      }
+    });
+  // eslint-disable-next-line react-hooks/exhaustive-deps
+  }, []);
+
+  if (loading) return <></>;
 
   const redirectToVatsim = () => {
     const url = [