After all the preparation, it is finally time for us to implement our authentication system. We'll start with a magic link system in order to get familiarized with all the new concepts, and later we'll discuss other authentication systems as well.
Think about how a magic link authentication workflow looks like:
- The user enters their email address on the website. Instead of asking for a password, the system sends them a special link (the "magic link") via email.
- The user opens their email and clicks the magic link. This link is unique and can only be used once, and it usually expires after a short time for security.
- When the user clicks the link, Auth.js recognizes it as valid and logs them in automatically, no password required.
We need two critical components for this magic link system to work, a database and a email provider. The database stores information, including the user information, token, expiry time, and so on. The email provider delivers the magic link to the user's inbox.
Setting up the database
We'll start by setting up the database, and again, we are going to use Prisma.js and SQLite as an example.
Run the following command to install Prisma:
1npm install prisma --save-dev
And also the Prisma client, which is a JavaScript library that allows us to interact with Prisma in our app.
1npm install @prisma/client