Skip to content

Commit 4742ca3

Browse files
committed
Added docs and more debug log
1 parent ae37530 commit 4742ca3

File tree

7 files changed

+61
-13
lines changed

7 files changed

+61
-13
lines changed

src/app/routes/Landing.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useNavigate } from 'react-router'
22

33
import { useEffect} from "react"
44
import BungieLogin from '../../features/auth/BungieLogin'
5-
import { generateToken, regenerateTokens } from '../../lib/bungie_api/TokenService'
5+
import { regenerateTokens } from '../../lib/bungie_api/TokenService'
66
import { isAuthenticated } from '../../lib/bungie_api/AuthService'
77

88

@@ -20,6 +20,8 @@ export const LandingRoute = () => {
2020
navigate('/app')
2121
}
2222

23+
console.log("Not authenticated")
24+
2325
}, [])
2426

2527
return (

src/app/routes/Return.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import { useEffect } from "react"
22
import { useNavigate } from "react-router-dom"
33
import { handleAuthReturn } from "../../features/auth/AuthReturn"
44

5+
/**
6+
* Bungie OAuth redirects here
7+
*/
58
export const ReturnRoute = () => {
69

710
const navigate = useNavigate()
811

912
useEffect( () => {
1013

1114
if (handleAuthReturn()) {
15+
// exit component if successful
1216
navigate('/')
1317
}
1418

src/features/auth/AuthReturn.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ function getAuthCodeFromURL(): string | null {
55
return window.location.href.includes("code=") ? window.location.href.split('code=')[1] : null
66
}
77

8+
/**
9+
* Get auth tokens from auth code
10+
* @returns whether or not tokens were successfully generated
11+
*/
812
export function handleAuthReturn(): boolean {
913

1014
const code = getAuthCodeFromURL()

src/lib/bungie_api/AuthService.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ import { Navigate } from "react-router-dom"
22
import { isTokenExpired } from "./TokenService"
33
import { getTokens } from "./TokensStore"
44

5+
/**
6+
* Navigates to the Bungie OAuth url
7+
*/
58
export function authenticate(): void {
69
window.location.replace(`https://www.bungie.net/en/OAuth/Authorize?client_id=${import.meta.env.VITE_CLIENT_ID}&response_type=code`)
710
}
811

12+
/**
13+
* Whether or not the user is authenticated.
14+
*
15+
* @returns if auth tokens are present in local store
16+
*/
917
export function isAuthenticated(): boolean {
1018

1119
const tokens = getTokens()
1220

1321
return !tokens ? false : !isTokenExpired(tokens.accessToken)
1422
}
1523

24+
/**
25+
* Restricts rendering of children if not authenticated
26+
*/
1627
export const ProtectedRoute = ({ children }: { children: React.ReactNode }) => {
1728

1829
if (!isAuthenticated()) {

src/lib/bungie_api/BungieApiClient.ts

+13
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,23 @@ const apiClient = axios.create({
99
}
1010
})
1111

12+
/**
13+
* Bungie get request
14+
* @param url
15+
* @param config axios config
16+
* @returns
17+
*/
1218
const _get = (url: string, config = {}) => {
1319
return apiClient.get(url, config)
1420
}
1521

22+
/**
23+
* Bungie post request
24+
* @param url
25+
* @param data request body
26+
* @param config axios config
27+
* @returns
28+
*/
1629
const _post = (url: string, data = {}, config = {}) => {
1730
return apiClient.post(url, data, config)
1831
}

src/lib/bungie_api/TokenService.ts

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ export function isTokenExpired(token?: Token) {
3434
return Date.now() > expiration
3535
}
3636

37-
function getTokenExpiration(token?: Token): number {
38-
return (token && 'acquired' in token && 'expires' in token) ? token.acquired + token.expires * 1000 : 0
37+
export function regenerateTokens(): boolean {
38+
39+
if (canTokensRefresh()) {
40+
generateToken(true)
41+
return true
42+
}
43+
44+
return false
3945
}
4046

4147
export function generateToken(refresh: boolean, authCode=""): Tokens | null {
@@ -68,6 +74,10 @@ export function generateToken(refresh: boolean, authCode=""): Tokens | null {
6874
return returnToken
6975
}
7076

77+
function getTokenExpiration(token?: Token): number {
78+
return (token && 'acquired' in token && 'expires' in token) ? token.acquired + token.expires * 1000 : 0
79+
}
80+
7181
function handleTokenResponse(response: AxiosResponse): Tokens {
7282
if (response.data.access_token) {
7383

@@ -98,14 +108,4 @@ function handleTokenResponse(response: AxiosResponse): Tokens {
98108
else {
99109
throw new Error(`Invalid response: ${JSON.stringify(response)}`)
100110
}
101-
}
102-
103-
export function regenerateTokens(): boolean {
104-
105-
if (canTokensRefresh()) {
106-
generateToken(true)
107-
return true
108-
}
109-
110-
return false
111111
}

src/lib/bungie_api/TokensStore.ts

+14
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,33 @@ export interface Tokens {
1414

1515
const key = 'authTokens'
1616

17+
/**
18+
* Gets the locally stored auth tokens
19+
* @returns the tokens or null
20+
*/
1721
export function getTokens(): Tokens | null {
1822
const tokenString = localStorage.getItem(key)
1923
return tokenString ? (JSON.parse(tokenString) as Tokens) : null
2024
}
2125

26+
/**
27+
* Stores auth tokens in local storage.
28+
*/
2229
export function setTokens(tokens: Tokens) {
2330
localStorage.setItem(key, JSON.stringify(tokens))
2431
}
2532

33+
/**
34+
* Removes the locally stored auth tokens
35+
*/
2636
export function removeTokens() {
2737
localStorage.removeItem(key)
2838
}
2939

40+
/**
41+
* Gets the membersip id from the locally stored auth tokens
42+
* @returns membershipId
43+
*/
3044
export function getMembershipId(): string | undefined {
3145
const tokens = getTokens()
3246

0 commit comments

Comments
 (0)