This repository has been archived by the owner on May 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathApp.tsx
132 lines (121 loc) · 3.49 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Example for a Azure B2C application using a B2CClient helper class
*/
import React from 'react';
import { Platform, SafeAreaView, ScrollView, StyleSheet, Switch, Text, View, TouchableOpacity } from 'react-native';
import type { MSALResult, MSALWebviewParams } from 'react-native-msal';
import { B2CClient } from './b2cClient';
import { b2cConfig, b2cScopes as scopes } from './msalConfig';
const b2cClient = new B2CClient(b2cConfig);
export default function App() {
const [authResult, setAuthResult] = React.useState<MSALResult | null>(null);
const [iosEphemeralSession, setIosEphemeralSession] = React.useState(false);
const webviewParameters: MSALWebviewParams = {
ios_prefersEphemeralWebBrowserSession: iosEphemeralSession,
};
React.useEffect(() => {
async function init() {
try {
await b2cClient.init();
const isSignedIn = await b2cClient.isSignedIn();
if (isSignedIn) {
setAuthResult(await b2cClient.acquireTokenSilent({ scopes }));
}
} catch (error) {
console.error(error);
}
}
init();
}, []);
const handleSignInPress = async () => {
try {
const res = await b2cClient.signIn({ scopes, webviewParameters });
setAuthResult(res);
} catch (error) {
console.warn(error);
}
};
const handleAcquireTokenPress = async () => {
try {
const res = await b2cClient.acquireTokenSilent({ scopes, forceRefresh: true });
setAuthResult(res);
} catch (error) {
console.warn(error);
}
};
const handleSignoutPress = async () => {
try {
await b2cClient.signOut();
setAuthResult(null);
} catch (error) {
console.warn(error);
}
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.buttonContainer}>
{authResult ? (
<>
<TouchableOpacity style={styles.button} onPress={handleAcquireTokenPress}>
<Text>Acquire Token (Silent)</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={handleSignoutPress}>
<Text>Sign Out</Text>
</TouchableOpacity>
</>
) : (
<TouchableOpacity style={styles.button} onPress={handleSignInPress}>
<Text>Sign In</Text>
</TouchableOpacity>
)}
{Platform.OS === 'ios' && (
<TouchableOpacity
style={[styles.button, styles.switchButton]}
onPress={() => setIosEphemeralSession(!iosEphemeralSession)}
>
<Text>Prefer ephemeral browser session (iOS only)</Text>
<Switch value={iosEphemeralSession} onValueChange={setIosEphemeralSession} />
</TouchableOpacity>
)}
</View>
<ScrollView style={styles.scrollView}>
<Text>{JSON.stringify(authResult, null, 2)}</Text>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: '1%',
backgroundColor: 'white',
},
buttonContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
paddingBottom: '1%',
margin: '-0.5%',
},
button: {
backgroundColor: 'aliceblue',
borderWidth: 1,
margin: '0.5%',
padding: 8,
width: '49%',
alignItems: 'center',
},
disabledButton: {
backgroundColor: '#ddd',
},
switchButton: {
flexDirection: 'row',
justifyContent: 'space-around',
padding: 4,
margin: '0.5%',
width: '99%',
},
scrollView: {
borderWidth: 1,
padding: 1,
},
});