Skip to content

Commit

Permalink
Validate token on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
NigelBreslaw committed Feb 17, 2024
1 parent 2377e6f commit 97eca7f
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion native_gg/src/authentication/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ 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 { RefreshToken, refreshTokenSchema, getAccessToken, getRefreshToken, isValidAccessToken } from "./Utilities.ts";
import {
RefreshToken,
refreshTokenSchema,
getAccessToken,
getRefreshToken,
isValidAccessToken,
isValidRefreshToken,
} from "./Utilities.ts";
import {
BungieUser,
BungieUserSchema,
Expand Down Expand Up @@ -69,6 +76,30 @@ class AuthService {
try {
const stringToken = v.parse(v.string(), token);
const validatedToken = v.parse(refreshTokenSchema, JSON.parse(stringToken));

const isValidRefresh = isValidRefreshToken(validatedToken);
if (!isValidRefresh) {
console.error("Refresh token expired");
return reject(false);
}
console.log("valid refresh token");
const isValidAccess = isValidAccessToken(validatedToken);
if (!isValidAccess) {
console.log("Access token expired");
getAccessToken(validatedToken)
.then((newAuthToken) => {
console.log("Got new access token");
AsyncStorage.setItem(
`${this.currentUserID}${Store._refresh_token}`,
JSON.stringify(newAuthToken),
);
this.setAuthToken(newAuthToken);
return resolve(true);
})
.catch((e) => {
return reject(e);
});
}
this.setAuthToken(validatedToken);
return resolve(true);
} catch (error) {
Expand Down

0 comments on commit 97eca7f

Please sign in to comment.