1.0.1 • Published 6 years ago
next-auth-hoc v1.0.1
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-hoc
API
withSignout
: Providesignout
function for a component to signoutredirect
: Provideredirect
function to redirect to another routewithApollo
: ProvideapolloClient
to 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