Passed
Push — development ( 2f9cbb...cdc56b )
by Peter
09:24 queued 15s
created

frontend/src/pages/Github.tsx   A

Complexity

Total Complexity 7
Complexity/F 0

Size

Lines of Code 78
Function Count 0

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 45.71%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 78
ccs 16
cts 35
cp 0.4571
rs 10
c 0
b 0
f 0
wmc 7
mnd 7
bc 7
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 2
const Github: React.FC = () => {
14
15 1
    const [ searchParams, setsearchParams] = useSearchParams();
16 1
    const [isLoggedIn, setisLoggedIn] = useState(false);
17 1
    const dispatch = useDispatch();
18 1
    const navigate = useNavigate();
19 1
    const attemptedRef = useRef(false);  // Track if we've attempted auth
20
21 2
    const userRole = useSelector((state: RootState) => state.auth.role);
22
23 1
    useEffect(() => {
24
25 2
        if (isLoggedIn) {
26
            // navigate('/');
27 2
            if (userRole === 'admin') {
28
                navigate('/adminstartpage');
29 2
            } else if (userRole === 'user') {
30
                navigate('/customerstartpage');
31 2
            } else if (userRole) {
32
                console.error('Unexpected role:', userRole);
33
            }
34
            return;
35
        }
36
37 1
        const code = searchParams.get('code');
38 4
        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 2
                    if (response.data.user.roles.includes("admin")) {
51
                        console.log("admin")
52
                        dispatch(setRole("admin"));
53
                    } else {
54
                        dispatch(setRole("user"));
55
                    }
56
                }
57
                
58
            catch(error)
59
            {
60
                console.log(error);
61
            }
62
            
63
        }
64
        backendAuth();
65
66
    }, [searchParams, isLoggedIn]); 
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