After that, we will need to create an Application in the Project Portal.
Create an application in the Portal
Go to Applications on the left menu bar.
Click ⊕Add Application in the top tool bar.
Input the name of your application, e.g. "MyAwesomeApp".
Select Single Page Application as the application type
Click "Save" to create the application
Configure Authorize Redirect URI
The Redirect URI is a URL in you application where the user will be redirected to after login with Authgear. In this path, make a finish authentication call to complete the login process.
For this tutorial, add http://localhost:4000/auth-redirect to Authorize Redirect URIs.
Configure Post Logout Redirect URI
The Post Logout Redirect URI is the URL users will be redirected after they have logged out. The URL must be whitelisted.
For this tutorial, add http://localhost:4000/ to Post Logout Redirect URIs.
Save the configuration before next steps.
Step 1: Create a simple React project
Here are some recommended steps to scaffold a React project. You can skip this part if you are adding Authgear to an existing project. See #install-authgear-sdk-to-the-project in the next section.
Install basic project dependencies
Create the project folder and install the dependencies. We will use parcel as the build tool and the react-router-dom, react , and react-dom packages. Also, we will use TypeScript in this tutorial.
# Create a new folder for your projectmkdirmy-app# Move into the project directorycdmy-app# Create source foldermkdirsrc# Create a brand new package.json filenpminit-y# Install parcelnpminstall--save-dev--save-exactparcel# Install react, react-dom and react routernpminstall--save-exactreactreact-domreact-router-dom# Install TypeScript and related typesnpminstall--save-dev--save-exacttypescript@types/react@types/react-dom@types/react-router-dom
Add script for launching the app
In the package.json file, add these two lines to the script section
The Authgear container instance takes endpoint and clientID as parameters. They can be obtained from the application page created in Setup Application in Authgear.
It is recommend to render the app after configure() resolves. So by the time the app is rendered, Authgear is ready to use.
Runnpm startnow and you should see a page with "Hello World" and no error message in the console if Authgear SDK is configured successfully
Step 3: Implement the Context Provider
Since we want to reference the logged in state in anywhere of the app, let's put the state in a context provider with UserProvider.tsx in the /src/context folder.
In UserProvider.tsx, it will have a isLoggedIn boolean and a setIsLoggedIn function. The is LoggedIn boolean state can be auto updated using the onSessionStateChange callback. This callback can be stored in delegate which is in the local SDK container.
// src/context/UserProvider.tsximport React, { createContext, useEffect, useState, useMemo } from"react";import authgear from"@authgear/web";interfaceUserContextValue { isLoggedIn:boolean;}exportconstUserContext=createContext<UserContextValue>({ isLoggedIn:false,});interfaceUserContextProviderProps { children:React.ReactNode;}constUserContextProvider:React.FC<UserContextProviderProps> = ({ children,}) => {// By default the user is not logged inconst [isLoggedIn,setIsLoggedIn] =useState<boolean>(false);useEffect(() => {// When the sessionState changed, logged in state will also be changedauthgear.delegate = {onSessionStateChange: (container) => {// sessionState is now up to date// Value of sessionState can be "NO_SESSION" or "AUTHENTICATED"constsessionState=container.sessionState;if (sessionState ==="AUTHENTICATED") {setIsLoggedIn(true); } else {setIsLoggedIn(false); } }, }; }, [setIsLoggedIn]);constcontextValue=useMemo<UserContextValue>(() => {return { isLoggedIn, }; }, [isLoggedIn]);return ( <UserContext.Providervalue={contextValue}>{children}</UserContext.Provider> );};exportdefault UserContextProvider;
Step 4: Implement the Auth Redirect
Next, we will add an "AuthRedirect" page for handling the authentication result after the user have been authenticated by Authgear.
Create the AuthRedirect.tsx component file in the src/ folder.
Call the Authgear finishAuthentication() function in the Auth Redirect component to send a token back to Authgear server in exchange for access token and refresh token. Don't worry about the technical jargons, finishAuthentication() will do all the hard work for you and and save the authentication data.
When the authentication is finished, the isLoggedIn state from the UserContextProvider will automatic set to true. Finally, navigate back to root (/) which is our Home page.
Since in React 18, useEffect will be fired twice in development mode, we need to implement a cleanup function to stop it from firing twice. We will use an useRef Hook to stop the user token from being sent twice to the Authgear Endpoint.
Without a cleanup function, anuseEffectHook will be fired twice and hence finishAuthentication() will send the token back to Authgear Endpoint for two times, which the second one will result in "Invalid Token" error since the token can only be used once.
Step 5: Add Routes and Context Provider to the App
Next, we will add a "Home" page . Create a Home.tsx component file the src/ folder.
Then import Home and AuthRedirect as routes. And Import UserContextProvider and wrap the routes with it.
First we will import the Authgear dependency and the React Hook that we will use to Home.tsx. Then add the login button which will call startAuthentication(ConfigureOptions) through startLogin callback on click. This will redirect the user to the login page.
// src/Home.tsximport React, { useEffect, useState, useCallback, useContext } from'react';import authgear from'@authgear/web';constHome:React.FC= () => {conststartLogin=useCallback(() => { authgear.startAuthentication({ redirectURI:'http://localhost:4000/auth-redirect', prompt:'login' }).then( () => {// started authentication, user should be redirected to Authgear }, err => {// failed to start authentication } ); }, []);return ( <div> <h1>Home Page</h1> <div> <buttononClick={startLogin}>Login</button> </div> </div> );}exportdefault Home;
You can now run npm start and you will be redirected to the Authgear Login page when you click the Login button.
Step 7: Show the user information
The Authgear SDK helps you get the information of the logged in users easily.
In the last step, the user is successfully logged in so let's try to print the user ID (sub) of the user in the Home page.
In Home.tsx, we will add a simple Loading splash and a greeting message printing the Sub ID. We will add two conditional elements such that they are only shown when user is logged in. We can also change the login button to show only if the user is not logged in.
Make use of isLoggedIn from the UserContext to control the components on the page. Fetch the user info by fetchInfo() and access its sub property.
To access restricted resources on your backend application server, the HTTP requests should include the access token in their Authorization headers. The Web SDK provides a fetch function which automatically handle this, or you can get the token with authgear.accessToken.
Option 1: Using fetch function provided by Authgear SDK
Authgear SDK provides the fetch function for you to call your application server. This fetch function will include the Authorization header in your application request, and handle refresh access token automatically. The authgear.fetch implements fetch.
Option 2: Add the access token to the HTTP request header
You can get the access token through authgear.accessToken. Call refreshAccessTokenIfNeeded every time before using the access token, the function will check and make the network call only if the access token has expired. Include the access token into the Authorization header of the application request.
authgear.refreshAccessTokenIfNeeded().then(() => {// access token is ready to use// accessToken can be string or undefined// it will be empty if user is not logged in or session is invalidconstaccessToken=authgear.accessToken;// include Authorization header in your application requestconstheaders= { Authorization:`Bearer ${accessToken}` }; });