Skip to content

Commit 029edcb

Browse files
authored
Release 0.1.0 (#8)
* Release 0.1.0
1 parent 877910d commit 029edcb

9 files changed

+114
-16
lines changed

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CHANGELOG.md

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Change Log
2+
3+
## [v0.1.0](https://github.com/auth0/auth0-react/tree/v0.1.0) (2020-05-08)
4+
5+
**Added**
6+
7+
- [SDK-1580][SDK-1581] Add withAuth and withLoginRequired [\#7](https://github.com/auth0/auth0-react/pull/7) ([adamjmcgrath](https://github.com/adamjmcgrath))
8+
- [SDK-1583] Setup basic README for Early Access [\#6](https://github.com/auth0/auth0-react/pull/6) ([adamjmcgrath](https://github.com/adamjmcgrath))
9+
- [SDK-1577][SDK-1578] Add login functionality [\#5](https://github.com/auth0/auth0-react/pull/5) ([adamjmcgrath](https://github.com/adamjmcgrath))
10+
- [SDK-1576] Linting [\#4](https://github.com/auth0/auth0-react/pull/4) ([adamjmcgrath](https://github.com/adamjmcgrath))
11+
- [SDK-1572] Added CircleCI config [\#3](https://github.com/auth0/auth0-react/pull/3) ([Widcket](https://github.com/Widcket))
12+
- [SDK-1568] Set up unit tests [\#2](https://github.com/auth0/auth0-react/pull/2) ([adamjmcgrath](https://github.com/adamjmcgrath))
13+
- [SDK-1570][SDK-1571] Setup dev environment and build targets [\#1](https://github.com/auth0/auth0-react/pull/1) ([adamjmcgrath](https://github.com/adamjmcgrath))

README.md

+82-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Auth0 SDK for React Applications.
1919

2020
- [Installation](#installation)
2121
- [Getting Started](#getting-started)
22+
- [Advanced Use Cases](#advanced-use-cases)
2223
- [Contributing](#contributing)
2324
- [Support + Feedback](#support--feedback)
2425
- [Vulnerability Reporting](#vulnerability-reporting)
@@ -28,17 +29,19 @@ Auth0 SDK for React Applications.
2829
## Installation
2930

3031
```bash
31-
npm install @auth0/auth0-spa-js auth0/auth0-react
32+
npm install @auth0/auth0-spa-js https://github.com/auth0/auth0-react/releases/download/0.1.0/auth0-auth0-react-js-0.1.0.tgz
3233
```
3334

3435
## Getting Started
3536

36-
```js
37+
Configure the SDK by wrapping your application in `Auth0Provider`:
38+
39+
```jsx
3740
// src/index.js
3841
import React from 'react';
3942
import ReactDOM from 'react-dom';
40-
import App from './App';
4143
import { Auth0Provider } from '@auth0/auth0-react';
44+
import App from './App';
4245

4346
ReactDOM.render(
4447
<Auth0Provider
@@ -52,7 +55,9 @@ ReactDOM.render(
5255
);
5356
```
5457

55-
```js
58+
Use the `useAuth0` hook in your components to access authentication state (`isLoading`, `isAuthenticated` and `user`) and authentication methods (`login` and `logout`):
59+
60+
```jsx
5661
// src/App.js
5762
import React from 'react';
5863
import { useAuth0 } from '@auth0/auth0-react';
@@ -77,6 +82,79 @@ function App() {
7782
export default App;
7883
```
7984

85+
## Advanced Use Cases
86+
87+
### Class Components
88+
89+
Use the `withAuth0` higher order component to add the `auth` property to Class components:
90+
91+
```jsx
92+
import React, { Component } from 'react';
93+
import { withAuth0 } from '@auth0/auth0-react';
94+
95+
class Profile extends Component {
96+
render() {
97+
const { user } = this.props.auth;
98+
return <div>Hello {user.name}</div>;
99+
}
100+
}
101+
102+
export default withAuth0(Profile);
103+
```
104+
105+
### Protecting Routes
106+
107+
Protect a route component using the `withLoginRequired` higher order component. Visits to this route when unauthenticated will redirect the user to the login page and back to this page after login:
108+
109+
```jsx
110+
import React from 'react';
111+
import { withLoginRequired } from '@auth0/auth0-react';
112+
113+
const PrivateRoute = () => <div>Private</div>;
114+
115+
export default withLoginRequired(PrivateRoute);
116+
```
117+
118+
### Access an API
119+
120+
Use a protected API with an Access Token:
121+
122+
```jsx
123+
import React, { useEffect, useState } from 'react';
124+
import { useAuth0 } from '@auth0/auth0-react';
125+
126+
const Posts = () => {
127+
const { getToken } = useAuth0();
128+
const [posts, setPosts] = useState(null);
129+
130+
useEffect(() => {
131+
(async () => {
132+
const token = await getToken();
133+
const response = await fetch('https://api.example.com/posts', {
134+
headers: {
135+
Authorization: `Bearer ${token}`,
136+
},
137+
});
138+
setPosts(await response.json());
139+
})();
140+
}, [getToken]);
141+
142+
if (!posts) {
143+
return <div>Loading...</div>;
144+
}
145+
146+
return (
147+
<ul>
148+
{posts.map((post, index) => {
149+
return <li key={index}>{post}</li>;
150+
})}
151+
</ul>
152+
);
153+
};
154+
155+
export default Posts;
156+
```
157+
80158
## Contributing
81159

82160
We appreciate feedback and contribution to this repo! Before you get started, please see the following:

__tests__/with-auth0.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import '@testing-library/jest-dom/extend-expect';
22
import React, { Component } from 'react';
3-
import withAuth0, { WithAuthProps } from '../src/with-auth0';
3+
import withAuth0, { WithAuth0Props } from '../src/with-auth0';
44
import { render, screen } from '@testing-library/react';
55

66
describe('withAuth0', () => {
77
it('should wrap a class component', () => {
8-
class MyComponent extends Component<WithAuthProps> {
8+
class MyComponent extends Component<WithAuth0Props> {
99
render(): JSX.Element {
1010
return <>hasAuth: {`${!!this.props.auth}`}</>;
1111
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "Auth0",
33
"name": "@auth0/auth0-react",
4-
"version": "0.0.0",
4+
"version": "0.1.0",
55
"description": "Auth0 SDK for React applications",
66
"keywords": [
77
"auth0",
@@ -23,7 +23,8 @@
2323
"build": "npm run lint && rollup -c --environment NODE_ENV:production",
2424
"lint": "eslint --ext=tsx ./src ./__tests__",
2525
"start": "rollup -cw",
26-
"test": "jest --coverage"
26+
"test": "jest --coverage",
27+
"prepack": "npm run test && npm run build"
2728
},
2829
"repository": {
2930
"type": "git",

src/auth0-provider.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ import { AppState, defaultOnRedirectCallback, hasAuthParams } from './utils';
1010
import { reducer } from './reducer';
1111
import { initialAuthState } from './auth-state';
1212

13-
interface AuthProviderOptions extends PropsWithChildren<Auth0ClientOptions> {
13+
export interface Auth0ProviderOptions
14+
extends PropsWithChildren<Auth0ClientOptions> {
1415
onRedirectCallback?: (appState: AppState) => void;
1516
}
1617

1718
const Auth0Provider = ({
1819
children,
1920
onRedirectCallback = defaultOnRedirectCallback,
2021
...opts
21-
}: AuthProviderOptions): JSX.Element => {
22+
}: Auth0ProviderOptions): JSX.Element => {
2223
const [client] = useState(() => new Auth0Client(opts));
2324
const [state, dispatch] = useReducer(reducer, initialAuthState);
2425

src/index.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export { default as Auth0Provider } from './auth0-provider';
1+
export {
2+
default as Auth0Provider,
3+
Auth0ProviderOptions,
4+
} from './auth0-provider';
25
export { default as useAuth0 } from './use-auth0';
3-
export { default as withAuth0 } from './with-auth0';
6+
export { default as withAuth0, WithAuth0Props } from './with-auth0';
47
export { default as withLoginRequired } from './with-login-required';
8+
export { Auth0ContextInterface } from './auth0-context';

src/with-auth0.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React, { ComponentType } from 'react';
22
import Auth0Context, { Auth0ContextInterface } from './auth0-context';
33

4-
export interface WithAuthProps {
4+
export interface WithAuth0Props {
55
auth: Auth0ContextInterface;
66
}
77

8-
const withAuth0 = <P extends WithAuthProps>(
8+
const withAuth0 = <P extends WithAuth0Props>(
99
Component: ComponentType<P>
10-
): ComponentType<Omit<P, keyof WithAuthProps>> => (props): JSX.Element => (
10+
): ComponentType<Omit<P, keyof WithAuth0Props>> => (props): JSX.Element => (
1111
<Auth0Context.Consumer>
1212
{(auth: Auth0ContextInterface): JSX.Element => (
1313
<Component auth={auth} {...(props as P)} />

0 commit comments

Comments
 (0)