next-auth-hoc
v1.0.1
Published
This library provides simple high order components (HOC) to work with JWT Authentication universally.
Downloads
14
Readme
Authentication HOCs for Next.js
This library provides simple high order components (HOC) to work with JWT Authentication universally.
You'll need a GraphQL Server to work with.
Install
yarn add next-auth-hocAPI
withSignout: Providesignoutfunction for a component to signoutredirect: Provideredirectfunction to redirect to another routewithApollo: ProvideapolloClientto work with GraphQL QueryrequireSignedIn: Require authentication before renderingredirectIfSignedIn: Redirect if a user is already signed in
Your pages/_app.js should provide component like this:
import App, { Container } from 'next/app'
import React from 'react'
import { ApolloProvider } from 'react-apollo'
import { withApollo } from 'next-auth-hoc'
class MyApp extends App {
render () {
const { Component, pageProps, apolloClient } = this.props
return <Container>
<ApolloProvider client={apolloClient}>
<Component {...pageProps} />
</ApolloProvider>
</Container>
}
}
export default withApollo(MyApp)
Example
signin.js
import React from 'react'
import Link from 'next/link'
import SigninBox from '../components/SigninBox'
import { redirectIfSignedIn } from 'next-auth-hoc'
const Signin = () =>
<React.Fragment>
<SigninBox />
<hr />
New? <Link prefetch href='/create-account'><a>Create account</a></Link>
</React.Fragment>
export default redirectIfSignedIn(Signin)License
MIT
