next-iron-session-with-JWT-authentication example
This example creates an JWT authentication system that uses a signed and encrypted cookie to store session data.
It uses current best practices as for authentication in the Next.js ecosystem:
1. no `getInitialProps` to ensure every page is static
2. `useUser` hook together with `swr` for data fetching
Features
- Logged in status synchronized between browser windows/tabs
- Layout based on logged in status
- All pages are static
- Session data is signed and encrypted in a cookie
Steps to test the functionality:
- Write login and refresh queries in lib/authenticate and lib/refreshToken. Sample Data is available as well.
- Set environment variables with respect to your server response.
- Click login and enter your username and password.
- Click home and click profile again, notice how your session is being used through a token stored in a cookie.
- Click logout and try to go to profile again. You'll get redirected to the `/login` route.