Commit b321e6e9 authored by Tage A. L. K's avatar Tage A. L. K Committed by Tim Neutkens

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
parent 812db745
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)
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)
import { ApolloClient } from 'apollo-client' import { ApolloClient, InMemoryCache } from 'apollo-boost'
import { createHttpLink } from 'apollo-link-http' import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { setContext } from 'apollo-link-context' import { setContext } from 'apollo-link-context'
import fetch from 'isomorphic-unfetch' import fetch from 'isomorphic-unfetch'
...@@ -48,4 +47,4 @@ export default function initApollo (initialState, options) { ...@@ -48,4 +47,4 @@ export default function initApollo (initialState, options) {
} }
return apolloClient return apolloClient
} }
\ No newline at end of file
...@@ -10,16 +10,16 @@ ...@@ -10,16 +10,16 @@
"test": "NODE_ENV=test ava" "test": "NODE_ENV=test ava"
}, },
"dependencies": { "dependencies": {
"apollo-client-preset": "1.0.2", "apollo-boost": "^0.1.4",
"apollo-link-context": "1.0.0", "apollo-link-context": "^1.0.7",
"cookie": "^0.3.1", "cookie": "^0.3.1",
"graphql": "0.11.7", "graphql": "^0.13.2",
"isomorphic-unfetch": "^2.0.0", "isomorphic-unfetch": "^2.0.0",
"next": "latest", "next": "latest",
"prop-types": "^15.5.10", "prop-types": "^15.6.1",
"react": "^16.0.0", "react": "^16.2.0",
"react-apollo": "2.0.0", "react-apollo": "^2.1.1",
"react-dom": "^16.0.0" "react-dom": "^16.2.0"
}, },
"devDependencies": { "devDependencies": {
"ava": "^0.19.1", "ava": "^0.19.1",
......
import React from 'react' import React from 'react'
import { graphql, withApollo, compose } from 'react-apollo' import { compose } from 'react-apollo'
import cookie from 'cookie'
import Link from 'next/link' import Link from 'next/link'
import gql from 'graphql-tag'
import withData from '../lib/withData' import withData from '../lib/withData'
import redirect from '../lib/redirect' import redirect from '../lib/redirect'
import checkLoggedIn from '../lib/checkLoggedIn' import checkLoggedIn from '../lib/checkLoggedIn'
import RegisterBox from '../components/RegisterBox'
class CreateAccount extends React.Component { class CreateAccount extends React.Component {
static async getInitialProps (context, apolloClient) { static async getInitialProps (context, apolloClient) {
const { loggedInUser } = await checkLoggedIn(context, apolloClient) const { loggedInUser } = await checkLoggedIn(context, apolloClient)
...@@ -24,13 +24,8 @@ class CreateAccount extends React.Component { ...@@ -24,13 +24,8 @@ class CreateAccount extends React.Component {
render () { render () {
return ( return (
<div> <div>
{/* this.props.create is the mutation function provided by apollo below */} {/* RegisterBox handles all register logic. */}
<form onSubmit={this.props.create}> <RegisterBox client={this.props.client} />
<input type='text' placeholder='Your Name' name='name' /><br />
<input type='email' placeholder='Email' name='email' /><br />
<input type='password' placeholder='Password' name='password' /><br />
<button>Create account</button>
</form>
<hr /> <hr />
Already have an account? <Link prefetch href='/signin'><a>Sign in</a></Link> Already have an account? <Link prefetch href='/signin'><a>Sign in</a></Link>
</div> </div>
...@@ -38,67 +33,7 @@ class CreateAccount extends React.Component { ...@@ -38,67 +33,7 @@ class CreateAccount extends React.Component {
} }
}; };
export default compose( export default compose( // TODO: Maybe remove the usage of compose?
// withData gives us server-side graphql queries before rendering // withData gives us server-side graphql queries before rendering
withData, withData
// withApollo exposes `this.props.client` used when logging out
withApollo,
graphql(
// The `createUser` & `signinUser` mutations are provided by graph.cool by
// default.
// Multiple mutations are executed by graphql sequentially
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
}
}
`,
{
// Use an unambiguous name for use in the `props` section below
name: 'createWithEmail',
// Apollo's way of injecting new props which are passed to the component
props: ({
createWithEmail,
// `client` is provided by the `withApollo` HOC
ownProps: { client }
}) => ({
// `create` is the name of the prop passed to the component
create: (event) => {
/* global FormData */
const data = new FormData(event.target)
event.preventDefault()
event.stopPropagation()
createWithEmail({
variables: {
email: data.get('email'),
password: data.get('password'),
name: data.get('name')
}
}).then(({ data: { signinUser: { token } } }) => {
// Store the token in cookie
document.cookie = cookie.serialize('token', token, {
maxAge: 30 * 24 * 60 * 60 // 30 days
})
// Force a reload of all the current queries now that the user is
// logged in
client.resetStore().then(() => {
// Now redirect to the homepage
redirect({}, '/')
})
}).catch((error) => {
// Something went wrong, such as incorrect password, or no network
// available, etc.
console.error(error)
})
}
})
}
)
)(CreateAccount) )(CreateAccount)
import React from 'react' import React from 'react'
import { graphql, withApollo, compose } from 'react-apollo' import { compose } from 'react-apollo'
import cookie from 'cookie'
import Link from 'next/link' import Link from 'next/link'
import gql from 'graphql-tag'
import withData from '../lib/withData' import withData from '../lib/withData'
import redirect from '../lib/redirect' import redirect from '../lib/redirect'
import checkLoggedIn from '../lib/checkLoggedIn' import checkLoggedIn from '../lib/checkLoggedIn'
import SigninBox from '../components/SigninBox'
class Signin extends React.Component { class Signin extends React.Component {
static async getInitialProps (context, apolloClient) { static async getInitialProps (context, apolloClient) {
const { loggedInUser } = await checkLoggedIn(context, apolloClient) const { loggedInUser } = await checkLoggedIn(context, apolloClient)
...@@ -24,12 +24,8 @@ class Signin extends React.Component { ...@@ -24,12 +24,8 @@ class Signin extends React.Component {
render () { render () {
return ( return (
<div> <div>
{/* this.props.signin is the mutation function provided by apollo below */} {/* SigninBox handles all login logic. */}
<form onSubmit={this.props.signin}> <SigninBox client={this.props.client} />
<input type='email' placeholder='Email' name='email' /><br />
<input type='password' placeholder='Password' name='password' /><br />
<button>Sign in</button>
</form>
<hr /> <hr />
New? <Link prefetch href='/create-account'><a>Create account</a></Link> New? <Link prefetch href='/create-account'><a>Create account</a></Link>
</div> </div>
...@@ -37,61 +33,7 @@ class Signin extends React.Component { ...@@ -37,61 +33,7 @@ class Signin extends React.Component {
} }
}; };
export default compose( export default compose( // TODO: Maybe remove the usage of compose?
// withData gives us server-side graphql queries before rendering // withData gives us server-side graphql queries before rendering
withData, withData
// withApollo exposes `this.props.client` used when logging out
withApollo,
graphql(
// The `signinUser` mutation is provided by graph.cool by default
gql`
mutation Signin($email: String!, $password: String!) {
signinUser(email: { email: $email, password: $password }) {
token
}
}
`,
{
// Use an unambiguous name for use in the `props` section below
name: 'signinWithEmail',
// Apollo's way of injecting new props which are passed to the component
props: ({
signinWithEmail,
// `client` is provided by the `withApollo` HOC
ownProps: { client }
}) => ({
// `signin` is the name of the prop passed to the component
signin: (event) => {
/* global FormData */
const data = new FormData(event.target)
event.preventDefault()
event.stopPropagation()
signinWithEmail({
variables: {
email: data.get('email'),
password: data.get('password')
}
}).then(({ data: { signinUser: { token } } }) => {
// Store the token in cookie
document.cookie = cookie.serialize('token', token, {
maxAge: 30 * 24 * 60 * 60 // 30 days
})
// Force a reload of all the current queries now that the user is
// logged in
client.resetStore().then(() => {
// Now redirect to the homepage
redirect({}, '/')
})
}).catch((error) => {
// Something went wrong, such as incorrect password, or no network
// available, etc.
console.error(error)
})
}
})
}
)
)(Signin) )(Signin)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment