Skip to content

Commit

Permalink
Expose authenticator context (#6)
Browse files Browse the repository at this point in the history
* Updated MakeAuth to expose the full React context.

**[Breaking Change]**
* Changed UserData export to include both Provider and Consumer
  • Loading branch information
FizzBuzz791 authored and thchia committed Jan 4, 2019
1 parent 7710847 commit 10039f6
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ The `Callback` component will call the `.signinRedirectCallback()` method from `

### `<UserData />`

This component exposes the data of the authenticated user. If you are familiar with React's Context API (the official v16.3.x one), this component is just a `Consumer`.
This component exposes the data of the authenticated user. If you are familiar with React's Context API (the official v16.3.x one), this component is just a `Context`.

```jsx
<UserData>{context => <p>{context.user.id_token}</p>}</UserData>
<UserData.Consumer>{context => <p>{context.user.id_token}</p>}</UserData.Consumer>
```

Render prop function
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Consumer as UserData, makeAuthenticator } from './makeAuth'
import { AuthenticatorContext as UserData, makeAuthenticator } from './makeAuth'
import makeUserManager from './makeUserManager'
import Callback from './Callback'

Expand Down
4 changes: 2 additions & 2 deletions src/makeAuth/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import { fireEvent, render, waitForElement } from 'react-testing-library'

import { Consumer, makeAuthenticator } from './'
import { AuthenticatorContext, makeAuthenticator } from './'
import makeUserManager from '../makeUserManager'
import MockUserManager from '../utils/userManager'

Expand Down Expand Up @@ -83,7 +83,7 @@ describe('makeAuthenticator', () => {

const WithAuth = makeAuthenticator({
userManager: makeUserManager(userManagerConfig, MockUserManager)
})(<Consumer>{({ signOut }) => <Logout signOut={signOut} />}</Consumer>)
})(<AuthenticatorContext.Consumer>{({ signOut }) => <Logout signOut={signOut} />}</AuthenticatorContext.Consumer>)
const { getByText, queryByText } = render(<WithAuth />)

await successfulGetUser
Expand Down
6 changes: 3 additions & 3 deletions src/makeAuth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const DEFAULT_CONTEXT: IAuthenticatorContext = {
user: null,
userManager: null
}
const { Consumer, Provider } = React.createContext<IAuthenticatorContext>(
const AuthenticatorContext = React.createContext<IAuthenticatorContext>(
DEFAULT_CONTEXT
)

Expand Down Expand Up @@ -93,7 +93,7 @@ function makeAuthenticator({
return placeholderComponent || null
}
return this.isValid() ? (
<Provider value={this.state.context}>{WrappedComponent}</Provider>
<AuthenticatorContext.Provider value={this.state.context}>{WrappedComponent}</AuthenticatorContext.Provider>
) : (
<RedirectToAuth
userManager={this.userManager}
Expand All @@ -106,4 +106,4 @@ function makeAuthenticator({
}
}
}
export { Consumer, makeAuthenticator }
export { AuthenticatorContext, makeAuthenticator }

0 comments on commit 10039f6

Please sign in to comment.