Skip to content

Commit

Permalink
fix: provide clientId to web component
Browse files Browse the repository at this point in the history
  • Loading branch information
saadjutt01 committed Apr 19, 2022
1 parent 8a3fe8b commit db70b1c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
19 changes: 11 additions & 8 deletions api/src/routes/web/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import { getWebBuildFolderPath } from '../../utils'

const webRouter = express.Router()

const codeToInject = `
const jsCodeForDesktopMode = `
<script>
localStorage.setItem('accessToken', JSON.stringify('accessToken'))
localStorage.setItem('refreshToken', JSON.stringify('refreshToken'))
</script>`

const jsCodeForServerMode = `
<script>
localStorage.setItem('CLIENT_ID', '${process.env.CLIENT_ID}')
</script>`

webRouter.get('/', async (_, res) => {
let content: string
try {
Expand All @@ -21,14 +26,12 @@ webRouter.get('/', async (_, res) => {
}

const { MODE } = process.env
if (MODE?.trim() !== 'server') {
const injectedContent = content.replace('</head>', `${codeToInject}</head>`)

res.setHeader('Content-Type', 'text/html')
return res.send(injectedContent)
}
const codeToInject =
MODE?.trim() === 'server' ? jsCodeForServerMode : jsCodeForDesktopMode
const injectedContent = content.replace('</head>', `${codeToInject}</head>`)

return res.send(content)
res.setHeader('Content-Type', 'text/html')
return res.send(injectedContent)
})

export default webRouter
5 changes: 2 additions & 3 deletions web/src/components/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ const Login = ({ setTokens, getCodeOnly }: any) => {
error = false
setErrorMessage('')
e.preventDefault()
let clientId = process.env.CLIENT_ID
let clientId = process.env.CLIENT_ID ?? localStorage.getItem('CLIENT_ID')

if (getCodeOnly) {
const params = new URLSearchParams(location.search)
const responseType = params.get('response_type')
if (responseType === 'code')
clientId = params.get('client_id') ?? undefined
if (responseType === 'code') clientId = params.get('client_id')
}

const { code } = await getAuthCode({
Expand Down

0 comments on commit db70b1c

Please sign in to comment.