Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,490 --> 00:00:06,760
The very last thing we want to do before moving on to the login form is to persist the authentication.
2
00:00:06,760 --> 00:00:11,910
After a user registers an account, they are automatically logged into the system.
3
00:00:11,920 --> 00:00:14,530
The token is stored locally on the system.
4
00:00:14,530 --> 00:00:17,010
The Firebase SDK handles that.
5
00:00:17,020 --> 00:00:20,560
We've also taken the time to create a state property.
6
00:00:20,860 --> 00:00:27,340
The problem we currently face is that if the user refreshes the page, then there are no longer authenticated
7
00:00:27,340 --> 00:00:28,300
into the app.
8
00:00:28,300 --> 00:00:32,020
At the end of the day, the UX is plain JavaScript.
9
00:00:32,020 --> 00:00:37,690
Any data stored in the store will become lost if the user navigates away from our site.
10
00:00:37,960 --> 00:00:42,010
The user logged in state property is set to false.
11
00:00:42,010 --> 00:00:44,140
This value is not what we want.
12
00:00:44,140 --> 00:00:46,810
We want to be able to log the user back in.
13
00:00:46,810 --> 00:00:53,290
If they come back to the application, we can use the token generated by Firebase to help us solve this
14
00:00:53,290 --> 00:00:54,070
issue.
15
00:00:54,100 --> 00:00:56,890
Firebase will keep track of the token for us.
16
00:00:56,890 --> 00:01:03,280
Even if the page refreshes, we can use it to tell the Vue application to set the state property to
17
00:01:03,280 --> 00:01:03,730
true.
18
00:01:03,730 --> 00:01:08,020
If the user is logged in, here's how we'll solve this problem.
19
00:01:08,020 --> 00:01:11,680
First, we'll need to load firebase before we load view.
20
00:01:11,710 --> 00:01:17,110
We can't check if the user is logged in if Firebase hasn't been initialized.
21
00:01:17,140 --> 00:01:22,540
The second step is to check the token after the Vue application has been initialized.
22
00:01:22,570 --> 00:01:26,950
If the token is valid, then we'll set the state property to true.
23
00:01:26,980 --> 00:01:30,370
We'll take care of the first step in this lecture.
24
00:01:30,820 --> 00:01:35,320
Let's begin open the main JS file in your editor.
25
00:01:37,610 --> 00:01:42,570
The first step is to initialize Firebase before we initialize the app.
26
00:01:42,590 --> 00:01:46,190
The app is initialized in the main JS file.
27
00:01:46,220 --> 00:01:50,790
We're going to modify it to wait for Firebase to initialize first.
28
00:01:50,810 --> 00:01:55,610
Specifically, we want to wait for when the authentication service is ready.
29
00:01:55,640 --> 00:02:00,850
Behind the scenes, Firebase will make a request to the authentication service.
30
00:02:00,860 --> 00:02:03,220
It'll check if the user is logged in.
31
00:02:03,230 --> 00:02:08,479
After receiving a response, Firebase will fire an event we can listen to.
32
00:02:08,509 --> 00:02:10,710
We'll want to listen for that event.
33
00:02:10,729 --> 00:02:13,250
It'll tell us if the user is logged in.
34
00:02:13,610 --> 00:02:18,410
First, we'll need to update the import statement for the Firebase file.
35
00:02:18,440 --> 00:02:23,630
Previously, we were importing the file without assigning the import a reference.
36
00:02:23,630 --> 00:02:27,410
We'll want to update this to grab the authentication object.
37
00:02:27,440 --> 00:02:31,880
The authentication object is how we'll be able to listen for the event.
38
00:02:32,000 --> 00:02:36,170
The structure, the import statement for the authentication object.
39
00:02:38,460 --> 00:02:41,200
The next thing we'll do is listen for the event.
40
00:02:41,220 --> 00:02:47,880
We're going to add the code before we initialize the view application using the authentication object
41
00:02:47,880 --> 00:02:48,330
call.
42
00:02:48,330 --> 00:02:51,150
The method off state changed.
43
00:02:53,600 --> 00:03:00,530
This method has one argument which is the callback function to call when the event is emitted will pass
44
00:03:00,530 --> 00:03:01,850
in an arrow function.
45
00:03:04,230 --> 00:03:09,180
Firebase will emit the event whenever the user's authentication state changes.
46
00:03:09,210 --> 00:03:11,760
This includes when the user is logged out.
47
00:03:11,790 --> 00:03:15,970
It will run immediately after Firebase is loaded on the page.
48
00:03:15,990 --> 00:03:18,480
It's guaranteed to run at least once.
49
00:03:18,720 --> 00:03:22,680
This event makes it safe to place the view instance inside of it.
50
00:03:22,710 --> 00:03:27,330
We're going to cut the new instance of view and paste it into the callback function.
51
00:03:29,660 --> 00:03:32,020
There is one problem with this approach.
52
00:03:32,030 --> 00:03:35,070
The event can be emitted multiple times.
53
00:03:35,090 --> 00:03:38,590
We don't want to initialize view every single time.
54
00:03:38,600 --> 00:03:45,590
We can prevent this issue from occurring by using a variable before the events define a variable called
55
00:03:45,590 --> 00:03:46,160
app.
56
00:03:48,580 --> 00:03:51,170
We won't assign it any type of value.
57
00:03:51,190 --> 00:03:57,490
Then inside the callback function, create a conditional statement that checks if the app variable is
58
00:03:57,490 --> 00:03:58,090
empty.
59
00:04:00,340 --> 00:04:03,910
We'll cut and paste the View app into the conditional statement.
60
00:04:06,060 --> 00:04:10,920
Lastly, we'll set the app variable to the create app function.
61
00:04:13,270 --> 00:04:18,880
By storing the application in a variable, we can check if the app has already been initialized.
62
00:04:18,910 --> 00:04:22,300
If it already has, we won't need to initialize it again.
63
00:04:22,330 --> 00:04:24,630
This takes care of the first step.
64
00:04:24,640 --> 00:04:30,670
We've loaded Firebase so that we can check if the user is authenticated when the application starts.
65
00:04:30,670 --> 00:04:33,850
The next step is to check if the user is authenticated.
66
00:04:33,850 --> 00:04:36,430
After the application is initialized.
67
00:04:36,430 --> 00:04:39,250
We'll take care of this step in the next lecture.
6464
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.