Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove deprected and refactor with-apollo-auth (#4070)
* Remove deprected use of apollo-client-preset, and refactor Changes * Remove deprected use of apollo-client-preset in favor of apollo-boost * Refactor for usage of react-apollo@2.1 * Use standard Just ran standard --fix
- Loading branch information
1 parent
812db74
commit b321e6e
Showing
6 changed files
with
142 additions
and
147 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,62 @@ | ||
import { Mutation, withApollo } from 'react-apollo' | ||
import gql from 'graphql-tag' | ||
import cookie from 'cookie' | ||
import redirect from '../lib/redirect' | ||
|
||
const CREATE_USER = gql` | ||
mutation Create($name: String!, $email: String!, $password: String!) { | ||
createUser(name: $name, authProvider: { email: { email: $email, password: $password }}) { | ||
id | ||
} | ||
signinUser(email: { email: $email, password: $password }) { | ||
token | ||
} | ||
} | ||
` | ||
|
||
const RegisterBox = (props) => { | ||
let name, email, password | ||
|
||
return ( | ||
<Mutation mutation={CREATE_USER} onCompleted={(data) => { | ||
// Store the token in cookie | ||
document.cookie = cookie.serialize('token', data.signinUser.token, { | ||
maxAge: 30 * 24 * 60 * 60 // 30 days | ||
}) | ||
// Force a reload of all the current queries now that the user is | ||
// logged in | ||
props.client.resetStore().then(() => { | ||
redirect({}, '/') | ||
}) | ||
}} onError={(error) => { | ||
// If you want to send error to external service? | ||
console.log(error) | ||
}}> | ||
{(create, { data, error }) => ( | ||
<div> | ||
<form onSubmit={e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
|
||
create({ variables: { | ||
name: name.value, | ||
email: email.value, | ||
password: password.value | ||
}}) | ||
|
||
name.value = email.value = password.value = '' | ||
}}> | ||
{ error && <p>Issue occured while registering :(</p> } | ||
<input name='name' placeholder='Name' ref={node => { name = node }} /><br /> | ||
<input name='email' placeholder='Email' ref={node => { email = node }} /><br /> | ||
<input name='password' placeholder='Password' ref={node => { password = node }} type='password' /><br /> | ||
<button>Register</button> | ||
</form> | ||
</div> | ||
)} | ||
|
||
</Mutation> | ||
) | ||
} | ||
|
||
export default withApollo(RegisterBox) |
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,57 @@ | ||
import { Mutation, withApollo } from 'react-apollo' | ||
import gql from 'graphql-tag' | ||
import cookie from 'cookie' | ||
import redirect from '../lib/redirect' | ||
|
||
const SIGN_IN = gql` | ||
mutation Signin($email: String!, $password: String!) { | ||
signinUser(email: { email: $email, password: $password}) { | ||
token | ||
} | ||
} | ||
` | ||
|
||
// TODO: Find a better name for component. | ||
const SigninBox = (props) => { | ||
let email, password | ||
|
||
return ( | ||
<Mutation mutation={SIGN_IN} onCompleted={(data) => { | ||
// Store the token in cookie | ||
document.cookie = cookie.serialize('token', data.signinUser.token, { | ||
maxAge: 30 * 24 * 60 * 60 // 30 days | ||
}) | ||
// Force a reload of all the current queries now that the user is | ||
// logged in | ||
props.client.resetStore().then(() => { | ||
redirect({}, '/') | ||
}) | ||
}} onError={(error) => { | ||
// If you want to send error to external service? | ||
console.log(error) | ||
}}> | ||
{(signinUser, { data, error }) => ( | ||
<div> | ||
<form onSubmit={e => { | ||
e.preventDefault() | ||
e.stopPropagation() | ||
|
||
signinUser({ variables: { | ||
email: email.value, | ||
password: password.value | ||
}}) | ||
|
||
email.value = password.value = '' | ||
}}> | ||
{ error && <p>No user found with that information.</p> } | ||
<input name='email' placeholder='Email' ref={node => { email = node }} /><br /> | ||
<input name='password' placeholder='Password' ref={node => { password = node }} type='password' /><br /> | ||
<button>Sign in</button> | ||
</form> | ||
</div> | ||
)} | ||
</Mutation> | ||
) | ||
} | ||
|
||
export default withApollo(SigninBox) |
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
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
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
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