1
00:00:00,000 --> 00:00:01,000
So in the end here,

2
00:00:01,000 --> 00:00:06,000
I wanna set a different server action depending on the mode.

3
00:00:07,000 --> 00:00:10,000
And we can achieve this by going

4
00:00:10,000 --> 00:00:13,000
to our auth actions file.

5
00:00:13,000 --> 00:00:17,000
And there I'll add a new third Server action,

6
00:00:17,000 --> 00:00:18,000
which I'll name auth.

7
00:00:20,000 --> 00:00:23,000
And here I expect to get my mode

8
00:00:23,000 --> 00:00:28,000
as a parameter followed by the previous state

9
00:00:28,000 --> 00:00:30,000
and the form data.

10
00:00:32,000 --> 00:00:37,000
And I then wanna check if the mode is equal to log in.

11
00:00:37,000 --> 00:00:41,000
And if that's the case, I want to call that log in function,

12
00:00:41,000 --> 00:00:44,000
that log in Server action and return that call

13
00:00:45,000 --> 00:00:49,000
and pass that previous state and the form data to it.

14
00:00:50,000 --> 00:00:53,000
Otherwise, I'll return the result of calling signup

15
00:00:53,000 --> 00:00:57,000
and passing previous state and form data to it.

16
00:00:58,000 --> 00:01:02,000
So that I use this as a helper Server action

17
00:01:02,000 --> 00:01:06,000
to call the appropriate Server action depending on the mode.

18
00:01:07,000 --> 00:01:10,000
Now, it's now that auth Server action that I want

19
00:01:10,000 --> 00:01:12,000
to use in my auth form.

20
00:01:13,000 --> 00:01:18,000
So here it's that auth Server action instead

21
00:01:18,000 --> 00:01:20,000
of the signup Server action.

22
00:01:21,000 --> 00:01:25,000
However, this auth Server action now needs to get

23
00:01:25,000 --> 00:01:28,000
that mode information, and by default it wouldn't.

24
00:01:30,000 --> 00:01:33,000
Now you can add extra data that will be added

25
00:01:33,000 --> 00:01:35,000
to the Server action, though

26
00:01:35,000 --> 00:01:40,000
by calling the bind method on this Server action function.

27
00:01:41,000 --> 00:01:43,000
The bind method is a default method

28
00:01:43,000 --> 00:01:46,000
that exists in JavaScript, which you can use

29
00:01:46,000 --> 00:01:49,000
to preconfigure functions

30
00:01:49,000 --> 00:01:51,000
so that when they will be executed,

31
00:01:51,000 --> 00:01:54,000
they will be preconfigured in a certain way.

32
00:01:54,000 --> 00:01:56,000
And you can, for example, preconfigure

33
00:01:56,000 --> 00:02:00,000
what this keyword in that function will refer to.

34
00:02:00,000 --> 00:02:02,000
And you do that with help

35
00:02:02,000 --> 00:02:04,000
of the first argument you pass to bind.

36
00:02:04,000 --> 00:02:07,000
But I don't care about that here, so I'll set this to null,

37
00:02:07,000 --> 00:02:11,000
but any other extra argument you add thereafter

38
00:02:11,000 --> 00:02:14,000
will actually be passed as a parameter

39
00:02:14,000 --> 00:02:17,000
into this function when it's being called.

40
00:02:17,000 --> 00:02:20,000
And that will be that mode then.

41
00:02:20,000 --> 00:02:22,000
So that mode should be passed

42
00:02:22,000 --> 00:02:26,000
as an extra argument into auth when it's being

43
00:02:26,000 --> 00:02:27,000
called in the future.

44
00:02:29,000 --> 00:02:33,000
And that's one way of making sure

45
00:02:33,000 --> 00:02:37,000
that different Server Actions will be called

46
00:02:37,000 --> 00:02:39,000
depending on the mode.

47
00:02:39,000 --> 00:02:41,000
So if you save that and you reload

48
00:02:41,000 --> 00:02:44,000
and you are in login mode,

49
00:02:44,000 --> 00:02:47,000
and you try to log in with a nonexisting user,

50
00:02:47,000 --> 00:02:51,000
which I here do, this is a user I have not created,

51
00:02:51,000 --> 00:02:54,000
if I click login, I get this error.

52
00:02:55,000 --> 00:02:59,000
If I on the other hand, use a valid email address

53
00:02:59,000 --> 00:03:03,000
and a valid password, so the correct password for

54
00:03:03,000 --> 00:03:08,000
that email address, I'm redirected to that training page

55
00:03:08,000 --> 00:03:11,000
because now the user was logged in

56
00:03:11,000 --> 00:03:14,000
and that off cookie was set.

57
00:03:14,000 --> 00:03:15,000
And that's therefore, now of course,

58
00:03:15,000 --> 00:03:18,000
another huge step forward

59
00:03:18,000 --> 00:03:23,000
because this now is a working log in process.

60
00:03:23,000 --> 00:03:27,000
Now as a last step, I actually also wanna make sure

61
00:03:27,000 --> 00:03:31,000
that we have a header here at the top if the user

62
00:03:31,000 --> 00:03:32,000
is logged in.

63
00:03:32,000 --> 00:03:36,000
And that header should then contain a logout button,

64
00:03:36,000 --> 00:03:39,000
which we can press to clear that cookie

65
00:03:39,000 --> 00:03:41,000
and to, well, lock the user out.

