|
1 | | -# TODO |
2 | | - |
3 | | - - Documentation |
4 | | - - Refresh Token |
5 | | - - Auth0 Params |
6 | | - - Clean Code |
7 | | - - Test Code |
| 1 | +# React Native Auth0 |
8 | 2 |
|
| 3 | +A wrapper on auth0's react native library with common case features already implemented. |
9 | 4 |
|
10 | | -Basic use: |
| 5 | +## Installation |
11 | 6 |
|
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: |
18 | 8 |
|
19 | | -import { auth0 } from './src/shared/auth0/client'; |
| 9 | +```sh |
| 10 | +$ npm i @cobuildlab/react-native-auth0 |
| 11 | +``` |
| 12 | +## Usage |
20 | 13 |
|
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: |
25 | 15 |
|
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> => { |
44 | 31 | const value = JSON.stringify(credentials); |
45 | 32 | await AsyncStorage.setItem('credentials_store', value); |
46 | 33 | }, |
47 | | - getCredentials: async (): Promise<Credentials | null> => { |
| 34 | + get: async () => { |
48 | 35 | const value = await AsyncStorage.getItem('credentials_store'); |
49 | 36 | return value != null ? JSON.parse(value) : null; |
50 | 37 | }, |
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(); |
52 | 93 |
|
53 | | -export const App = () => { |
54 | 94 | return ( |
55 | | - <AuthProvider config={Conifg}> |
56 | | - <App /> |
57 | | - </AuthProvider> |
58 | | - ); |
59 | | -}; |
| 95 | + <View> |
| 96 | + <Button onPress={clearSession} title="Sign in"> |
| 97 | + </View> |
| 98 | + ) |
| 99 | +} |
60 | 100 | ``` |
61 | 101 |
|
| 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