Skip to content

Commit 2e3bb08

Browse files
committed
Fixing auth0 integration
1 parent 1ec769b commit 2e3bb08

File tree

7 files changed

+52
-33
lines changed

7 files changed

+52
-33
lines changed

.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PORT=3000
2-
REACT_APP_API_URL=http://localhost:8080
2+
REACT_APP_API_URL=http://localhost:7080
33
REACT_APP_WEBSOCKET_URL=http://localhost:7070
44

55
# Auth0

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "frontend",
3-
"version": "6.0.0",
3+
"version": "6.1.0",
44
"description": "React frontend for the Cards 110",
55
"author": "Daithi Hearn",
66
"license": "MIT",
77
"private": true,
88
"dependencies": {
9-
"@auth0/auth0-react": "1.12.1",
9+
"@auth0/auth0-react": "2.0.1",
1010
"@coreui/coreui": "^4.2.6",
1111
"@coreui/icons": "^3.0.0",
1212
"@coreui/react": "^4.6.0",

public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"short_name": "Cards 110",
33
"name": "Cards 110",
4-
"version": "6.0.0",
4+
"version": "6.1.0",
55
"icons": [
66
{
77
"src": "./assets/favicon.png",

src/App.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ import Layout from "./pages/Layout/Layout"
3232
import ErrorPage from "./pages/Error/Error"
3333

3434
const AUTHO_DOMAIN = process.env.REACT_APP_AUTH0_DOMAIN as string
35-
const AUTH0_AUDIENCE = process.env.REACT_APP_AUTH0_AUDIENCE as string
3635
const AUTH0_CLIENT_ID = process.env.REACT_APP_AUTH0_CLIENT_ID as string
37-
const AUTH0_SCOPE = process.env.REACT_APP_AUTH0_SCOPE as string
3836

3937
const router = createBrowserRouter(
4038
createRoutesFromElements(
@@ -53,10 +51,7 @@ const App = () => {
5351
<SnackbarProvider maxSnack={3}>
5452
<Auth0Provider
5553
domain={AUTHO_DOMAIN}
56-
audience={AUTH0_AUDIENCE}
5754
clientId={AUTH0_CLIENT_ID}
58-
redirectUri={window.location.origin}
59-
scope={AUTH0_SCOPE}
6055
useRefreshTokens={true}>
6156
<MyProfileSync />
6257
<RouterProvider router={router} />

src/components/Header/NavBar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const NavBar = () => {
5555

5656
const myProfile = useAppSelector(getMyProfile)
5757

58-
const signOut = () => logout({ returnTo: window.location.origin })
58+
const signOut = () => logout()
5959

6060
if (!myProfile) {
6161
return null

src/components/MyProfile/MyProfileSync.tsx

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { useAppDispatch } from "caches/hooks"
77
import { useSnackbar } from "notistack"
88
import parseError from "utils/ErrorUtils"
99

10+
const AUTH0_AUDIENCE = process.env.REACT_APP_AUTH0_AUDIENCE as string
11+
const AUTH0_SCOPE = process.env.REACT_APP_AUTH0_SCOPE as string
12+
1013
const MyProfileSync: React.FC = () => {
1114
const dispatch = useAppDispatch()
1215
const { user, error, isAuthenticated, getAccessTokenSilently } = useAuth0()
@@ -16,31 +19,39 @@ const MyProfileSync: React.FC = () => {
1619
if (error) enqueueSnackbar(error.message, { variant: "error" })
1720
}, [error])
1821

22+
const updateProfile = async (name: string, picture: string) => {
23+
console.log(`DAITHI: Attempting to update profile: ${name}`)
24+
const accessToken = await getAccessTokenSilently({
25+
authorizationParams: {
26+
audience: AUTH0_AUDIENCE,
27+
redirect_uri: window.location.origin,
28+
scope: AUTH0_SCOPE,
29+
},
30+
})
31+
32+
console.log(`DAITHI: Got access token: ${accessToken}`)
33+
34+
dispatch(
35+
ProfileService.updateProfile(
36+
{
37+
name,
38+
picture,
39+
},
40+
accessToken,
41+
),
42+
).catch((e: Error) =>
43+
enqueueSnackbar(parseError(e), {
44+
variant: "error",
45+
}),
46+
)
47+
}
48+
1949
useEffect(() => {
50+
console.log(`DAITHI: isAuthenticated: ${isAuthenticated} user: ${user}`)
2051
if (isAuthenticated && user && user.name && user.picture) {
21-
getAccessTokenSilently()
22-
.then(accessToken =>
23-
dispatch(
24-
ProfileService.updateProfile(
25-
{
26-
name: user.name!,
27-
picture: user.picture!,
28-
},
29-
accessToken,
30-
),
31-
).catch((e: Error) =>
32-
enqueueSnackbar(parseError(e), {
33-
variant: "error",
34-
}),
35-
),
36-
)
37-
.catch((e: Error) =>
38-
enqueueSnackbar(parseError(e), {
39-
variant: "error",
40-
}),
41-
)
52+
updateProfile(user.name, user.picture)
4253
}
43-
}, [user, isAuthenticated])
54+
}, [user, isAuthenticated, getAccessTokenSilently])
4455

4556
return <></>
4657
}

src/pages/Layout/Layout.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,25 @@ import DefaultHeader from "components/Header/Header"
66
import { useEffect } from "react"
77
import Loading from "components/icons/Loading"
88

9+
const AUTH0_AUDIENCE = process.env.REACT_APP_AUTH0_AUDIENCE as string
10+
const AUTH0_SCOPE = process.env.REACT_APP_AUTH0_SCOPE as string
11+
912
const Layout = () => {
1013
const accessToken = useAppSelector(getAccessToken)
1114
const { isLoading, isAuthenticated, loginWithRedirect } = useAuth0()
1215

1316
useEffect(() => {
14-
if (!isLoading && !isAuthenticated) loginWithRedirect()
17+
console.log(
18+
`DAITHI: isLoading: ${isLoading} isAuthenticated: ${isAuthenticated}`,
19+
)
20+
if (!isLoading && !isAuthenticated)
21+
loginWithRedirect({
22+
authorizationParams: {
23+
audience: AUTH0_AUDIENCE,
24+
redirect_uri: window.location.origin,
25+
scope: AUTH0_SCOPE,
26+
},
27+
})
1528
}, [isLoading, isAuthenticated])
1629

1730
return (

0 commit comments

Comments
 (0)