So now that we did install Lucia,
let's get started using it
so that we can soon create such auth sessions
and use them to identify authenticated users.
For that, I'll go to the lib folder
and in there I'll add a auth.js file now.
And it's now in this file
where I'll create a new constant called lucia.
The name is up to you,
but it should store a new object that's created
with help of the Lucia constructor function.
And for that you actually need
to import Lucia from this Lucia package, like this.
Now this constructor function
then needs a couple of arguments.
For example, it needs an adapter,
which will tell Lucia where and how to store those sessions.
That's why we installed that other package,
this @lucia-auth/adapter-sqlite package,
from which we can now import the BetterSqlite3Adapter.
With that imported, we can create a new adapter object
by constructing this BetterSqlite3Adapter.
And to this adapter, we need to pass a handle
to our BetterSqlite3.Database.
And conveniently we have that in the db.js file.
There I'm creating such a better-sqlite3 database
and I'm then exporting that.
So therefore we can easily use that database here
by importing it, like this
and by then assigning it as a first argument
to this BetterSqlite3Adapter constructor.
And then as a second argument for this constructor here,
we can pass a configuration object
where you can tell this adapter
and therefore in the end, Lucia,
what the table name of your users' database table will be.
And in my case, it's users
because in a db.js file, I'm creating a user's table here
for storing the users
and Lucia needs to know
in which database table you do store your users.
And it also needs to know
where you are storing your sessions
or where it should store the sessions,
what the name of the table in the database,
where those sessions should be stored will be.
And in my case, it should be this sessions table here.
So the table name is sessions.
That's what I'll assign here.
With that we have an adapter
and that's now the first argument we must pass
to that Lucia constructor.
Then you can also pass a configuration object
to that constructor function here.
And there you can configure various things,
but for us here, it's the sessionCookie.
That's important
because Lucia will automatically create that cookie
that contains the session ID
because it's also Lucia that creates the session.
But we can now configure that cookie.
And for example, when using Lucia in a NextJS application,
you should set expires to false.
That's something you learn in the Lucia documentation.
And we can also add some attributes to that cookie.
And specifically, I wanna set this cookie
to only work on HTTPS
if we're running this app in production.
So if the NODE_ENV environment variable,
which is set automatically
by pretty much all hosting providers to production
if the app is running in production,
if that environment variable holds a value of production,
which means we're not running the app locally
during development, but instead we now did deploy it.
And in that case, I want to enforce this cookie
to only work across HTTPS.
But that's then it.
That's how we create a new Lucia instance
and we can now use that instance
to then create sessions and session cookies
and to validate incoming requests
and see if they have a valid session cookie.