-
Notifications
You must be signed in to change notification settings - Fork 0
qr scanner for the login #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
beny25585
wants to merge
9
commits into
main
Choose a base branch
from
feature/add-qr-login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4297481
qr scanner for the login
beny25585 9f528f3
add mock QR login flow for patients
beny25585 5855ad7
coderabit fixs
beny25585 5bedf71
fix pr comments
beny25585 44f1e4c
move the styles to styles.ts and realability fixes
beny25585 7dc1a50
added full screen view, onClose
beny25585 dc7e432
fix the yamal file for the prritier
beny25585 d5d27ff
Extract QR login logic into useQrLogin hook
beny25585 fab534e
addres the comment and change the inline close button to the constant…
beny25585 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { useState, useCallback } from 'react' | ||
| import { useTranslation } from 'react-i18next' | ||
| import { useNavigate } from 'react-router-dom' | ||
| import { createToken, verifyToken } from '@/services/qrService' | ||
| import { extractQrToken } from '@/utils/qrUtils' | ||
| import { CANT_GET_TOKEN, NAV_ROUTES } from '@/components/NavBar/constants' | ||
|
|
||
| export const useQrLogin = () => { | ||
| const { t } = useTranslation() | ||
| const navigate = useNavigate() | ||
| const [scannerOpen, setScannerOpen] = useState(false) | ||
| const [scannerError, setScannerError] = useState<string | null>(null) | ||
| const [qrToken, setQrToken] = useState<string>() | ||
|
|
||
| const openScanner = useCallback(() => { | ||
| setScannerError(null) | ||
| setScannerOpen(true) | ||
| }, []) | ||
|
|
||
| const closeScanner = useCallback(() => { | ||
| setScannerOpen(false) | ||
| }, []) | ||
|
|
||
| const generateQrToken = useCallback((userId: string) => { | ||
| const token = createToken(userId) | ||
| setQrToken(token) | ||
| }, []) | ||
|
|
||
| const handleQrSuccess = useCallback( | ||
| async (result: string) => { | ||
| try { | ||
| const token = extractQrToken(result) | ||
| if (!token) { | ||
| throw new Error(CANT_GET_TOKEN) | ||
| } | ||
| await verifyToken(token) | ||
| navigate(NAV_ROUTES.home) | ||
| } catch { | ||
| setScannerError(t('login.qrLoginFailed')) | ||
| setScannerOpen(false) | ||
| } | ||
| }, | ||
| [navigate, t], | ||
| ) | ||
|
|
||
| const handleBackToLogin = useCallback(() => { | ||
| setQrToken(undefined) | ||
| }, []) | ||
|
|
||
| return { | ||
| scannerOpen, | ||
| scannerError, | ||
| qrToken, | ||
| openScanner, | ||
| closeScanner, | ||
| generateQrToken, | ||
| handleQrSuccess, | ||
| handleBackToLogin, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { Html5Qrcode } from 'html5-qrcode' | ||
| import { useEffect, useState } from 'react' | ||
| import { useTranslation } from 'react-i18next' | ||
| import * as styles from './styles' | ||
| import { SGLCloseIcon } from '@/components/UI/Icons/CloseIcon/SGLCloseIcon' | ||
|
|
||
| interface QRScannerProps { | ||
| onSuccess: (value: string) => void | ||
| onClose?: () => void | ||
| } | ||
|
|
||
| export const QRScanner = ({ onSuccess, onClose }: QRScannerProps) => { | ||
| const { t } = useTranslation() | ||
| const [loading, setLoading] = useState(true) | ||
| const [error, setError] = useState<string | null>(null) | ||
|
|
||
| useEffect(() => { | ||
|
beny25585 marked this conversation as resolved.
|
||
| const qr = new Html5Qrcode('reader') | ||
| let stopped = false | ||
| let started = false | ||
|
|
||
| const handleDecode = async (decodedText: string) => { | ||
| if (stopped) return | ||
| stopped = true | ||
| await qr.stop().catch(() => undefined) | ||
| onSuccess(decodedText) | ||
| } | ||
|
|
||
| const startScanner = async () => { | ||
| try { | ||
| await qr.start( | ||
| { facingMode: 'environment' }, | ||
| { fps: 20, qrbox: 250 }, | ||
| handleDecode, | ||
| () => {}, | ||
| ) | ||
| started = true | ||
| setLoading(false) | ||
| } catch (err) { | ||
| setLoading(false) | ||
| setError((err as Error)?.message || t('login.cameraPermission')) | ||
| } | ||
| } | ||
| startScanner() | ||
|
|
||
| return () => { | ||
| if (started && !stopped) { | ||
| qr.stop().catch(() => undefined) | ||
| } | ||
| } | ||
| }, []) | ||
|
|
||
| return ( | ||
| <div style={styles.scannerContainer}> | ||
| <div id="reader" style={styles.scannerReader} /> | ||
| {onClose && <SGLCloseIcon onClick={onClose} styles={styles.closeIcon} />} | ||
| {loading && !error && <div style={styles.loadingOverlay}>{t('login.cameraPermission')}</div>} | ||
| {error && <div style={styles.loadingOverlay}>{error}</div>} | ||
| </div> | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| export const QR_TOKEN_PARAM = 'token' | ||
|
|
||
| export const buildQrLoginUrl = (token: string) => | ||
| `${window.location.origin}/qr-login?token=${encodeURIComponent(token)}` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import type { CSSProperties } from '@mui/material' | ||
| import { theme } from '@/theme' | ||
|
|
||
| export const scannerContainer: CSSProperties = { | ||
| position: 'fixed', | ||
| inset: 0, | ||
| zIndex: 2, | ||
| backgroundColor: theme.palette.common.black, | ||
| } | ||
|
|
||
| export const scannerReader: CSSProperties = { | ||
| width: '100%', | ||
| height: '100%', | ||
| } | ||
|
|
||
| export const loadingOverlay: CSSProperties = { | ||
| position: 'absolute', | ||
| inset: 0, | ||
| display: 'flex', | ||
| alignItems: 'center', | ||
| justifyContent: 'center', | ||
| color: theme.palette.common.white, | ||
| zIndex: 2, | ||
| } | ||
|
|
||
| export const closeIcon: CSSProperties = { | ||
| position: 'absolute', | ||
| top: 16, | ||
| right: 16, | ||
| zIndex: 2, | ||
| color: theme.palette.common.white, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| const tokenStore = new Map<string, string>() | ||
|
|
||
| export const createToken = (userId: string): string => { | ||
| const token = crypto.randomUUID() | ||
| tokenStore.set(token, userId) | ||
| return token | ||
| } | ||
|
|
||
| export const verifyToken = async (token: string): Promise<{ userId: string }> => { | ||
| const userId = tokenStore.get(token) | ||
| tokenStore.delete(token) | ||
| if (!userId) { | ||
| throw new Error('invalid token') | ||
| } | ||
| return { userId } | ||
| } | ||
|
GilHeller marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { QR_TOKEN_PARAM } from '@/pages/login/QRLogin/constants' | ||
|
|
||
| export const extractQrToken = (qrContent: string): string | null => { | ||
| const url = new URL(qrContent) | ||
| return url.searchParams.get(QR_TOKEN_PARAM) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.