-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
188 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect } from "react" | ||
import { useNavigate } from "react-router-dom" | ||
import { handleAuthReturn } from "../../features/auth/AuthReturn" | ||
|
||
export const ReturnRoute = () => { | ||
|
||
const navigate = useNavigate() | ||
|
||
useEffect( () => { | ||
|
||
if (handleAuthReturn()) { | ||
navigate('/') | ||
} | ||
|
||
}, []) | ||
|
||
return ( | ||
<div> | ||
Authentication Error | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { generateToken } from "../../lib/bungie_api/TokenService" | ||
import { setTokens } from "../../lib/bungie_api/TokensStore" | ||
|
||
function getAuthCodeFromURL(): string | null { | ||
return window.location.href.includes("code=") ? window.location.href.split('code=')[1] : null | ||
} | ||
|
||
export function handleAuthReturn(): boolean { | ||
|
||
const code = getAuthCodeFromURL() | ||
|
||
if (!code?.length) { | ||
console.log("Could not find authorization code") | ||
return false | ||
} | ||
|
||
try { | ||
const tokens = generateToken(false, code) | ||
|
||
if (tokens) { | ||
setTokens(tokens) | ||
|
||
return true | ||
} | ||
|
||
} | ||
catch (error) { | ||
console.log(error) | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { Token, Tokens, getTokens, setTokens } from "./TokensStore" | ||
import { _post } from "./BungieApiClient" | ||
import { CLIENT_ID, CLIENT_SECRET } from "./utils" | ||
import { AxiosResponse } from "axios" | ||
|
||
export function canTokensRefresh() { | ||
const tokens = getTokens() | ||
|
||
if (!tokens) { | ||
return false | ||
} | ||
|
||
return tokens && !isTokenExpired(tokens.refreshToken) | ||
} | ||
|
||
export function expireTokens() { | ||
const tokens = getTokens() | ||
|
||
if (tokens) { | ||
tokens.accessToken.acquired = 0 | ||
tokens.accessToken.expires = 0 | ||
setTokens(tokens) | ||
} | ||
} | ||
|
||
export function isTokenExpired(token?: Token) { | ||
|
||
if (!token) { | ||
return true | ||
} | ||
|
||
const expiration = getTokenExpiration(token) | ||
|
||
return Date.now() > expiration | ||
} | ||
|
||
function getTokenExpiration(token?: Token): number { | ||
return (token && 'acquired' in token && 'expires' in token) ? token.acquired + token.expires * 1000 : 0 | ||
} | ||
|
||
export function generateToken(refresh: boolean, authCode=""): Tokens | null { | ||
const REFRESH_TOKEN = getTokens()?.refreshToken | ||
var returnToken = null | ||
|
||
let body = refresh === false ? | ||
`grant_type=authorization_code&code=${authCode}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}` : | ||
`grant_type=refresh_token&refresh_token=${REFRESH_TOKEN}&client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}` | ||
|
||
_post('/Platform/App/OAuth/Token/', body, { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
'Authorization': `Basic ${window.btoa(`${CLIENT_ID}:${CLIENT_SECRET}`)}` | ||
} | ||
}) | ||
.then(response => { | ||
|
||
returnToken = handleTokenResponse(response) | ||
|
||
if (refresh) { | ||
setTokens(returnToken) | ||
console.log("Tokens successfully generated") | ||
} | ||
}) | ||
.catch(error => { | ||
throw new Error(error) | ||
}) | ||
|
||
return returnToken | ||
} | ||
|
||
function handleTokenResponse(response: AxiosResponse): Tokens { | ||
if (response.data.access_token) { | ||
|
||
const aquired = Date.now() | ||
|
||
const accessToken: Token = { | ||
value: response.data.access_token, | ||
expires: response.data.expires_in, | ||
name: 'access', | ||
acquired: aquired | ||
} | ||
|
||
const refreshToken: Token = { | ||
value: response.data.refresh_token, | ||
expires: response.data.refresh_expires_in, | ||
name: 'refresh', | ||
acquired: aquired | ||
} | ||
|
||
const tokens: Tokens = { | ||
accessToken, | ||
refreshToken, | ||
membershipId: response.data.membership_id | ||
} | ||
|
||
return tokens | ||
} | ||
else { | ||
throw new Error(`Invalid response: ${JSON.stringify(response)}`) | ||
} | ||
} | ||
|
||
export function regenerateTokens(): boolean { | ||
|
||
if (canTokensRefresh()) { | ||
generateToken(true) | ||
return true | ||
} | ||
|
||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
export const API_KEY = import.meta.env.VITE_API_KEY | ||
|
||
export const CLIENT_ID = import.meta.env.VITE_CLIENT_ID | ||
|
||
export const CLIENT_SECRET = import.meta.env.VITE_CLIENT_SECRET |