Passed
Push — development ( e131c7...d0a4d7 )
by Peter
05:27 queued 13s
created

frontend/src/pages/Github.tsx   A

Complexity

Total Complexity 6
Complexity/F 0

Size

Lines of Code 72
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 6
mnd 6
bc 6
fnc 0
bpm 0
cpm 0
noi 0
1
import React, { useState, useEffect, useRef } from 'react';
2
import Spinner from '../components/Spinner';
3
import { useDispatch, useSelector } from 'react-redux';
4
import { RootState, AppDispatch } from '../redux/store/store';
5
import { setLoggedInOut, setCurrentUser, setToken, setRole } from '../redux/slices/authLogin';
6
// import userRole from './HomePage';
7
8
import { useSearchParams, useNavigate } from "react-router-dom";
9
import { API_URL } from '../helpers/config';
10
import axios from 'axios';
11
// import HomePage from './HomePage';
12
13
const Github: React.FC = () => {
14
15
    const [ searchParams, setsearchParams] = useSearchParams();
16
    const [isLoggedIn, setisLoggedIn] = useState(false);
17
    const dispatch = useDispatch();
18
    const navigate = useNavigate();
19
    const attemptedRef = useRef(false);  // Track if we've attempted auth
20
21
    const userRole = useSelector((state: RootState) => state.auth.role);
22
23
    useEffect(() => {
24
25
        if (isLoggedIn) { // Only in component (not from localStorage)
26
            // navigate('/');
27
            if (userRole === 'admin') {
28
                navigate('/adminstartpage');
29
            } else if (userRole === 'customer') {
30
                navigate('/customerstartpage');
31
            } else if (userRole) {
32
                console.error('Unexpected role:', userRole);
33
            }
34
            return;
35
        }
36
37
        const code = searchParams.get('code');
38
        if (!code || attemptedRef.current) return;  // Skip if no code or already attempted
39
40
        const backendAuth = async() => {
41
            try {
42
                    attemptedRef.current = true;  // Mark as attempted
43
                    const codeObject = Object.fromEntries(searchParams);
44
                    const response = await axios.post(`${API_URL}/auth/token`, codeObject);
45
                    console.log(response);
46
                    dispatch(setToken(response.data.access_token));
47
                    dispatch(setCurrentUser(response.data.user.username));
48
                    dispatch(setLoggedInOut(true));
49
                    setisLoggedIn(true);
50
                }
51
                
52
            catch(error)
53
            {
54
                console.log(error);
55
            }
56
            
57
        }
58
        backendAuth();
59
60
    }, [searchParams, isLoggedIn]); 
61
62
    return (
63
        <div className="flex flex-col items-center justify-center h-screen" data-testid="github-test">
64
            <Spinner spinnerColor='red'/>
65
            Setting credentials 
66
            {searchParams.get('searchParams')}
67
        </div>
68
    );
69
};
70
71
export default Github;
72