This project was bootstrapped with Create React App.
There are more than one way to approach integrating OAuth2 Popup Flow with React and React-Router. The following is one example and assumes you know how react-router works.
git clone https://github.com/ricokahler/oauth2-popup-flow.git
cd oauth2-popup-flow/examples/react-example
npm install
npm start
npm install --save oauth2-popup-flow
- User clicks "Login" button. This will open the authorization page/login page in a new tab.
- User enters their login info in the authorization page.
- Authorization server redirect the user's browser (still in that new tab) to the configured redirect/callback URL including an encoded token in the
#
of the redirect URL (e.g.http://localhost:8080/redirect#access_token=SOME.TOKEN.HERE
) - Based on the
/redirect
route, front-end will handle the redirect and try to parse the token out of the URL. If this is successful, the new tab will close. - Original tab will receive tokens and normal app flow continues.
App
is the first component rendered and includes a commonAppBar
andreact-router
's<Switch>
component.- The
App
supports 3 routes:/authenticated
which will only render when the user is logged in./unauthenticated
which will render when the user is not logged in./redirect
which will render to handle the redirect/callback the authorization server redirect the user back to.
- The
App
component add event listeners to theauth
singleton for bothlogin
andlogout
events. The handlers on those events callhistory.push
orhistory.replace
to either push or redirect the user to the correct routes (/authenticated
and/unauthenticated
respectively). - The
Redirect
component/route callsauth.handleRedirect()
to handle the redirect and cause the calling window to close the tab.
The key files to look at are:
- auth.js–creates the auth singleton
- App.js–manages top-level routing
- AuthenticatedRoute.js–the authenticated route
- UnauthenticatedRoute.js–the unauthenticated route/login call to action
- Redirect.js–handles the redirect from the authorization server.
Enjoy!