Skip to content

Commit

Permalink
Expo web auth fix (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw authored Feb 7, 2024
1 parent 1f55e8e commit 28b112e
Show file tree
Hide file tree
Showing 3 changed files with 2,267 additions and 35 deletions.
50 changes: 41 additions & 9 deletions native_gg/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,59 @@ import { useEffect, useState } from "react";

export default function App() {
const url = Linking.useURL();
const env = process.env.NODE_ENV;
const localAppID = "46261";
const realAppID = "46250";
const localRedirectURL = "https://localhost:19006/auth";
const realRedirectURL = "https://guardianghost.com/auth";

const redirectURL = env === "development" ? localRedirectURL : realRedirectURL;

const appID = env === "development" ? localAppID : realAppID;

const [token, setToken] = useState("");

useEffect(() => {
if (url) {
function processURL(url: string) {
console.log(Platform.OS, url);
if (Platform.OS === "web") {
const { hostname, path, queryParams } = Linking.parse(url);
console.log("hostname", hostname, "path", path, "queryParams", queryParams);

if (queryParams?.code) {
console.log("yay:", queryParams?.code);
setToken(queryParams.code.toString());
} else {
console.log("boooo");
}
}
}

useEffect(() => {
if (url) {
const { hostname, path, queryParams } = Linking.parse(url);
console.log("useEffect", Platform.OS, process.env.NODE_ENV);
if (queryParams?.code) {
console.log("ue: code:", queryParams?.code);
setToken(queryParams.code.toString());

if (Platform.OS === "ios") {
WebBrowser.dismissAuthSession();
if (Platform.OS === "ios") {
WebBrowser.dismissAuthSession();
}

if (Platform.OS === "web") {
console.log("maybeCompleteAuthSession");
WebBrowser.maybeCompleteAuthSession();
}
}
}
}, [url]);

function openURL(url: string) {
console.log("Opening URL in WebBrowser!");
WebBrowser.openAuthSessionAsync(url, "https://guardianghost.com/auth").then((result) => {
console.log("WebBrowser result", result);
WebBrowser.openAuthSessionAsync(url, `${redirectURL}`).then((result) => {
console.log("hi nigel", result);
if (result?.type === "success") {
processURL(result.url);
}
});
}

Expand All @@ -47,11 +79,11 @@ export default function App() {
<Button
title="Auth"
onPress={() =>
openURL("https://www.bungie.net/en/oauth/authorize?client_id=46250&response_type=code&reauth=true")
openURL(`https://www.bungie.net/en/oauth/authorize?client_id=${appID}&response_type=code&reauth=true`)
}
/>
<Text style={{ fontSize: 22, marginTop: 15, color: "#150f63" }}>
Auth token: <Text style={{ fontWeight: "bold" }}>{token}</Text>
Auth token:: <Text style={{ fontWeight: "bold" }}>{token}</Text>
</Text>
<StatusBar style="auto" />
</View>
Expand Down
1 change: 1 addition & 0 deletions native_gg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dependencies": {
"@babel/preset-env": "7.23.9",
"@expo/metro-runtime": "3.1.3",
"@expo/webpack-config": "19.0.1",
"expo": "50.0.6",
"expo-build-properties": "0.11.1",
"expo-dev-client": "3.3.8",
Expand Down
Loading

0 comments on commit 28b112e

Please sign in to comment.