Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:03,860
Now to do all of that,
2
00:00:03,860 --> 00:00:07,750
it would make sense to not just expect the token here
3
00:00:07,750 --> 00:00:12,350
in the login handler but also the expiration time
4
00:00:13,650 --> 00:00:17,720
and then we can calculate the remaining time
5
00:00:17,720 --> 00:00:19,630
by taking the current time
6
00:00:19,630 --> 00:00:22,690
and deducting it from the expiration time.
7
00:00:22,690 --> 00:00:25,590
And we also want to store that expiration time
8
00:00:25,590 --> 00:00:26,980
in local storage,
9
00:00:26,980 --> 00:00:29,400
so that if we reloaded this app,
10
00:00:29,400 --> 00:00:33,540
we can recalculate that expiration time correctly.
11
00:00:33,540 --> 00:00:36,433
So there are a couple of steps we need to take now.
12
00:00:37,450 --> 00:00:40,120
Now let's start with setting that time
13
00:00:40,120 --> 00:00:42,530
when login handler is called.
14
00:00:42,530 --> 00:00:44,790
I will add a new helper function
15
00:00:44,790 --> 00:00:49,040
outside of the auth context provider function here,
16
00:00:49,040 --> 00:00:51,184
which I'll name calculateRemainingTime
17
00:00:51,184 --> 00:00:55,020
which I'll name calculateRemainingTime
18
00:00:55,020 --> 00:00:59,180
and here I get the expiration time
19
00:00:59,180 --> 00:01:04,180
as a parameter like this
20
00:01:04,220 --> 00:01:08,060
and I want to return the remaining duration in milliseconds
21
00:01:08,060 --> 00:01:10,280
because if I return it in milliseconds,
22
00:01:10,280 --> 00:01:13,103
we can use it just like that in set timeout.
23
00:01:13,970 --> 00:01:18,450
So here I'll then get the current time by calling new date.
24
00:01:18,450 --> 00:01:22,163
This gives me the current timestamp.
25
00:01:23,300 --> 00:01:26,900
To be precise, I'll call get time on that then
26
00:01:26,900 --> 00:01:30,480
and get time gives me that current timestamp
27
00:01:30,480 --> 00:01:32,853
in milliseconds as it says here.
28
00:01:33,840 --> 00:01:36,069
Then I expect expiration time to be a string, let's say,
29
00:01:36,069 --> 00:01:39,940
Then I expect expiration time to be a string, let's say,
30
00:01:39,940 --> 00:01:44,940
and hence, I'll get my adjusted expiration time--
31
00:01:45,430 --> 00:01:47,330
whoops, adjusted expiration time
32
00:01:47,330 --> 00:01:51,760
by first of all passing expiration time to new date
33
00:01:51,760 --> 00:01:53,930
to convert it to a date object
34
00:01:53,930 --> 00:01:56,710
and then we also call get time on that
35
00:01:56,710 --> 00:02:00,250
to get that timestamp in milliseconds.
36
00:02:00,250 --> 00:02:03,840
And that then should typically be some time in the future,
37
00:02:03,840 --> 00:02:06,700
this on the other end is the current timestamp.
38
00:02:06,700 --> 00:02:09,889
And now we got the remaining time
39
00:02:11,240 --> 00:02:15,040
in milliseconds or the remaining duration,
40
00:02:15,040 --> 00:02:17,020
however you want to name it,
41
00:02:17,020 --> 00:02:20,150
by taking this adjusted expiration time,
42
00:02:20,150 --> 00:02:22,900
which is in the future minus the current time,
43
00:02:22,900 --> 00:02:24,163
which is now.
44
00:02:25,210 --> 00:02:29,090
Of course, if we already passed this expiration time,
45
00:02:29,090 --> 00:02:31,710
this will be a negative value but that's okay.
46
00:02:31,710 --> 00:02:33,273
We can then handle this.
47
00:02:34,440 --> 00:02:37,283
So here I'll just return this remaining duration.
48
00:02:39,010 --> 00:02:40,900
So this is now a helper function,
49
00:02:40,900 --> 00:02:44,220
which we can use now in the login handler.
50
00:02:44,220 --> 00:02:47,970
Here, I want to calculate the remaining time
51
00:02:47,970 --> 00:02:50,820
and I'll pass my expiration time to that.
52
00:02:50,820 --> 00:02:54,010
And then here I get this remaining time.
53
00:02:54,010 --> 00:02:57,200
And here, since we adjust logged in,
54
00:02:57,200 --> 00:02:58,586
we know that this will definitely be a positive value
55
00:02:58,586 --> 00:03:02,520
we know that this will definitely be a positive value
56
00:03:02,520 --> 00:03:05,690
around one hour in milliseconds
57
00:03:05,690 --> 00:03:08,220
and we can then set a timer.
58
00:03:08,220 --> 00:03:13,220
And when that timer expires, we can log the user out.
59
00:03:13,300 --> 00:03:16,970
So what I'll do here is I'll actually switch order
60
00:03:16,970 --> 00:03:20,890
and move login handler after the logout handler,
61
00:03:20,890 --> 00:03:23,600
so that here I can call set timeout
62
00:03:23,600 --> 00:03:27,080
and point at the logout handler as a callback,
63
00:03:27,080 --> 00:03:31,130
so that the logout handler is executed if that timer expires
64
00:03:31,130 --> 00:03:34,480
and the time for that timer is that remaining time
65
00:03:35,930 --> 00:03:38,760
because that should be a number in milliseconds,
66
00:03:38,760 --> 00:03:41,180
since I calculated from two timestamps
67
00:03:41,180 --> 00:03:43,653
that are expressed in milliseconds.
68
00:03:45,570 --> 00:03:48,660
So that should automatically log the user out
69
00:03:48,660 --> 00:03:50,453
after that time expired.
70
00:03:51,950 --> 00:03:54,960
Now this also means that for the login handler,
71
00:03:54,960 --> 00:03:58,360
we now need to provide this expiration time.
72
00:03:58,360 --> 00:04:00,360
So in the auth form here
73
00:04:00,360 --> 00:04:02,640
where I do call this login function,
74
00:04:02,640 --> 00:04:05,990
it's not enough to just pass the ID token.
75
00:04:05,990 --> 00:04:09,563
Instead, we now also have to pass this expiration time.
76
00:04:10,540 --> 00:04:12,220
Now, when we do log in,
77
00:04:12,220 --> 00:04:14,370
when we send the request to the sign in
78
00:04:14,370 --> 00:04:17,060
or sign up API routes here,
79
00:04:17,060 --> 00:04:20,910
we do get this expires in part in the response
80
00:04:20,910 --> 00:04:24,950
but that's the number of seconds until the ID token expires.
81
00:04:24,950 --> 00:04:29,350
That's not a timestamp, which is what I'm expecting here
82
00:04:29,350 --> 00:04:31,680
in the login handler though.
83
00:04:31,680 --> 00:04:33,870
Now we could change what we expect here
84
00:04:33,870 --> 00:04:37,280
or we transform the data here and I'll do the latter.
85
00:04:37,280 --> 00:04:40,590
So here I got my expiration time
86
00:04:40,590 --> 00:04:45,590
by simply calculating a new date,
87
00:04:46,000 --> 00:04:50,770
creating a new date object from a timestamp in milliseconds,
88
00:04:50,770 --> 00:04:54,080
which I will derive by getting the current time
89
00:04:54,080 --> 00:04:58,923
in milliseconds plus data expires in,
90
00:04:59,900 --> 00:05:03,980
so that expiresin key and the response,
91
00:05:03,980 --> 00:05:05,280
which is the number of seconds
92
00:05:05,280 --> 00:05:08,593
but please note that it will be a string actually.
93
00:05:10,110 --> 00:05:14,750
So I get that here and I'll wrap this in brackets here
94
00:05:14,750 --> 00:05:16,300
to make it a bit easier to read
95
00:05:16,300 --> 00:05:19,540
and I'll convert this to a number with a plus here
96
00:05:19,540 --> 00:05:21,540
and then once it is converted to a number,
97
00:05:21,540 --> 00:05:23,850
I take it times 1,000
98
00:05:23,850 --> 00:05:26,740
to convert it from seconds to milliseconds
99
00:05:26,740 --> 00:05:30,030
and then here we got a number in milliseconds
100
00:05:31,230 --> 00:05:32,810
and there's a missing bracket,
101
00:05:32,810 --> 00:05:35,200
so here we got a number in milliseconds,
102
00:05:35,200 --> 00:05:38,960
which I add to the current timestamp in milliseconds
103
00:05:38,960 --> 00:05:41,510
and that then is added together
104
00:05:41,510 --> 00:05:44,358
and passed to new date again to construct a new date object
105
00:05:44,358 --> 00:05:46,720
and passed to new date again to construct a new date object
106
00:05:46,720 --> 00:05:49,707
from that timestamp in milliseconds.
107
00:05:51,310 --> 00:05:53,350
And we can actually also get rid of
108
00:05:53,350 --> 00:05:56,570
that outer pair of brackets here.
109
00:05:56,570 --> 00:06:01,570
This should then be the expiration time as a date object
110
00:06:01,570 --> 00:06:05,280
and I'll pass this here to the login function,
111
00:06:05,280 --> 00:06:10,280
the expiration time, and actually I'll pass it as a string.
112
00:06:10,910 --> 00:06:13,910
We could also pass it as a date object
113
00:06:13,910 --> 00:06:17,550
but since I do actually convert it
114
00:06:18,530 --> 00:06:22,570
to a date object here in my helper function already,
115
00:06:22,570 --> 00:06:25,480
I'll pass it as a string.
116
00:06:25,480 --> 00:06:27,470
Now that's just one possible approach.
117
00:06:27,470 --> 00:06:29,160
Definitely not the only one.
118
00:06:29,160 --> 00:06:32,240
And you can come up with a different one if you prefer it
119
00:06:32,240 --> 00:06:35,570
but that should set us that expiration time
120
00:06:36,850 --> 00:06:39,475
and it should set a timer that expires
121
00:06:39,475 --> 00:06:42,473
after one hour as a default.
122
00:06:44,780 --> 00:06:46,010
Now to verify this,
123
00:06:46,010 --> 00:06:50,650
I will temporarily manually change that timeout here
124
00:06:50,650 --> 00:06:54,520
and change that to just 3,000.
125
00:06:54,520 --> 00:06:55,983
So three seconds.
126
00:06:56,970 --> 00:07:00,590
And with that if I now go back and I log out,
127
00:07:00,590 --> 00:07:02,970
if I do log in again,
128
00:07:02,970 --> 00:07:07,760
we should be logged out automatically after three seconds
129
00:07:07,760 --> 00:07:09,570
and that's looking good.
130
00:07:09,570 --> 00:07:13,080
You see that the navigation changed after three seconds.
131
00:07:13,080 --> 00:07:15,040
I'll quickly try this again.
132
00:07:15,040 --> 00:07:18,420
I'll navigate to the profile page and after three seconds,
133
00:07:18,420 --> 00:07:19,703
we are navigated away.
134
00:07:20,590 --> 00:07:22,150
So that works and therefore,
135
00:07:22,150 --> 00:07:25,810
I'll now go back to the remaining time here
136
00:07:25,810 --> 00:07:28,960
but that's just a start because at the moment,
137
00:07:28,960 --> 00:07:31,900
we're not storing that expiration time in local storage,
138
00:07:31,900 --> 00:07:34,780
so if I ever log in and reload this page,
139
00:07:34,780 --> 00:07:37,300
we'll not set a new timer
140
00:07:37,300 --> 00:07:39,523
and we should, of course, be doing that.
141
00:07:40,390 --> 00:07:44,410
In addition, I also want to clear that timer here
142
00:07:44,410 --> 00:07:48,360
if the user logged out manually in the mean time
143
00:07:48,360 --> 00:07:50,070
because it doesn't make any sense
144
00:07:50,070 --> 00:07:53,840
to keep a timer around if the user did already log out.
145
00:07:53,840 --> 00:07:55,313
So let's work on that next.
11598
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.