Skip to content

Commit d2aa274

Browse files
committed
* add Auth0 mocks for loginWithRedirect
1 parent fcc759f commit d2aa274

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bldrs",
3-
"version": "1.0.1280",
3+
"version": "1.0.1281",
44
"main": "src/index.jsx",
55
"license": "AGPL-3.0",
66
"homepage": "https://github.com/bldrs-ai/Share",

src/Auth0/Auth0ProviderProxy.jsx

+29-18
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,23 @@ export const Auth0Provider = ({children, onRedirectCallback, ...props}) => {
2626
})
2727

2828
const getAccessTokenSilently = (options) => {
29+
if (!state.isAuthenticated) {
30+
// Detailed response based on the options
31+
setState({
32+
isAuthenticated: true,
33+
user: mockGitHubUser,
34+
token: 'mock_access_token',
35+
})
36+
}
2937
return new Promise((resolve, reject) => {
30-
if (state.isAuthenticated) {
31-
if (options && options.detailedResponse) {
32-
// Detailed response based on the options
33-
const response = {
34-
access_token: 'mock_access_token',
35-
id_token: 'mock_id_token',
36-
expires_in: 3600, // Expiry in seconds
37-
token_type: 'Bearer',
38-
scope: 'openid profile email offline_access repo',
39-
}
40-
resolve(response)
41-
} else {
42-
// Default to returning a simple string access token
43-
resolve('mock_access_token')
44-
}
45-
} else {
46-
// eslint-disable-next-line prefer-promise-reject-errors
47-
reject({error: 'login_required'})
38+
const response = {
39+
access_token: 'mock_access_token',
40+
id_token: 'mock_id_token',
41+
expires_in: 3600, // Expiry in seconds
42+
token_type: 'Bearer',
43+
scope: 'openid profile email offline_access repo',
4844
}
45+
resolve(response)
4946
})
5047
}
5148

@@ -58,6 +55,19 @@ export const Auth0Provider = ({children, onRedirectCallback, ...props}) => {
5855
})
5956
}
6057

58+
// Simulate the login functionality
59+
const loginWithRedirect = () => {
60+
setState({
61+
isAuthenticated: true,
62+
user: mockGitHubUser,
63+
token: 'mock_access_token',
64+
})
65+
66+
// Simulate Auth0 redirect to /popup-callback
67+
window.location.href = '/popup-callback'
68+
}
69+
70+
6171
// Simulate the logout functionality
6272
const logout = () => {
6373
setState({
@@ -71,6 +81,7 @@ export const Auth0Provider = ({children, onRedirectCallback, ...props}) => {
7181
const providerValue = {
7282
...state,
7383
loginWithPopup,
84+
loginWithRedirect,
7485
logout,
7586
getAccessTokenSilently,
7687
}

src/Auth0/Auth0Proxy.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ function mockLoginWithPopup() {
5555
MockAuth0Context._currentValue.user = mockGitHubUser
5656
}
5757

58+
/**
59+
*
60+
*/
61+
function mockLoginWithRedirect() {
62+
MockAuth0Context._currentValue.isAuthenticated = true
63+
MockAuth0Context._currentValue.user = mockGitHubUser
64+
}
65+
5866
/**
5967
*
6068
*/
@@ -71,7 +79,7 @@ export const MockAuth0Context = React.createContext({
7179
getAccessTokenSilently: mockGetAccessTokenSilently,
7280
getAccessTokenWithPopup: () => 'mock_access_token',
7381
getIdTokenClaims: () => ({__raw: 'mock_id_token'}),
74-
loginWithRedirect: () => undefined,
82+
loginWithRedirect: mockLoginWithRedirect,
7583
loginWithPopup: mockLoginWithPopup,
7684
logout: mockLogout,
7785
handleRedirectCallback: () => ({appState: {}}),

src/Components/Auth/PopupAuth.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, {useEffect} from 'react'
2-
import {useAuth0} from '@auth0/auth0-react'
2+
import {useAuth0} from '../../Auth0/Auth0Proxy'
33

44

55
/**
@@ -18,7 +18,7 @@ function PopupAuth() {
1818
loginWithRedirect({
1919
redirectUri: `${window.location.origin }/popup-callback`,
2020
})
21-
}, [loginWithRedirect])
21+
})
2222

2323
return <div>Redirecting to Auth0…</div>
2424
}

src/Components/Profile/ProfileControl.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function ProfileControl() {
5050
}
5151
window.addEventListener('storage', handleStorageEvent)
5252
return () => window.removeEventListener('storage', handleStorageEvent)
53-
}, [getAccessTokenSilently])
53+
})
5454

5555
// Open a popup that will trigger Auth0 login
5656
const handleLogin = () => {

0 commit comments

Comments
 (0)