Polyfire provides a hook to simplify the login process of your users in React.
NOTE:
The usePolyfire hook is a React Hook
If you're not using React, please follow this page instead: Importing polyfire
Importing Polyfire
First install the Polyfire SDK:
npm install polyfire-js
Usage
PolyfireProvider
You will need to wrap your app in the Polyfire Provider to be able to use the usePolyfire hook. Pass your project id to the PolyfireProvider
root.render(
<PolyfireProvider project="your_project_alias">
<App />
</PolyfireProvider>
);
Hook: usePolyfire
The usePolyfire hook exports a client with all the Polyfire functions, to use most of them you will first need to login. The authentification functions are exported like this:
const { auth: { login, logout, status }, models: { generate } } = usePolyfire();
All of the functions except the login function requires the user to be authenticated before calling them. You can check whether you're authenticated with status
. status
can be authenticated
, unauthenticated
and loading
.
if (status === "authenticated") {
const haiku = await generate("Generate a hello world haiku");
console.log(haiku);
}
The login function takes the login provider as a parameter.
<button onClick={() => login("github")}>Login with Github</button>
WARNING: For now, the only login provider available are GitHub, Google and Firebase, we will add more in the future
To logout, use the logout function
<button onClick={() => logout()}>Logout</button>