Skip to content

Commit f3b1365

Browse files
committed
Update README.md
1 parent e577dfc commit f3b1365

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

README.md

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<p align="center">
22
<a href="https://www.propelauth.com?ref=github" target="_blank" align="center">
3-
<img src="https://propelauth-logos.s3.us-west-2.amazonaws.com/logo-only.png" width="100">
3+
<img src="https://www.propelauth.com/imgs/lockup.svg" width="200">
44
</a>
55
</p>
66

@@ -17,6 +17,72 @@ Your frontend gets a beautiful, safe, and customizable login screen. Your backen
1717
- Full reference this library is [here](https://docs.propelauth.com/reference/frontend-apis/react)
1818
- Getting started guides for PropelAuth are [here](https://docs.propelauth.com/)
1919

20+
## Installation
21+
22+
```bash
23+
npm install @propelauth/react
24+
```
25+
26+
## Configuration
27+
28+
We need to tell PropelAuth where our application is running so that it will allow requests from our application.
29+
Go to the **Frontend Integration** section of your [PropelAuth dashboard](https://app.propelauth.com), and
30+
enter http://localhost:3000 into the **Application URL** field:
31+
32+
While we're here, we'll also copy the Auth URL into an `.env` file, which we'll use in a second:
33+
34+
```text
35+
# Test environment only, in production, we'll use our own domain
36+
REACT_APP_AUTH_URL=https://something.propelauthtest.com
37+
38+
# If you are using Vite:
39+
# VITE_AUTH_URL=https://something.propelauthtest.com
40+
```
41+
42+
## Initialization
43+
44+
At the root of your application, wrap your app in an [AuthProvider](https://docs.propelauth.com/reference/frontend-apis/react#auth-provider) component.
45+
46+
```js
47+
import { AuthProvider } from "@propelauth/react";
48+
49+
const root = ReactDOM.createRoot(document.getElementById("root"));
50+
root.render(
51+
<AuthProvider authUrl={process.env.REACT_APP_AUTH_URL}>
52+
<YourApp />
53+
</AuthProvider>,
54+
document.getElementById("root")
55+
);
56+
```
57+
58+
The [AuthProvider](https://docs.propelauth.com/reference/frontend-apis/react#auth-provider) is responsible for fetching the current user's authentication information.
59+
If your entire application requires the user to be logged in (for example, for a dashboard),
60+
use [RequiredAuthProvider](https://docs.propelauth.com/reference/frontend-apis/react#required-auth-provider) instead and you'll never have to check `isLoggedIn`.
61+
62+
## Authorization in the React library
63+
64+
On the frontend, authorization is useful for hiding UI elements that the user doesn't have access to.
65+
You will still need to implement authorization on the backend to prevent users from accessing data they shouldn't.
66+
67+
For example, you may want to hide the "Billing" page from users who aren't admins.
68+
You can do this by using the [UserClass](https://docs.propelauth.com/reference/frontend-apis/react#user-class) to check if the user has the "Admin" role.
69+
70+
```jsx
71+
const Sidebar = withRequiredAuthInfo(({userClass}) => {
72+
// We're using the router here to get the current orgId
73+
const router = useRouter()
74+
const orgId = router.query.orgId as string
75+
76+
// Only Admins can see the billing page, so hide it from the sidebar
77+
const isAdmin = userClass.isAtLeastRole(orgId, "Admin")
78+
return <div>
79+
<div>Dashboard</div>
80+
<div>Reports</div>
81+
{isAdmin && <div>Billing</div>}
82+
</div>
83+
})
84+
```
85+
2086
## Questions?
2187

2288
Feel free to reach out at [email protected]

0 commit comments

Comments
 (0)