Skip to content

Commit

Permalink
[default-login] Children can be a function that passes the user to th…
Browse files Browse the repository at this point in the history
…e wrapped content (#288)
  • Loading branch information
skogsmaskin authored and bjoerge committed Oct 19, 2017
1 parent a756339 commit 245bbf3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
14 changes: 9 additions & 5 deletions packages/@sanity/default-login/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# @sanity/default-login

Lets the user log in to Sanity.
Let a user log into Sanity, and get access to the child content.

```
<LoginWrapper>
{user => <Layout><div>{user.displayName} is logged in!</div></Layout>}
</LoginWrapper>
```

## Props

Expand All @@ -10,16 +16,14 @@ static propTypes = {
children: PropTypes.node.isRequired,
title: PropTypes.string,
description: PropTypes.string,
sanityLogo: PropTypes.node,
showSanityLogo: PropTypes.bool
sanityLogo: PropTypes.node
}
```

```
static defaultProps = {
title: 'Choose login provider',
description: null,
sanityLogo: null,
showSanityLogo: true
sanityLogo: <SanityStudioLogo />,
}
```
10 changes: 6 additions & 4 deletions packages/@sanity/default-login/src/LoginWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ const projectId = (isProjectLogin && client.config().projectId)
export default class LoginWrapper extends React.PureComponent {

static propTypes = {
children: PropTypes.node.isRequired,
children: PropTypes.oneOfType([
PropTypes.node,
PropTypes.func
]).isRequired,
title: PropTypes.string,
description: PropTypes.string,
sanityLogo: PropTypes.node
Expand Down Expand Up @@ -51,7 +54,7 @@ export default class LoginWrapper extends React.PureComponent {

render() {
const {error, user, isLoading} = this.state

const {children} = this.props
if (isLoading) {
return <Spinner fullscreen center />
}
Expand All @@ -67,7 +70,6 @@ export default class LoginWrapper extends React.PureComponent {
title={this.props.title}
description={this.props.description}
sanityLogo={this.props.sanityLogo}
showSanityLogo={this.props.showSanityLogo}
projectId={projectId}
/>
</CookieTest>
Expand All @@ -78,6 +80,6 @@ export default class LoginWrapper extends React.PureComponent {
return <UnauthorizedUser user={user} />
}

return this.props.children
return typeof children === 'function' ? children(user) : children
}
}

0 comments on commit 245bbf3

Please sign in to comment.