Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 10 additions & 28 deletions src/withRequiredAuthInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import hoistNonReactStatics from "hoist-non-react-statics"
import React, { useContext } from "react"
import { Subtract } from "utility-types"
import { AuthContext } from "./AuthContext"
import { getOrgHelper } from "./OrgHelper"
import { RedirectToLogin } from "./useRedirectFunctions"
import { WithLoggedInAuthInfoProps } from "./withAuthInfo"
import { withAuthInfo } from "./withAuthInfo"

export interface WithRequiredAuthInfoArgs {
displayWhileLoading?: React.ReactElement
Expand All @@ -15,22 +15,12 @@ export function withRequiredAuthInfo<P extends WithLoggedInAuthInfoProps>(
Component: React.ComponentType<P>,
args?: WithRequiredAuthInfoArgs
): React.ComponentType<Subtract<P, WithLoggedInAuthInfoProps>> {
const displayName = `withRequiredAuthInfo(${Component.displayName || Component.name || "Component"})`

const WithRequiredAuthInfoWrapper = (props: Subtract<P, WithLoggedInAuthInfoProps>) => {
const WithRequiredAuthInfoWrapper = (props: Subtract<P, WithLoggedInAuthInfoProps>) => {
const context = useContext(AuthContext)
if (context === undefined) {
throw new Error("withRequiredAuthInfo must be used within an AuthProvider or RequiredAuthProvider")
}

function displayLoading() {
if (args && args.displayWhileLoading) {
return args.displayWhileLoading
} else {
return <React.Fragment />
}
}

function displayLoggedOut() {
if (args && args.displayIfLoggedOut) {
return args.displayIfLoggedOut
Expand All @@ -39,25 +29,17 @@ export function withRequiredAuthInfo<P extends WithLoggedInAuthInfoProps>(
}
}

const { loading, authInfo, selectOrgId, userSelectedOrgId } = context
if (loading) {
return displayLoading()
} else if (authInfo) {
const orgHelper = getOrgHelper(authInfo.orgHelper, selectOrgId, userSelectedOrgId)
const loggedInProps: P = {
...(props as P),
accessToken: authInfo.accessToken,
isLoggedIn: !!authInfo.accessToken,
orgHelper: orgHelper,
user: authInfo.user,
}
return <Component {...loggedInProps} />
const { authInfo } = context
if (!authInfo) {
return displayLoggedOut()
} else {
return displayLoggedOut()
const ComponentWithAuthInfo = withAuthInfo(Component, args)
return <ComponentWithAuthInfo {...props} />
}

}
WithRequiredAuthInfoWrapper.displayName = displayName
WithRequiredAuthInfoWrapper.WrappedComponent = Component

WithRequiredAuthInfoWrapper.displayName = `withRequiredAuthInfo(${Component.displayName || Component.name || "Component"})`
WithRequiredAuthInfoWrapper.WrappedComponent = Component
return hoistNonReactStatics(WithRequiredAuthInfoWrapper, Component)
}