@globus/react-auth-context
A simple React context for managing Globus-related authentication state, built on top of the @globus/sdk.
Installation
npm install --save @globus/sdk @globus/react-auth-context
Usage
The package includes a <Provider>
that can be configured with a client
, scopes
, and redirect
that will be used to configure an AuthorizationManager
instance for the context. The useGlobusAuth
hook can be used to access the authentication state and the AuthorizationManager
instance.
import React, { useEffect } from "react";
import { Provider, useGlobusAuth } from '@globus/react-auth-context';
const client = '645b6bfb-4195-4010-83f5-a71332bd4761';
const scopes = 'urn:globus:auth:scope:transfer.api.globus.org:all';
const redirect = '/';
const App = () => (
<Provider client={client} scopes={scopes} redirect={redirect}>
<ExampleComponent />
</Provider>
);
const ExampleComponent = () => {
const { isAuthenticated, authorization } = useGlobusAuth();
useEffect(() => {
async function attempt() {
if (!authorization || isAuthenticated) {
return;
}
await instance?.handleCodeRedirect({
shouldReplace: false,
});
}
attempt();
}, [authorization, authorization?.handleCodeRedirect, isAuthenticated]);
return (
<div>
{isAuthenticated ? (
<button onClick={async () => await auth.authorization?.revoke()}>Logout</button>
) : (
<button onClick={async () => await auth.authorization?.login()}>Login</button>
)}
</div>
);
};