|
6 | 6 | - Clean Code |
7 | 7 | - Test Code |
8 | 8 |
|
| 9 | +# React Native Auth0 |
9 | 10 |
|
10 | | -Basic use: |
| 11 | +A wrapper on auth0's react native library with common case features already implemented. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +1. Run on your terminal the following command: |
| 16 | + |
| 17 | +```sh |
| 18 | +$ npm i @cobuildlab/react-native-auth0 |
| 19 | +``` |
| 20 | +## Usage |
| 21 | + |
| 22 | +Create a new client instance using Auth0Native: |
11 | 23 |
|
12 | 24 | ```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'; |
18 | | - |
19 | | -import { auth0 } from './src/shared/auth0/client'; |
20 | | - |
21 | | -const App: React.FC = () => { |
22 | | - const { login, logout, isLoading, isAuthenticated } = useAuth(); |
23 | | - console.log('---- isAuthenticated --- ', isAuthenticated); |
24 | | - console.log('---- isLoading --- ', isLoading); |
25 | | - |
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) => { |
| 25 | +import { Auth0Native } from '@cobuildlab/react-native-auth0'; |
| 26 | + |
| 27 | +// AUTH0 options |
| 28 | +const AUTH0_OPTIONS = { |
| 29 | + clientId: AUTH_CLIENT_ID, |
| 30 | + domain: AUTH_CLIENT_DOMAIN, |
| 31 | +} |
| 32 | + |
| 33 | +// You can handle the credentials obtained in auth0 and store them in the async store or another store of your choice |
| 34 | +const CREDENTIALS_HANDLER = { |
| 35 | + save: async (credentials): Promise<void> => { |
44 | 36 | const value = JSON.stringify(credentials); |
45 | 37 | await AsyncStorage.setItem('credentials_store', value); |
46 | 38 | }, |
47 | | - getCredentials: async (): Promise<Credentials | null> => { |
| 39 | + get: async () => { |
48 | 40 | const value = await AsyncStorage.getItem('credentials_store'); |
49 | 41 | return value != null ? JSON.parse(value) : null; |
50 | 42 | }, |
51 | | -}; |
52 | | - |
53 | | -export const App = () => { |
54 | | - return ( |
55 | | - <AuthProvider config={Conifg}> |
56 | | - <App /> |
57 | | - </AuthProvider> |
58 | | - ); |
59 | | -}; |
| 43 | + clear: async () => { |
| 44 | + await AsyncStorage.removeItem('credentials_store'); |
| 45 | + apolloClient.resetStore(); |
| 46 | + }, |
| 47 | +} |
| 48 | + |
| 49 | +export const client = new Auth0Native(AUTH0_OPTIONS, CREDENTIALS_HANDLER); |
60 | 50 | ``` |
61 | 51 |
|
0 commit comments