Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions packages/core/auth-js/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ export function expiresAt(expiresIn: number) {
return timeNow + expiresIn
}

export function uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
export function uuid(): string {
const bytes = new Uint8Array(16)
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
crypto.getRandomValues(bytes)
} else {
const cryptoNode = require('crypto')
cryptoNode.randomFillSync(bytes)
}
bytes[6] = (bytes[6] & 0x0f) | 0x40
bytes[8] = (bytes[8] & 0x3f) | 0x80

let i = 0
return bytes.reduce((uuid, byte) => {
const hex = byte.toString(16).padStart(2, '0')
if (i === 4 || i === 6 || i === 8 || i === 10) uuid += '-'
i++
return uuid + hex
}, '')
}

export const isBrowser = () => typeof window !== 'undefined' && typeof document !== 'undefined'
Expand Down