forked from stashenergy/react-native-msal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
131 lines (122 loc) · 3.9 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
import * as React from 'react';
import { StyleSheet, Text, Button, SafeAreaView, ScrollView, Platform, Switch, View } from 'react-native';
import MSALCLient, { MSALResult } from 'react-native-msal';
// Example config, modify to your needs
const msalConfig = {
clientId: '<CLIENT_ID>',
sisuAuthority: 'https://<TENANT_NAME>.b2clogin.com/tfp/<TENANT_NAME>.onmicrosoft.com/B2C_1A_SignInUp_Native',
pswdResetAuthority:
'https://<TENANT_NAME>.b2clogin.com/tfp/<TENANT_NAME>.onmicrosoft.com/B2C_1A_PasswordReset_Native',
scopes: ['https://<TENANT_NAME>.onmicrosoft.com/api/user_impersonation'],
};
const msalClient = new MSALCLient(msalConfig.clientId);
export default function App() {
const [authResult, setAuthResult] = React.useState<MSALResult | null>(null);
const [prefersEphemeralWebBrowserSession, setPrefersEphemeralWebBrowserSession] = React.useState<boolean>(false);
const handleResult = (result: MSALResult) => {
setAuthResult(result);
};
const acquireToken = async () => {
try {
const res = await msalClient.acquireToken({
authority: msalConfig.sisuAuthority,
scopes: msalConfig.scopes,
ios_prefersEphemeralWebBrowserSession: prefersEphemeralWebBrowserSession,
});
handleResult(res);
} catch (error) {
console.warn(error);
}
};
const acquireTokenSilent = async () => {
if (authResult) {
try {
const res = await msalClient.acquireTokenSilent({
authority: msalConfig.sisuAuthority,
scopes: msalConfig.scopes,
accountIdentifier: authResult.account.identifier,
});
handleResult(res);
} catch (error) {
console.warn(error);
}
}
};
const removeAccount = async () => {
if (authResult) {
try {
await msalClient.removeAccount({
authority: msalConfig.sisuAuthority,
accountIdentifier: authResult.account.identifier,
});
setAuthResult(null);
} catch (error) {
console.warn(error);
}
}
};
const signout = async () => {
if (authResult) {
try {
await msalClient.signout({
authority: msalConfig.sisuAuthority,
accountIdentifier: authResult.account.identifier,
ios_prefersEphemeralWebBrowserSession: prefersEphemeralWebBrowserSession,
});
setAuthResult(null);
} catch (error) {
console.warn(error);
}
}
};
return (
<SafeAreaView style={styles.container}>
<Button title="Acquire Token" onPress={acquireToken} />
<Button title="Acquire Token Silently" onPress={acquireTokenSilent} disabled={!authResult} />
<Button title="Remove account" onPress={removeAccount} disabled={!authResult} />
{Platform.OS === 'ios' && <Button title="Sign out (iOS only)" onPress={signout} disabled={!authResult} />}
{Platform.OS === 'ios' && (
<View style={styles.switch}>
<View style={styles.switchSpacer} />
<View style={styles.switchLabel}>
<Text
onPress={() => setPrefersEphemeralWebBrowserSession(!prefersEphemeralWebBrowserSession)}
style={styles.text}
>
Prefer ephemeral web browser session?
{'\n'}
(iOS only)
</Text>
</View>
<View style={styles.switchSpacer}>
<Switch value={prefersEphemeralWebBrowserSession} onValueChange={setPrefersEphemeralWebBrowserSession} />
</View>
</View>
)}
<ScrollView>
<Text>{JSON.stringify(authResult, null, 4)}</Text>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
padding: 4,
},
switch: {
flexDirection: 'row',
alignItems: 'center',
},
text: {
textAlign: 'center',
},
switchSpacer: {
flex: 1,
},
switchLabel: {
flexGrow: 0,
padding: 10,
},
});