Skip to content

Commit

Permalink
Enabled localhost https. Fixed authenticated redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
dragoni7 committed Jun 24, 2024
1 parent 4742ca3 commit 0af5970
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dist
dist-ssr
*.local
.env
certs

# Editor directories and files
.vscode/*
Expand All @@ -23,3 +24,4 @@ dist-ssr
*.njsproj
*.sln
*.sw?
certs/*
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 14 additions & 11 deletions src/app/routes/Landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
14 changes: 5 additions & 9 deletions src/features/auth/AuthReturn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion src/lib/bungie_api/AuthService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/lib/bungie_api/TokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -7,7 +7,5 @@ const root = document.getElementById('root')
if (!root) throw new Error('no root element found')

createRoot(root).render(
<React.StrictMode>
<App />
</React.StrictMode>,
<App />
)
21 changes: 18 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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()],
}
}
})

0 comments on commit 0af5970

Please sign in to comment.