fix app.tsx no –crypto.randomUUID is not a function error

This commit is contained in:
Mabe
2026-06-21 16:50:53 +02:00
parent a92a67c8a7
commit 848d593410
+16 -1
View File
@@ -39,6 +39,21 @@ async function createCodeVerifier() {
return base64UrlEncode(array) return base64UrlEncode(array)
} }
function generateUUID() {
// Use native crypto.randomUUID when available, otherwise fallback to RFC4122 v4
// Compatible with older browsers that lack crypto.randomUUID
if (typeof crypto !== 'undefined' && 'randomUUID' in crypto) {
// @ts-ignore
return (crypto as any).randomUUID()
}
const bytes = crypto.getRandomValues(new Uint8Array(16))
bytes[6] = (bytes[6] & 0x0f) | 0x40
bytes[8] = (bytes[8] & 0x3f) | 0x80
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('')
return `${hex.substr(0, 8)}-${hex.substr(8, 4)}-${hex.substr(12, 4)}-${hex.substr(16, 4)}-${hex.substr(20, 12)}`
}
function getLocalToken() { function getLocalToken() {
return window.localStorage.getItem('oidc_access_token') return window.localStorage.getItem('oidc_access_token')
} }
@@ -137,7 +152,7 @@ function App() {
async function login() { async function login() {
try { try {
const config = await fetchOIDCConfig() const config = await fetchOIDCConfig()
const state = crypto.randomUUID() const state = generateUUID()
const codeVerifier = await createCodeVerifier() const codeVerifier = await createCodeVerifier()
const codeChallenge = await sha256(codeVerifier) const codeChallenge = await sha256(codeVerifier)