-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
128 lines (114 loc) · 3.45 KB
/
App.js
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
import React, { useCallback, useEffect, useState } from "react";
import { StatusBar } from "expo-status-bar";
import { StyleSheet, Text, View, Button, Linking } from "react-native";
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import { InAppBrowser } from "react-native-inappbrowser-reborn";
import axios from "axios";
const CLIENT_ID = process.env.EXPO_PUBLIC_CLIENT_ID;
const LOGIN_URL = `https://auth.furo.one/login/${CLIENT_ID}`;
const Stack = createNativeStackNavigator();
export default function App() {
const linking = {
prefixes: ["exp://"],
config: {
screens: {
Home: "*",
},
},
};
return (
<NavigationContainer linking={linking}>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
const HomeScreen = () => {
const [accessToken, setAccessToken] = useState(null);
const openURLFromInAppBrowserOrLinking = useCallback(async (url) => {
try {
if (await InAppBrowser.isAvailable()) {
const response = await InAppBrowser.openAuth(url, "exp://", {
/**
* iOS Properties
* If you want to disable the iOS system dialog, set the property `ephemeralWebSession` to `true`.
* More info: https://github.com/proyecto26/react-native-inappbrowser?tab=readme-ov-file#ios-options
*/
ephemeralWebSession: false,
// Android Properties
showTitle: false,
enableUrlBarHiding: true,
enableDefaultShare: false,
showInRecents: true,
forceCloseOnRedirection: true,
});
if (response.type === "success" && response.url) {
console.log(response);
Linking.openURL(response.url);
}
} else {
Linking.openURL(url);
}
} catch (error) {
console.error(error);
}
}, []);
useEffect(() => {
const handleDeepLink = ({ url }) => {
const code = url.split("?")[1]?.match(/code=([^&]+)/)?.[1];
if (code) {
authenticateWithCode(code)
.then((response) => {
const { access_token } = response;
setAccessToken(access_token);
})
.catch((e) => {
console.log("error: ", e.response.data);
});
}
};
Linking.addEventListener("url", handleDeepLink);
return () => {
Linking.removeAllListeners("url");
};
}, []);
const authenticateWithCode = async (code) => {
const { data } = await axios.post("https://api.furo.one/sessions/code/authenticate", { code });
return data;
};
return (
<View style={styles.container}>
<Text style={styles.title}>[Furo] React Native Example</Text>
<View style={styles.content}>
{accessToken ? (
<>
<Text>{accessToken}</Text>
<Button title="Clear" onPress={() => setAccessToken(null)} />
</>
) : (
<Button title="Login" onPress={() => openURLFromInAppBrowserOrLinking(LOGIN_URL)} />
)}
</View>
<StatusBar style="auto" />
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
title: {
fontSize: 16,
fontWeight: "600",
},
content: {
marginTop: 10,
width: "80%",
alignItems: "center",
},
});