amplify-auth-next-storage
Isomorphic cookie storage for Next.js apps using @aws-amplify/auth
Installation
$ npm install amplify-auth-next-storage
Options
ctx options
ctx
is the Next.js ctx object, only required server-side. Passnull
orundefined
on the client-side.options
are identical to Amplify'scookieStorage
configuration options.
https://aws-amplify.github.io/docs/js/authentication#manual-setup
Option | Type | Default | Description |
---|---|---|---|
domain (required) | string | none | Cookies domain |
expires | number | 365 | Cookie expiration in days |
path | string | / | Cookies path |
secure | boolean | true | Cookie secure flag |
Usage
// utils/auth-utils.js // Placing your configuration into a reusable utility function isn't strictly// necessary, but makes repeated use of Auth.configure() easier ;; { Auth;} // pages/_app.js ;;; globalfetch = fetch; // One workaround for getting Auth.configure to work properly server-side const YourApp = { // Running this once at the app level, client-side, allows you to // use `Auth` methods in all of your components ; return <Component ...pageProps />;}; YourAppgetInitialProps = async { const appProps = await App; // However, we need to configure the pool every time it's needed within getInitialProps ; // ... do stuff with Auth. e.g. Auth.currentUserInfo return ...appProps ;}; ; // components/FancyComponent.js ;; const FancyComponent = { // You can use auth here without configuring the pool since we already // configured it at the YourApp level Auth; return <div>Fancy Component</div>;} FancyComponentgetInitialProps = async { // If we need Auth in this component server-side, we need to configure the pool again ; await Auth; return {};}
Notes
If you're struggling to get Auth.configure
to work properly on the server side, one workaround is to use something like node-fetch
as the global fetch.
// _app.js ; globalfetch = fetch; // ... the rest of your _app.js file