From 0af59708b7554edf5bc781c72a03b6a8665bb9eb Mon Sep 17 00:00:00 2001 From: dragoni7 Date: Mon, 24 Jun 2024 12:23:48 -0700 Subject: [PATCH] Enabled localhost https. Fixed authenticated redirect --- .gitignore | 2 ++ package-lock.json | 31 ++++++++++++++++++++++++++++++ package.json | 1 + src/app/routes/Landing.tsx | 25 +++++++++++++----------- src/features/auth/AuthReturn.tsx | 14 +++++--------- src/lib/bungie_api/AuthService.tsx | 6 +++++- src/lib/bungie_api/TokenService.ts | 5 +---- src/main.tsx | 6 ++---- vite.config.ts | 21 +++++++++++++++++--- 9 files changed, 79 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index 438657a..bfc27b3 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ dist dist-ssr *.local .env +certs # Editor directories and files .vscode/* @@ -23,3 +24,4 @@ dist-ssr *.njsproj *.sln *.sw? +certs/* diff --git a/package-lock.json b/package-lock.json index 0987408..6e6a490 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,6 +19,7 @@ "react-router-dom": "^6.23.1" }, "devDependencies": { + "@types/node": "^20.14.8", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "@vitejs/plugin-react": "^4.2.1", @@ -1620,6 +1621,15 @@ "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", "dev": true }, + "node_modules/@types/node": { + "version": "20.14.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.8.tgz", + "integrity": "sha512-DO+2/jZinXfROG7j7WKFn/3C6nFwxy2lLpgLjEXJz+0XKphZlTLJ14mo8Vfg8X5BWN6XjyESXq+LcYdT7tR3bA==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, "node_modules/@types/parse-json": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz", @@ -4807,6 +4817,12 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "node_modules/update-browserslist-db": { "version": "1.0.16", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.16.tgz", @@ -6004,6 +6020,15 @@ "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", "dev": true }, + "@types/node": { + "version": "20.14.8", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.8.tgz", + "integrity": "sha512-DO+2/jZinXfROG7j7WKFn/3C6nFwxy2lLpgLjEXJz+0XKphZlTLJ14mo8Vfg8X5BWN6XjyESXq+LcYdT7tR3bA==", + "dev": true, + "requires": { + "undici-types": "~5.26.4" + } + }, "@types/parse-json": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/@types/parse-json/-/parse-json-4.0.2.tgz", @@ -8261,6 +8286,12 @@ "which-boxed-primitive": "^1.0.2" } }, + "undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + }, "update-browserslist-db": { "version": "1.0.16", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.0.16.tgz", diff --git a/package.json b/package.json index a88e823..a32c5f2 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "react-router-dom": "^6.23.1" }, "devDependencies": { + "@types/node": "^20.14.8", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "@vitejs/plugin-react": "^4.2.1", diff --git a/src/app/routes/Landing.tsx b/src/app/routes/Landing.tsx index 1011323..5967102 100644 --- a/src/app/routes/Landing.tsx +++ b/src/app/routes/Landing.tsx @@ -9,19 +9,22 @@ import { isAuthenticated } from '../../lib/bungie_api/AuthService' export const LandingRoute = () => { const navigate = useNavigate() + useEffect( () => { - if (isAuthenticated()) { - console.log("Already authenticated") - navigate('/app') - } - else if (regenerateTokens()) { - console.log("Token regenerated and authenticated") - navigate('/app') - } - - console.log("Not authenticated") - + setTimeout(()=>{ + if (isAuthenticated()) { + console.log("Already authenticated") + navigate('/app') + } + else if (regenerateTokens()) { + console.log("Tokens regenerated and authenticated") + navigate('/app') + } + else { + console.log("Not authenticated") + } + }, 50) }, []) return ( diff --git a/src/features/auth/AuthReturn.tsx b/src/features/auth/AuthReturn.tsx index d742002..b9c68c8 100644 --- a/src/features/auth/AuthReturn.tsx +++ b/src/features/auth/AuthReturn.tsx @@ -13,24 +13,20 @@ export function handleAuthReturn(): boolean { const code = getAuthCodeFromURL() + console.log("auth code is " + code) + if (!code?.length) { console.log("Could not find authorization code") return false } try { - const tokens = generateToken(false, code) - - if (tokens) { - setTokens(tokens) - - return true - } - + generateToken(false, code) } catch (error) { console.log(error) + return false } - return false + return true } \ No newline at end of file diff --git a/src/lib/bungie_api/AuthService.tsx b/src/lib/bungie_api/AuthService.tsx index 53e5c73..4b6e5d1 100644 --- a/src/lib/bungie_api/AuthService.tsx +++ b/src/lib/bungie_api/AuthService.tsx @@ -18,7 +18,11 @@ export function isAuthenticated(): boolean { const tokens = getTokens() - return !tokens ? false : !isTokenExpired(tokens.accessToken) + if (!tokens) { + return false + } + + return !isTokenExpired(tokens.accessToken) } /** diff --git a/src/lib/bungie_api/TokenService.ts b/src/lib/bungie_api/TokenService.ts index c0a3bb9..64a34ec 100644 --- a/src/lib/bungie_api/TokenService.ts +++ b/src/lib/bungie_api/TokenService.ts @@ -62,10 +62,7 @@ export function generateToken(refresh: boolean, authCode=""): Tokens | null { returnToken = handleTokenResponse(response) - if (refresh) { - setTokens(returnToken) - console.log("Tokens successfully generated") - } + setTokens(returnToken) }) .catch(error => { throw new Error(error) diff --git a/src/main.tsx b/src/main.tsx index 9aace90..68a8c15 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,4 +1,4 @@ -import * as React from 'react' +import React from 'react' import { createRoot } from 'react-dom/client' import App from './app/App' import './index.css' @@ -7,7 +7,5 @@ const root = document.getElementById('root') if (!root) throw new Error('no root element found') createRoot(root).render( - - - , + ) diff --git a/vite.config.ts b/vite.config.ts index 5a33944..88ad121 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,22 @@ import { defineConfig } from 'vite' import react from '@vitejs/plugin-react' +import fs from 'fs' -// https://vitejs.dev/config/ -export default defineConfig({ - plugins: [react()], +export default defineConfig(({ command, mode, isSsrBuild, isPreview }) => { + if (command === 'serve') { + return { + plugins: [react()], + server: { + https: { + key: fs.readFileSync('./certs/localhost.key'), + cert: fs.readFileSync('./certs/localhost.crt') + } + } + } + } + else { + return { + plugins: [react()], + } + } })