-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[default-login] Test cookies and allow for whitelisting before loggin…
…g in (#271)
- Loading branch information
1 parent
61b0937
commit a731831
Showing
2 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import PropTypes from 'prop-types' | ||
import React, {PureComponent} from 'react' | ||
import client from 'part:@sanity/base/client' | ||
import styles from './styles/LoginDialog.css' | ||
import Button from 'part:@sanity/components/buttons/default' | ||
import Spinner from 'part:@sanity/components/loading/spinner' | ||
import SanityStudioLogo from 'part:@sanity/base/sanity-studio-logo' | ||
|
||
const checkCookies = () => { | ||
return client.request({method: 'post', uri: '/auth/testCookie', withCredentials: true}).then(() => true) | ||
.then(() => { | ||
return client.request({uri: '/auth/testCookie', withCredentials: true}) | ||
.then(() => true) | ||
.catch(() => false) | ||
}) | ||
.then(get => ({isCookieError: !get})) | ||
.catch(error => ({error})) | ||
} | ||
|
||
class CookieCheck extends PureComponent { | ||
|
||
static propTypes = { | ||
children: PropTypes.node.isRequired | ||
} | ||
|
||
constructor(...args) { | ||
super(...args) | ||
this.state = {isLoading: true} | ||
} | ||
|
||
componentWillMount() { | ||
checkCookies().then(res => this.setState({ | ||
result: res, | ||
isLoading: false, | ||
})) | ||
} | ||
|
||
renderWhiteListForm() { // eslint-disable-line class-methods-use-this | ||
const redirectTo = `${client.clientConfig.url}/auth/whitelist?redirectTo=${encodeURIComponent(window.location.toString())}` | ||
return ( | ||
<div className={styles.root}> | ||
|
||
<div className={styles.sanityStudioLogo}> | ||
<SanityStudioLogo /> | ||
</div> | ||
|
||
<div className={styles.inner}> | ||
|
||
<h2 className={styles.headline}>Accept the Cookie</h2> | ||
<p>Your browser didn't accept our cookie so we're having trouble logging you in.</p> | ||
<p>You can explicitly accept it though by clicking below.</p> | ||
|
||
<form | ||
method="post" | ||
action={`${client.clientConfig.url}/auth/testCookie`} | ||
encType="application/x-www-form-urlencoded" | ||
> | ||
<input type="hidden" name="redirectTo" value={redirectTo} /> | ||
<Button color="success" inverted type="submit">ACCEPT COOKIE</Button> | ||
</form> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
render() { | ||
const {isLoading, result} = this.state | ||
|
||
if (isLoading) { | ||
return <div><Spinner message="Running diagnostics..." inline /><p /></div> | ||
} | ||
|
||
if (result.isCookieError) { | ||
return this.renderWhiteListForm() | ||
} | ||
|
||
return ( | ||
<div> | ||
{this.props.children} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default CookieCheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters