Authentication
Objective
- Verifying and signing JSON Web Tokens (JWT).
- Authenticating using a one-time-password or an OAuth provider.
- Using Next.js server actions/mutations, router, and headers.
1 Magic Authentication
- Register and create a Magic Auth (opens in a new tab) application:
- Install the Magic SDK packages and use them on the client and server side, respectively,
npm install magic-sdk @magic-sdk/admin
. - Store the publishable key as an environment variable, accessible on the client side,
NEXT_PUBLIC_MAGIC_KEY
. - Store the secret key as an environment variable, accessible only on the server side,
MAGIC_SECRET_KEY
. - Store the keys in
.env.local
instead of.env
.
- Install the Magic SDK packages and use them on the client and server side, respectively,
- Create a component that allows the user to login and logout using Magic Auth’s email one-time password:
- Update the user’s email address after a successful authentication.
- Use the token, after storing it in local storage, to authenticate and identify the users. The headers must be set and used correctly, both on the client and server side.
- Add GitHub as a provider and update the page/schema/endpoints to display a user’s name and picture.
- Test the application using multiple users. You can use a private/incognito tab for each user.
2 Server Actions
- Create a Next application using:
npx create-next-app@latest .
. Update the configuration file,next.config.js
, to enable (experimental) server actions:const nextConfig = { experimental: { serverActions: true } };
.- Create a Prisma schema for managing users and their ideas:
- A user has an email address, a required creation date, a registration date, and a list of ideas.
- An idea has a required title, a list of (predefined) tags, and a required creation date.
- Create a page that allows a user to manage their ideas:
- Use server actions to retrieve, create, and delete ideas.
- Use local storage to identify a user using their model identifier.
- Do not store the ideas in local storage.
- Create a data repository service to manage users and their ideas. A user should be able to retrieve their ideas, create a new idea, and delete an existing idea.
- Create an API with multiple endpoints and methods that builds on top of the data repository service methods:
/api/[user]/[idea]
. - Test the application using multiple users. You can use a private/incognito tab for each user.