This library is currently in beta. Use it with caution and report any issues you encounter.
@entespotify/react-oauth-client-components is a React library that simplifies the implementation of OAuth2 authentication in your React applications. It provides components and utilities to handle login, token management, and PKCE (Proof Key for Code Exchange).
Install the library using npm:
npm install @entespotify/react-oauth-client-componentsYou also need to have react as a peer dependency in your project:
npm install reactThe library exports the following components and utilities:
AuthProviderLoginPageCallbackPageAuthService
Wrap your application with the AuthProvider and provide the necessary configuration:
import React from 'react';
import { AuthProvider } from '@entespotify/react-oauth-client-components';
const config = {
clientId: 'your-client-id',
authorizationEndpoint: 'https://example.com/oauth/authorize',
tokenEndpoint: 'https://example.com/oauth/token',
redirectUri: 'https://your-app.com/callback',
scope: 'read write',
storage: 'localStorage', // or 'sessionStorage'
};
function App() {
return (
<AuthProvider config={config}>
<YourApp />
</AuthProvider>
);
}
export default App;Use the LoginPage component to provide a login interface:
import React from 'react';
import { LoginPage } from '@entespotify/react-oauth-client-components';
function Login() {
return <LoginPage label="Sign in with OAuth" />;
}
export default Login;Use the CallbackPage component to handle the OAuth2 redirect callback:
import React from 'react';
import { CallbackPage } from '@entespotify/react-oauth-client-components';
function Callback() {
return <CallbackPage onSuccessRedirect="/" />;
}
export default Callback;Use the useAuth hook to access authentication state and methods:
import React from 'react';
import { useAuth } from '@entespotify/react-oauth-client-components';
function Dashboard() {
const { token, isAuthenticated, logout } = useAuth();
if (!isAuthenticated) {
return <div>Please log in</div>;
}
return (
<div>
<h1>Welcome!</h1>
<p>Access Token: {token?.access_token}</p>
<button onClick={logout}>Logout</button>
</div>
);
}
export default Dashboard;- Props:
config: Configuration object with the following fields:clientId(string, required): OAuth2 client ID.authorizationEndpoint(string, required): URL for the authorization endpoint.tokenEndpoint(string, required): URL for the token endpoint.redirectUri(string, required): Redirect URI for your application.scope(string, optional): OAuth2 scope.storage(string, optional): Storage type (localStorageorsessionStorage).
- Props:
label(string, optional): Label for the login page.children(ReactNode, optional): Additional content to render.
- Props:
onSuccessRedirect(string, optional): URL to redirect to after successful login.
- Returns:
token: The current OAuth2 token.isAuthenticated: Boolean indicating if the user is authenticated.login: Function to initiate login.logout: Function to log out.service: Instance ofAuthService.
To build the library, run:
npm run buildTo clean the build artifacts:
npm run cleanThis project is licensed under the MIT License.
Since this is a beta version, your feedback is highly appreciated. Please report any issues or suggestions in the GitHub repository.