Skip to content

Commit

Permalink
Possible refresh bug (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw authored Feb 14, 2024
1 parent 8749187 commit e734110
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
45 changes: 11 additions & 34 deletions native_gg/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,25 @@ import { useEffect, useReducer, useRef } from "react";
import AuthUI from "./src/authentication/AuthUI.tsx";
import { clientID } from "./src/constants/env.ts";
import AuthService from "./src/authentication/AuthService.ts";
import { AppAction, AppState } from "./src/state/Actions.ts";

const initialState: AppState = {
authenticated: false,
currentAccount: null,
};

const reducer = (state: AppState, action: AppAction) => {
const { authenticated, currentAccount } = state;
switch (action.type) {
case "setAuthenticated": {
return { authenticated: action.payload, currentAccount };
}
case "setCurrentAccount": {
return { authenticated, currentAccount: action.payload };
}
default: {
return state;
}
}
};
import { authReducer, initialAuthState } from "./src/state/Actions.ts";

export default function App() {
if (process.env.NODE_ENV === "development" && clientID === undefined) {
console.warn("No .ENV file found. Please create one.");
}
const authServiceRef = useRef<AuthService | null>(null);
authServiceRef.current = AuthService.getInstance();

const [state, dispatch] = useReducer(reducer, initialState);
authServiceRef.current.subscribe(dispatch);
const [state, dispatch] = useReducer(authReducer, initialAuthState);

const accountAvatar = state.initComplete
? state.currentAccount
? `https://www.bungie.net${state.currentAccount?.iconPath}`
: "https://d33wubrfki0l68.cloudfront.net/554c3b0e09cf167f0281fda839a5433f2040b349/ecfc9/img/header_logo.svg"
: "";

useEffect(() => {
authServiceRef.current = AuthService.getInstance();
authServiceRef.current.subscribe(dispatch);
// Unsubscribe when the component unmounts
return () => {
if (authServiceRef.current) {
Expand All @@ -56,16 +42,7 @@ export default function App() {
/>
<Text style={{ color: "#fff" }}>Guardian Ghost</Text>

<Image
style={{ width: 200, height: 200 }}
contentFit="contain"
transition={300}
source={
state.currentAccount
? `https://www.bungie.net${state.currentAccount?.iconPath}`
: "https://d33wubrfki0l68.cloudfront.net/554c3b0e09cf167f0281fda839a5433f2040b349/ecfc9/img/header_logo.svg"
}
/>
<Image style={{ width: 200, height: 200 }} contentFit="contain" transition={300} source={accountAvatar} />

<AuthUI
startAuth={() => {
Expand Down
15 changes: 12 additions & 3 deletions native_gg/src/authentication/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as WebBrowser from "expo-web-browser";
import * as v from "valibot";
import { clientID, redirectURL } from "../constants/env.ts";
import { Store } from "../constants/storage.ts";
import { AppAction } from "../state/Actions.ts";
import { RefreshToken, refreshTokenSchema } from "./Types.ts";
import { getAccessToken, getRefreshToken } from "./Utilities.ts";
import {
Expand All @@ -15,11 +14,12 @@ import {
getLinkedProfiles,
linkedProfilesSchema,
} from "../account/Account.ts";
import { AuthAction } from "../state/Actions.ts";

class AuthService {
private static instance: AuthService;
private authToken: RefreshToken | null;
private dispatch: React.Dispatch<AppAction> | null;
private dispatch: React.Dispatch<AuthAction> | null;
private stateID: string;
private usedAuthCodes: Array<string>;
private currentAccount: BungieUser | null;
Expand All @@ -40,6 +40,7 @@ class AuthService {
console.info("No valid user and auth found");
})
.finally(() => {
this.setInitComplete();
const p2 = performance.now();
console.log("took:", (p2 - p1).toFixed(4), "ms");
});
Expand Down Expand Up @@ -101,7 +102,7 @@ class AuthService {
}

// Method to subscribe to auth changes
subscribe(dispatch: React.Dispatch<AppAction>) {
subscribe(dispatch: React.Dispatch<AuthAction>) {
this.dispatch = dispatch;
}

Expand Down Expand Up @@ -146,6 +147,14 @@ class AuthService {
}
}

setInitComplete() {
if (this.dispatch) {
this.dispatch({ type: "setInitComplete", payload: true });
} else {
console.error("No dispatch");
}
}

startAuth(): void {
this.stateID = randomUUID();
const authURL = `https://www.bungie.net/en/oauth/authorize?client_id=${clientID}&response_type=code&reauth=true&state=${this.stateID}`;
Expand Down
37 changes: 33 additions & 4 deletions native_gg/src/state/Actions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import { BungieUser } from "../account/Account";

export type AppAction =
export type AuthState = {
initComplete: boolean;
authenticated: boolean;
currentAccount: BungieUser | null;
};

export const initialAuthState: AuthState = {
initComplete: false,
authenticated: false,
currentAccount: null,
};

export type AuthAction =
| {
type: "setInitComplete";
payload: boolean;
}
| {
type: "setAuthenticated";
payload: boolean;
Expand All @@ -10,7 +26,20 @@ export type AppAction =
payload: BungieUser | null;
};

export type AppState = {
authenticated: boolean;
currentAccount: BungieUser | null;
export const authReducer = (state: AuthState, action: AuthAction) => {
const { initComplete, authenticated, currentAccount } = state;
switch (action.type) {
case "setInitComplete": {
return { initComplete: action.payload, authenticated, currentAccount };
}
case "setAuthenticated": {
return { initComplete, authenticated: action.payload, currentAccount };
}
case "setCurrentAccount": {
return { initComplete, authenticated, currentAccount: action.payload };
}
default: {
return state;
}
}
};

0 comments on commit e734110

Please sign in to comment.