-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also mock sign out on localhost. For #400.
- Loading branch information
Showing
5 changed files
with
88 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
// Copyright 2022, University of Colorado Boulder | ||
|
||
/* eslint-disable */ | ||
|
||
/** | ||
* Create a component with a sign-out link. | ||
* | ||
* @author <liammulh@gmail.com> | ||
*/ | ||
|
||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
import signOut from '../js/signOut.js'; | ||
|
||
/** | ||
* Return a re-usable sign-out link. (Easier to have the component than typing out the whole URL every time | ||
* you need it.) | ||
* | ||
* @returns {JSX.Element} | ||
* Return a re-usable sign-out button. | ||
*/ | ||
const SignOutLink = () => { | ||
return <a href={`${window.location.origin}/en/sign-out`}>Sign Out</a>; | ||
const [ isSending, setIsSending ] = useState( false ); | ||
const isMounted = useRef( true ); | ||
|
||
// Set isMounted to false when we unmount the component. | ||
useEffect( () => { | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
} ); | ||
|
||
const sendRequest = useCallback( async () => { | ||
if ( isSending ) { | ||
return; | ||
} | ||
else { | ||
setIsSending( true ); | ||
await signOut(); | ||
if ( isMounted.current ) { | ||
setIsSending( false ); | ||
} | ||
} | ||
}, [ isSending ] ); | ||
|
||
return ( | ||
<button disabled={isSending} onClick={ sendRequest }>Sign Out</button> | ||
); | ||
}; | ||
|
||
export default SignOutLink; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2023, University of Colorado Boulder | ||
|
||
/** | ||
* Sign the user out. | ||
* | ||
* @author Liam Mulhall <liammulh@gmail.com> | ||
*/ | ||
|
||
import axios from 'axios'; | ||
import publicConfig from '../../common/publicConfig.js'; | ||
import alertErrorMessage from './alertErrorMessage.js'; | ||
|
||
/** | ||
* Send request to PhET server to sign the user out. | ||
* | ||
* @returns {Promise<void>} | ||
*/ | ||
const signOut = async () => { | ||
try { | ||
const signOutRes = await axios( { | ||
url: publicConfig.SIGN_OUT_URL, | ||
method: 'get', | ||
withCredentials: true | ||
} ); | ||
|
||
// If the sign-out request was successful, redirect the user to the home page. | ||
if ( signOutRes.status >= 200 && signOutRes.status < 300 ) { | ||
window.location.href = window.location.origin; | ||
} | ||
} | ||
catch( e ) { | ||
alertErrorMessage( e ); | ||
} | ||
}; | ||
|
||
export default signOut; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2023, University of Colorado Boulder | ||
|
||
/** | ||
* Mock sign out function for local testing. | ||
* | ||
* @author Liam Mulhall <liammulh@gmail.com> | ||
*/ | ||
|
||
import logger from '../logger.js'; | ||
|
||
const mockSignOut = ( req, res ) => { | ||
logger.info( 'mocking sign out' ); | ||
res.status( 200 ).send(); | ||
}; | ||
|
||
export default mockSignOut; |