frontend/src/pages/Github.tsx   A
last analyzed

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 78
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 43.24%

Importance

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