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
9 changes: 7 additions & 2 deletions src/with-auth.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthorizationOpts } from '@crossid/crossid-spa-js'
import React, { useEffect, useRef, useState } from 'react'
import React, { useEffect, useRef } from 'react'
import { useAuth } from './use-auth'

export interface AuthRequiredOpts {
Expand All @@ -9,6 +9,11 @@ export interface AuthRequiredOpts {

const defaultReturnTo = (): string => `${window.location.pathname}${window.location.search}`

const isExpired = (exp: number): boolean => {
const nowSecondsTS = Date.now() / 1000
return nowSecondsTS >= exp
}

/**
* a high order component that renders children only if user is authenticated.
* anonymous visitors will be redirected to the login page.
Expand All @@ -25,7 +30,7 @@ export function withAuth<T>(WrappedComponent: React.ComponentType<T>, opts: Auth
const { return_to = defaultReturnTo, login_opts } = opts

// todo consider authorization restrictions by letting the user pass some claims assertions.
const authenticated = !!idToken
const authenticated = !!idToken && !isExpired(idToken.exp || 0)

useEffect(() => {
rendered.current = true
Expand Down