Skip to content

Commit

Permalink
sebankid/qr: fallback to button element if QR cannot be rendered (#34)
Browse files Browse the repository at this point in the history
* sebankid/qr: fallback to button element if QR cannot be rendered

* sebankid:same-device: proper fallback
  • Loading branch information
mickhansen authored Aug 23, 2024
1 parent a338771 commit 4a5c2ed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
23 changes: 13 additions & 10 deletions src/components/AuthMethodSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,16 @@ type SwedenProps = Omit<AuthMethodSelectorProps, 'acrValues'> & {
acrValues: string[]
}

const QR_ACR_VALUE = 'urn:grn:authn:se:bankid:another-device:qr';
export function Sweden(props: SwedenProps) {
const {acrValues} = props;
const {action, uiLocales} = useContext(CriiptoVerifyContext);
const language = props.language || uiLocales as Language || 'en';
const hasQR = acrValues.includes(QR_ACR_VALUE);
const filteredAcrValues = acrValues.filter(s => s !== QR_ACR_VALUE);
const hasQR = acrValues.includes(SEBankIDQrCode.acr_values);
const filteredAcrValues = acrValues.filter(s => s !== SEBankIDQrCode.acr_values);

const userAgent = getUserAgent(typeof navigator !== 'undefined' ? navigator.userAgent : props.userAgent);
const mobileOS = userAgent?.os.name === 'iOS' ? 'iOS' : userAgent?.os.name === 'Android' ? 'android' : null;
const [showQR, setShowQR] = useState(hasQR && !mobileOS);
const showQR = hasQR && !mobileOS;

return (
<div className="criipto-eid-selector">
Expand All @@ -81,14 +80,18 @@ export function Sweden(props: SwedenProps) {
/>
))}
{showQR ? (
<SEBankIDQrCode language={language} redirectUri={props.redirectUri} />
<SEBankIDQrCode
language={language}
redirectUri={props.redirectUri}
fallback={(
<AuthMethodButton
acrValue={SEBankIDQrCode.acr_values}
/>
)}
/>
) : (hasQR && mobileOS) ? (
<AuthMethodButton
acrValue={QR_ACR_VALUE}
onClick={(event) => {
event.preventDefault();
setShowQR(true);
}}
acrValue={SEBankIDQrCode.acr_values}
/>
) : null}
</AuthButtonGroup>
Expand Down
17 changes: 11 additions & 6 deletions src/components/SEBankIDQRCode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import CriiptoVerifyContext from "../context";

import logo from './SEBankIDQRCode/[email protected]';
import './SEBankIDQRCode/SEBankIDQRCode.css';
import AuthMethodButton from "./AuthMethodButton";

interface Props {
redirectUri?: string,
Expand All @@ -28,7 +29,10 @@ interface Props {
isCompleting: boolean
error: OAuth2Error | Error | null
retry: () => void
}) => React.ReactElement
}) => React.ReactElement

/** Render fallback element while loading */
fallback?: React.ReactElement
}

interface QrResponse {
Expand All @@ -47,11 +51,6 @@ function searchParamsToPOJO(input: URLSearchParams) {
}, {});
}

// const logo = new Image();
// logo.src = logoSrc;

// const LOGO_RATIO = 0.15;

export default function SEBankIDQrCode(props: Props) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const wrapperRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -180,6 +179,10 @@ export default function SEBankIDQrCode(props: Props) {
</div>
);

if (!qrCode && props.fallback) {
return props.fallback;
}

if (props.children) {
return props.children({
qrElement,
Expand All @@ -193,6 +196,8 @@ export default function SEBankIDQrCode(props: Props) {
return qrElement;
}

SEBankIDQrCode.acr_values = 'urn:grn:authn:se:bankid:another-device:qr';

type UseDrawOptions = {
width?: number,
canvas: HTMLCanvasElement | null,
Expand Down
1 change: 0 additions & 1 deletion src/components/SEBankIDSameDeviceButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export default function SEBankIDSameDeviceButton(props: Props) {
})
.catch(err => {
setInitiated(false);
setError(err?.toString());
});
}, [buildAuthorizeUrl, redirectUri, strategy]);

Expand Down

0 comments on commit 4a5c2ed

Please sign in to comment.