Skip to content

Commit

Permalink
90 add qr code component (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
royscheeren authored Feb 26, 2024
2 parents 3edfcc7 + f8fc60a commit b674149
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/oidc-client/src/App.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ p {
align-items: center;
margin-top: 1rem;
background-color: #ffffff;
padding: 1rem;
padding: 2rem 1rem;
width: 100%;
max-width: 20rem;
}
Expand Down
37 changes: 29 additions & 8 deletions apps/oidc-client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { useState } from 'react'
import ReactQRCode from 'react-qr-code'

import styles from './App.module.css'
import { getOpenIdConnectUrl } from './utils'

const QRCode = ReactQRCode as any

function App() {
const urlSearchParams = new URLSearchParams(window.location.search)
const [error, setError] = useState<string | null>(null)
const [qrCodeValue, setQrCodeValue] = useState<string | null>(null)

try {
const url = getOpenIdConnectUrl(urlSearchParams)
setQrCodeValue(url)
} catch (e: any) {
setQrCodeValue(null)
setError(e.message)
}

return (
<div class={styles.App}>
<header class={styles.header}>
Expand All @@ -12,14 +30,17 @@ function App() {
<p>
Sign in with your verified credentials to continue to <strong>Envited Data Space</strong>
</p>

<div class={`${styles.qrCodeContainer} ${styles.shadow}`}>
<img src="/qrcode.png" alt="QR code" />
</div>
<div class={styles.error}>
<h3 class={styles.errorTitle}>Error</h3>
<p>Something went wrong</p>
</div>
{qrCodeValue && (
<div class={`${styles.qrCodeContainer} ${styles.shadow}`}>
<QRCode value={qrCodeValue} />
</div>
)}
{error && (
<div class={styles.error}>
<h3 class={styles.errorTitle}>Error</h3>
<p>{error}</p>
</div>
)}
</main>
</div>
)
Expand Down
1 change: 1 addition & 0 deletions apps/oidc-client/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { getOpenIdConnectUrl } from './utils'
32 changes: 32 additions & 0 deletions apps/oidc-client/src/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as SUT from './utils'

describe('src/utils', () => {
describe('getOpenIdConnectUrl', () => {
it('should get the open id connect url as expected', () => {
// when ... we want to get the open id connect url from the url search params
// then ... it should get the url as expected
const params = new URLSearchParams()
params.set('client_id', 'CLIENT_ID')
params.set('external_url', 'EXTERNAL_URL')
params.set('login_id', 'LOGIN_ID')

const result = SUT.getOpenIdConnectUrl(params)

expect(result).toEqual(
'openid-vc://?client_id=CLIENT_ID&request_uri=EXTERNAL_URL%2Fapi%2FpresentCredential%3Flogin_id%3DLOGIN_ID',
)
})

it('should throw an error when not all url params are set', () => {
// when ... we want to get the open id connect url from the url search params
// then ... it should get the url as expected
const params = new URLSearchParams()
params.set('client_id', 'CLIENT_ID')
params.set('external_url', 'EXTERNAL_URL')

const result = () => SUT.getOpenIdConnectUrl(params)

expect(result).toThrowError('Missing required parameters')
})
})
})
13 changes: 13 additions & 0 deletions apps/oidc-client/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const getOpenIdConnectUrl = (urlSearchParams: URLSearchParams) => {
const clientId = urlSearchParams.get('client_id')
const externalUrl = urlSearchParams.get('external_url')
const loginId = urlSearchParams.get('login_id')

if (!clientId || !externalUrl || !loginId) {
throw new Error('Missing required parameters')
}

return `openid-vc://?client_id=${clientId}&request_uri=${encodeURIComponent(
externalUrl + '/api/presentCredential?login_id=' + loginId,
)}`
}
1 change: 0 additions & 1 deletion apps/oidc-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'
import preact from '@preact/preset-vite'
import { defineConfig, searchForWorkspaceRoot } from 'vite'

console.log(searchForWorkspaceRoot(process.cwd()))
export default defineConfig({
cacheDir: '../../node_modules/.vite/oidc-client',

Expand Down
29 changes: 25 additions & 4 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 @@ -27,6 +27,7 @@
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.50.1",
"react-qr-code": "^2.0.12",
"react-toastify": "^10.0.4",
"ts-pattern": "^5.0.6",
"tslib": "^2.3.0",
Expand Down

0 comments on commit b674149

Please sign in to comment.