This project provides a React context and set of hooks to manage authentication in your React applications. It facilitates API requests with Bearer token authentication, session handling, and login redirection. The context also supports custom hooks for GET
, POST
, PATCH
, and DELETE
requests.
- Auth Context and API Integration README
To integrate this into your React project, copy the provided code into your project and ensure the necessary dependencies are installed.
npm install newpaper-api
This provider manages authentication state and provides the loginPageUrl
and apiBaseUrl
to the components that consume this context.
Wrap your App Components with {children} like so :
import { AuthProvider } from "newpaper-api";
<AuthProvider
loginPageUrl="https://login.example.com"
apiBaseUrl="https://api.example.com"
>
<YourAppComponents />
</AuthProvider>;
The LoginButton
component provides a pre-built button for redirecting users to the login page.
This hook tells us if the user should log or not.
import { LoginButton, useAuthSession } from "newpaper-api";
// Your Component
const { shouldLogin } = useAuthSession();
{
shouldLogin && <LoginButton />;
}
{
!shouldLogin && <AppContent />;
}
This hook makes a GET
request with the Bearer token automatically included in the request header.
const { data, loading, error, refetch } = useGet({
path: "/data-endpoint",
options: { queryString: "?param=value" },
});
The usePost
hook is for sending POST
requests. It returns a submit function to be called with the request body.
const { loading, error, submit } = usePost({ path: "/submit-data" });
submit({ key: "value" }, (response) => {
console.log("Success", response);
});
Use this hook for making PATCH
requests.
const { loading, error, submit } = usePatch({ path: "/update-data" });
submit({ updateKey: "newValue" }, (response) => {
console.log("Update Success", response);
});
Use this hook for making DELETE
requests.
const { loading, error, submit } = useDelete({ path: "/delete-data" });
submit({}, (response) => {
console.log("Deleted", response);
});
This function opens a login popup to redirect users to the authentication page.
openLogin({ loginPageUrl: "https://login.example.com" });
Retrieves the Bearer token (bearer_token
) from the URL query parameters.
const token = getBToken();
Checks if a JWT token has expired.
const expired = isTokenExpired(token);
if (expired) {
// Redirect user to login
}
Each hook (useGet
, usePost
, usePatch
, and useDelete
) returns an error
state, which contains any errors encountered during the request. This should be handled in your UI, typically by showing an error message.
if (error) {
console.error(error.message);
}
const { data, loading, error, refetch } = useGet({ path: "/user/profile" });
if (loading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Error: {error.message}</p>;
}
return <div>{data?.name}</div>;
const { submit, loading, error } = usePost({ path: "/user/register" });
const handleRegister = () => {
submit({ username: "newUser" }, (data) => {
console.log("Registration success:", data);
});
};
return (
<div>
<button onClick={handleRegister} disabled={loading}>
Register
</button>
{error && <p>Error: {error.message}</p>}
</div>
);
const { shouldLogin } = useAuthSession();
if (shouldLogin) {
openLogin({ loginPageUrl: "https://login.example.com" });
}
This project is licensed under the MIT License.