Skip to content

Commit 89f0862

Browse files
piotr pietruszewskipi0trpietruszewski
authored andcommitted
refactor: added user login status persist to example app
1 parent 4d0603a commit 89f0862

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"start": "react-native start"
1010
},
1111
"dependencies": {
12+
"@react-native-async-storage/async-storage": "^1.15.5",
1213
"react": "16.13.1",
1314
"react-native": "0.63.4",
1415
"react-native-config": "^1.4.2"

example/src/App.tsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import Button from './Button';
2020
import Input from './Input';
2121
import type { Registration } from '../../lib/typescript';
2222
import Config from 'react-native-config';
23+
import AsyncStorage from '@react-native-async-storage/async-storage';
24+
25+
const AUTK_KEY = 'auth';
2326

2427
const COLLECTIONS: string[] = []; //Provide help center collections ids
2528
// To change, replace values in .env
@@ -65,6 +68,16 @@ export default function App() {
6568
};
6669

6770
useEffect(() => {
71+
/**
72+
* Restore user login status
73+
*/
74+
AsyncStorage.getItem(AUTK_KEY).then((it) => {
75+
it === 'true' && setLoggedUser(true);
76+
if (it && it !== 'true') {
77+
setUser((u) => ({ ...u, email: it }));
78+
}
79+
});
80+
6881
/**
6982
* Handle PushNotification
7083
*/
@@ -154,7 +167,10 @@ export default function App() {
154167
disabled={loggedUser}
155168
title="Login unidentified User"
156169
onPress={() => {
157-
Intercom.registerUnidentifiedUser().then(() => setLoggedUser(true));
170+
Intercom.registerUnidentifiedUser().then(() => {
171+
setLoggedUser(true);
172+
AsyncStorage.setItem(AUTK_KEY, 'true');
173+
});
158174
}}
159175
/>
160176
<Input
@@ -174,9 +190,10 @@ export default function App() {
174190
title="Login identified User"
175191
onPress={() => {
176192
if (user.email?.includes('@')) {
177-
Intercom.registerIdentifiedUser(user).then(() =>
178-
setLoggedUser(true)
179-
);
193+
Intercom.registerIdentifiedUser(user).then(() => {
194+
AsyncStorage.setItem(AUTK_KEY, user.email ?? '');
195+
setLoggedUser(true);
196+
});
180197
} else {
181198
showEmptyAlertMessage('Email');
182199
}
@@ -425,7 +442,10 @@ export default function App() {
425442
disabled={!loggedUser}
426443
title="Logout user"
427444
onPress={() => {
428-
Intercom.logout().then(() => setLoggedUser(false));
445+
Intercom.logout().then(() => {
446+
AsyncStorage.removeItem(AUTK_KEY);
447+
setLoggedUser(false);
448+
});
429449
}}
430450
/>
431451
</ScrollView>

0 commit comments

Comments
 (0)