1
00:00:00,000 --> 00:00:03,000
So now that we did install Lucia,

2
00:00:03,000 --> 00:00:04,000
let's get started using it

3
00:00:04,000 --> 00:00:08,000
so that we can soon create such auth sessions

4
00:00:08,000 --> 00:00:11,000
and use them to identify authenticated users.

5
00:00:13,000 --> 00:00:15,000
For that, I'll go to the lib folder

6
00:00:15,000 --> 00:00:19,000
and in there I'll add a auth.js file now.

7
00:00:19,000 --> 00:00:21,000
And it's now in this file

8
00:00:21,000 --> 00:00:25,000
where I'll create a new constant called lucia.

9
00:00:25,000 --> 00:00:26,000
The name is up to you,

10
00:00:26,000 --> 00:00:30,000
but it should store a new object that's created

11
00:00:30,000 --> 00:00:33,000
with help of the Lucia constructor function.

12
00:00:34,000 --> 00:00:36,000
And for that you actually need

13
00:00:36,000 --> 00:00:41,000
to import Lucia from this Lucia package, like this.

14
00:00:44,000 --> 00:00:46,000
Now this constructor function

15
00:00:46,000 --> 00:00:48,000
then needs a couple of arguments.

16
00:00:48,000 --> 00:00:52,000
For example, it needs an adapter,

17
00:00:52,000 --> 00:00:57,000
which will tell Lucia where and how to store those sessions.

18
00:00:57,000 --> 00:01:01,000
That's why we installed that other package,

19
00:01:01,000 --> 00:01:05,000
this @lucia-auth/adapter-sqlite package,

20
00:01:05,000 --> 00:01:10,000
from which we can now import the BetterSqlite3Adapter.

21
00:01:13,000 --> 00:01:16,000
With that imported, we can create a new adapter object

22
00:01:16,000 --> 00:01:18,000
by constructing this BetterSqlite3Adapter.

23
00:01:21,000 --> 00:01:24,000
And to this adapter, we need to pass a handle

24
00:01:24,000 --> 00:01:28,000
to our BetterSqlite3.Database.

25
00:01:28,000 --> 00:01:32,000
And conveniently we have that in the db.js file.

26
00:01:32,000 --> 00:01:36,000
There I'm creating such a better-sqlite3 database

27
00:01:36,000 --> 00:01:39,000
and I'm then exporting that.

28
00:01:39,000 --> 00:01:42,000
So therefore we can easily use that database here

29
00:01:42,000 --> 00:01:46,000
by importing it, like this

30
00:01:46,000 --> 00:01:48,000
and by then assigning it as a first argument

31
00:01:48,000 --> 00:01:53,000
to this BetterSqlite3Adapter constructor.

32
00:01:53,000 --> 00:01:56,000
And then as a second argument for this constructor here,

33
00:01:56,000 --> 00:01:59,000
we can pass a configuration object

34
00:01:59,000 --> 00:02:01,000
where you can tell this adapter

35
00:02:01,000 --> 00:02:04,000
and therefore in the end, Lucia,

36
00:02:04,000 --> 00:02:09,000
what the table name of your users' database table will be.

37
00:02:10,000 --> 00:02:11,000
And in my case, it's users

38
00:02:11,000 --> 00:02:16,000
because in a db.js file, I'm creating a user's table here

39
00:02:16,000 --> 00:02:17,000
for storing the users

40
00:02:17,000 --> 00:02:19,000
and Lucia needs to know

41
00:02:19,000 --> 00:02:23,000
in which database table you do store your users.

42
00:02:23,000 --> 00:02:26,000
And it also needs to know

43
00:02:26,000 --> 00:02:28,000
where you are storing your sessions

44
00:02:28,000 --> 00:02:31,000
or where it should store the sessions,

45
00:02:31,000 --> 00:02:33,000
what the name of the table in the database,

46
00:02:33,000 --> 00:02:37,000
where those sessions should be stored will be.

47
00:02:37,000 --> 00:02:41,000
And in my case, it should be this sessions table here.

48
00:02:41,000 --> 00:02:44,000
So the table name is sessions.

49
00:02:44,000 --> 00:02:46,000
That's what I'll assign here.

50
00:02:48,000 --> 00:02:50,000
With that we have an adapter

51
00:02:50,000 --> 00:02:52,000
and that's now the first argument we must pass

52
00:02:52,000 --> 00:02:54,000
to that Lucia constructor.

53
00:02:56,000 --> 00:02:58,000
Then you can also pass a configuration object

54
00:02:58,000 --> 00:03:02,000
to that constructor function here.

55
00:03:02,000 --> 00:03:05,000
And there you can configure various things,

56
00:03:05,000 --> 00:03:08,000
but for us here, it's the sessionCookie.

57
00:03:08,000 --> 00:03:09,000
That's important

58
00:03:10,000 --> 00:03:14,000
because Lucia will automatically create that cookie

59
00:03:14,000 --> 00:03:16,000
that contains the session ID

60
00:03:16,000 --> 00:03:19,000
because it's also Lucia that creates the session.

61
00:03:19,000 --> 00:03:22,000
But we can now configure that cookie.

62
00:03:22,000 --> 00:03:26,000
And for example, when using Lucia in a NextJS application,

63
00:03:26,000 --> 00:03:30,000
you should set expires to false.

64
00:03:30,000 --> 00:03:34,000
That's something you learn in the Lucia documentation.

65
00:03:34,000 --> 00:03:37,000
And we can also add some attributes to that cookie.

66
00:03:37,000 --> 00:03:40,000
And specifically, I wanna set this cookie

67
00:03:40,000 --> 00:03:43,000
to only work on HTTPS

68
00:03:43,000 --> 00:03:45,000
if we're running this app in production.

69
00:03:45,000 --> 00:03:49,000
So if the NODE_ENV environment variable,

70
00:03:49,000 --> 00:03:51,000
which is set automatically

71
00:03:51,000 --> 00:03:55,000
by pretty much all hosting providers to production

72
00:03:55,000 --> 00:03:57,000
if the app is running in production,

73
00:03:57,000 --> 00:04:00,000
if that environment variable holds a value of production,

74
00:04:00,000 --> 00:04:02,000
which means we're not running the app locally

75
00:04:02,000 --> 00:04:05,000
during development, but instead we now did deploy it.

76
00:04:05,000 --> 00:04:08,000
And in that case, I want to enforce this cookie

77
00:04:08,000 --> 00:04:11,000
to only work across HTTPS.

78
00:04:12,000 --> 00:04:14,000
But that's then it.

79
00:04:14,000 --> 00:04:17,000
That's how we create a new Lucia instance

80
00:04:17,000 --> 00:04:19,000
and we can now use that instance

81
00:04:19,000 --> 00:04:22,000
to then create sessions and session cookies

82
00:04:22,000 --> 00:04:26,000
and to validate incoming requests

83
00:04:26,000 --> 00:04:29,000
and see if they have a valid session cookie.

