@@ -20,6 +20,9 @@ import Button from './Button';
2020import Input from './Input' ;
2121import type { Registration } from '../../lib/typescript' ;
2222import Config from 'react-native-config' ;
23+ import AsyncStorage from '@react-native-async-storage/async-storage' ;
24+
25+ const AUTK_KEY = 'auth' ;
2326
2427const 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