Skip to content

Commit 4c9240c

Browse files
committed
Merge branch 'main' into feature/auth0-class-extend
2 parents a8c7f1b + 3fb1dea commit 4c9240c

1 file changed

Lines changed: 114 additions & 45 deletions

File tree

README.md

Lines changed: 114 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,130 @@
1-
# TODO
2-
3-
- Documentation
4-
- Refresh Token
5-
- Auth0 Params
6-
- Clean Code
7-
- Test Code
1+
# React Native Auth0
82

3+
A wrapper on auth0's react native library with common case features already implemented.
94

10-
Basic use:
5+
## Installation
116

12-
```tsx
13-
import React from 'react';
14-
import { View, Button, Text } from 'react-native';
15-
import { useAuth, AuthProviderConfig, AuthProvider } from '@cobuildlab/react-native-auth0';
16-
import { Credentials } from 'react-native-auth0';
17-
import AsyncStorage from '@react-native-async-storage/async-storage';
7+
1. Run on your terminal the following command:
188

19-
import { auth0 } from './src/shared/auth0/client';
9+
```sh
10+
$ npm i @cobuildlab/react-native-auth0
11+
```
12+
## Usage
2013

21-
const App: React.FC = () => {
22-
const { login, logout, isLoading, isAuthenticated } = useAuth();
23-
console.log('---- isAuthenticated --- ', isAuthenticated);
24-
console.log('---- isLoading --- ', isLoading);
14+
#### Auth0 Client Setup:
2515

26-
return (
27-
<View>
28-
<Text>{isAuthenticated ? 'Im On' : 'Im Off'}</Text>
29-
<Button onPress={login} title="login" />
30-
<Button onPress={logout} title="logout" />
31-
</View>
32-
);
33-
};
34-
35-
const Conifg: AuthProviderConfig = {
36-
auth0,
37-
eichBaseToken: '8base-token',
38-
eichBaseAuthProfileId: 'my-profile-id',
39-
eichBaseEndpoint: '8base-environemnt-branch',
40-
removeCredentials: async () => {
41-
await AsyncStorage.removeItem('credentials_store');
42-
},
43-
saveCredentials: async (credentials) => {
16+
Create a new client instance using Auth0Native:
17+
18+
```tsx
19+
import { Auth0Native } from '@cobuildlab/react-native-auth0';
20+
21+
// AUTH0 options
22+
const AUTH0_OPTIONS = {
23+
clientId: AUTH_CLIENT_ID,
24+
domain: AUTH_CLIENT_DOMAIN,
25+
}
26+
27+
// You can handle the credentials obtained in auth0
28+
// and store them in the async store or another store of your choice
29+
const CREDENTIALS_HANDLER = {
30+
save: async (credentials): Promise<void> => {
4431
const value = JSON.stringify(credentials);
4532
await AsyncStorage.setItem('credentials_store', value);
4633
},
47-
getCredentials: async (): Promise<Credentials | null> => {
34+
get: async () => {
4835
const value = await AsyncStorage.getItem('credentials_store');
4936
return value != null ? JSON.parse(value) : null;
5037
},
51-
};
38+
clear: async () => {
39+
await AsyncStorage.removeItem('credentials_store');
40+
apolloClient.resetStore();
41+
},
42+
}
43+
44+
export const client = new Auth0Native(AUTH0_OPTIONS, CREDENTIALS_HANDLER);
45+
```
46+
47+
Then import the client and pass through props to the provider:
48+
49+
```tsx
50+
import { AuthProvider } from '@cobuildlab/react-native-auth0';
51+
import { client } from './config';
52+
53+
54+
const AUTH0_SCOPE = 'offline_access email openid profile';
55+
56+
export const App = () => (
57+
<AuthProvider
58+
client={auth0NativeClient}
59+
scope={client}>
60+
<App />
61+
</AuthProvider>
62+
);
63+
```
64+
65+
#### Hook usage:
66+
67+
Login View:
68+
69+
```tsx
70+
import { View, Button } from 'react-native;
71+
import { useAuth } from '@cobuildlab/react-native-auth0';
72+
73+
export const LoginView = () => {
74+
const { authorize } = useAuth();
75+
76+
return (
77+
<View>
78+
<Button onPress={authorize} title="Sign in">
79+
</View>
80+
)
81+
}
82+
83+
```
84+
85+
Logout View:
86+
87+
```tsx
88+
import { View, Button } from 'react-native;
89+
import { useAuth } from '@cobuildlab/react-native-auth0';
90+
91+
export const LogOutView = () => {
92+
const { clearSession } = useAuth();
5293

53-
export const App = () => {
5494
return (
55-
<AuthProvider config={Conifg}>
56-
<App />
57-
</AuthProvider>
58-
);
59-
};
95+
<View>
96+
<Button onPress={clearSession} title="Sign in">
97+
</View>
98+
)
99+
}
60100
```
61101

102+
App Component:
103+
104+
```tsx
105+
import { useAuth } from '@cobuildlab/react-native-auth0';
106+
import { LoginView } from './LoginView';
107+
import { LogOutView } from './LogOutView';
108+
import { MainView, Loading } from './others'
109+
110+
111+
export function App() {
112+
const { isLoading, isAuthenticated, clearSession } = useAuth();
113+
114+
if(isLoading){
115+
return <Loading />
116+
}
117+
118+
if(!isAuthenticated){
119+
return < LoginView />
120+
}
121+
122+
return <MainView />
123+
}
124+
125+
```
126+
127+
### TODO
128+
129+
- Documentation
130+
- Test Code

0 commit comments

Comments
 (0)