|
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
|
|
|
|