Skip to content

⬅️ Back to Table of Contents

📄 useJwt

📊 Analysis Summary

Metric Count
🔧 Functions 2
📦 Imports 8
🟢 Vue Composition API 2
📐 Interfaces 2

📚 Table of Contents

🛠️ File Location:

📂 packages/integrations/useJwt/index.ts

📦 Imports

Name Source
JwtDecodeOptions jwt-decode
JwtHeader jwt-decode
JwtPayload jwt-decode
ComputedRef vue
MaybeRefOrGetter vue
jwtDecode jwt-decode
computed vue
toValue vue

Vue Composition API

Name Type Reactive Variables Composables
computed computed none none
computed computed none none

Functions

useJwt(encodedJwt: MaybeRefOrGetter<string>, options: UseJwtOptions<Fallback>): UseJwtReturn<Payload, Header, Fallback>

Reactive decoded jwt token.

See: https://vueuse.org/useJwt

Raw JSDoc
/**
 * Reactive decoded jwt token.
 *
 * @see https://vueuse.org/useJwt
 */

Calls:

  • jwtDecode (from jwt-decode)
  • onError
  • computed (from vue)
  • decodeWithFallback
  • toValue (from vue)
Code
export function useJwt<
  Payload extends object = JwtPayload,
  Header extends object = JwtHeader,
  Fallback = null,
>(
  encodedJwt: MaybeRefOrGetter<string>,
  options: UseJwtOptions<Fallback> = {},
): UseJwtReturn<Payload, Header, Fallback> {
  const {
    onError,
    fallbackValue = null,
  } = options

  const decodeWithFallback = <T extends object>(encodedJwt: string, options?: JwtDecodeOptions): T | Fallback => {
    try {
      return jwtDecode<T>(encodedJwt, options)
    }
    catch (err) {
      onError?.(err)
      return fallbackValue as Fallback
    }
  }

  const header = computed(() => decodeWithFallback<Header>(toValue(encodedJwt), { header: true }))
  const payload = computed(() => decodeWithFallback<Payload>(toValue(encodedJwt)))

  return {
    header,
    payload,
  }
}

Internal helpers

Declared inside another function in this file.

decodeWithFallback(encodedJwt: string, options: JwtDecodeOptions): T | Fallback

Parameters:

  • encodedJwt string
  • options JwtDecodeOptions

Returns: T | Fallback

Calls:

  • jwtDecode (from jwt-decode)
  • onError
Code
<T extends object>(encodedJwt: string, options?: JwtDecodeOptions): T | Fallback => {
    try {
      return jwtDecode<T>(encodedJwt, options)
    }
    catch (err) {
      onError?.(err)
      return fallbackValue as Fallback
    }
  }

Interfaces

UseJwtOptions<Fallback>

Interface Code
export interface UseJwtOptions<Fallback> {
  /**
   * Value returned when encounter error on decoding
   *
   * @default null
   */
  fallbackValue?: Fallback

  /**
   * Error callback for decoding
   */
  onError?: (error: unknown) => void
}

Properties

Name Type Optional Description
fallbackValue Fallback not shown
onError (error: unknown) => void not shown

UseJwtReturn<Payload, Header, Fallback>

Interface Code
export interface UseJwtReturn<Payload, Header, Fallback> {
  header: ComputedRef<Header | Fallback>
  payload: ComputedRef<Payload | Fallback>
}

Properties

Name Type Optional Description
header ComputedRef<Header \| Fallback> not shown
payload ComputedRef<Payload \| Fallback> not shown

Generated by Syntax Scribe