Using with React
Using the @basictech/react library with your React project
Installation
Create a new React project
If you don’t have one already, create a new React project:
When prompted in the CLI, enter y
for installing the vite package.
Then give your project a name. Choose React as your framework.
And finally choose the TypeScript variant.
Install the @basictech/react library
cd project-name
into your project, run npm i
and install the @basictech/react
library in your app:
Set up basic.config
Create a basic.config.ts file
Create a new file called basic.config.ts
in the root of your project. You can either do this manually, or go through the CLI flow to have it automatically created for you.
Customize the config
Add your project ID to the basic.config.ts
file, and customize the schema in the file. You can copy your schema from the Basic dashboard and paste it into the file.
Any changes you make to the schema in basic.config need to eventually be deployed to the Basic dashboard. Local changes would be for development purposes only.
Adding Auth
Add the BasicProvider to main.tsx
Navigate to src/main.tsx
, add the BasicProvider to imports, and wrap your app in it.
Add a login button to App.tsx
Now navigate to src/App.tsx
and import signin
, isSignedIn
, and user
. Vite comes with some template code, but you can replace the contents with the following:
Let’s test it out! Clicking on the button should now redirect you to the Basic.id page where your users can login or create an account. Once they’re done, they’ll be redirected back to your app and see their email displayed.
Add a logout button
Use the isSignedIn hook to check if the user is logged in, and the signout function to sign out the user:
That’s it! You’ve successfully added Basic Auth to your React app 🎉
Using the database
You can use the React SDK to read and write to the user’s database. Checkout the React SDK docs to get started.