Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,130 --> 00:00:03,640
So we have this problem
2
00:00:03,640 --> 00:00:05,689
that we stay on this profile page
3
00:00:05,689 --> 00:00:07,180
even if we log out.
4
00:00:07,180 --> 00:00:09,260
But we have a bigger problem,
5
00:00:09,260 --> 00:00:12,260
if I'm not logged in as I'm currently am,
6
00:00:12,260 --> 00:00:13,770
I'm not logged in here,
7
00:00:13,770 --> 00:00:18,770
I can already manually enter /profile in the URL
8
00:00:19,170 --> 00:00:21,580
and we load this page,
9
00:00:21,580 --> 00:00:23,923
even though we should not be able to visit it.
10
00:00:24,810 --> 00:00:27,130
Now of course, it's not a big problem
11
00:00:27,130 --> 00:00:30,930
because if I try to change the password here,
12
00:00:30,930 --> 00:00:33,760
this will fail, it will simply fail
13
00:00:33,760 --> 00:00:38,760
because we did provide an invalid token in this case,
14
00:00:38,950 --> 00:00:41,330
null to be precise.
15
00:00:41,330 --> 00:00:45,490
So we can't do any harm, but we still might want to ensure
16
00:00:45,490 --> 00:00:49,860
that we can't visit this page if we are not logged in.
17
00:00:49,860 --> 00:00:53,940
And we can achieve this by adding a feature
18
00:00:53,940 --> 00:00:56,900
which you could call navigation guards.
19
00:00:56,900 --> 00:00:59,480
And this sounds bigger than it actually is.
20
00:00:59,480 --> 00:01:02,110
It's just about dynamically changing
21
00:01:02,110 --> 00:01:05,040
our route configuration here
22
00:01:05,040 --> 00:01:08,020
based on whether we are logged in or not,
23
00:01:08,020 --> 00:01:10,840
because that is something that we can do.
24
00:01:10,840 --> 00:01:13,090
Keep in mind that we can only visit routes
25
00:01:13,090 --> 00:01:18,030
which are defined here in our App JS file in this app.
26
00:01:18,030 --> 00:01:22,570
Now, if I render these routes conditionally,
27
00:01:22,570 --> 00:01:24,560
only under certain conditions,
28
00:01:24,560 --> 00:01:27,430
then of course we can only visit a certain route
29
00:01:27,430 --> 00:01:29,223
if that condition is true.
30
00:01:30,090 --> 00:01:32,270
And that means that we could, of course,
31
00:01:32,270 --> 00:01:35,800
use the current authentication status of the user
32
00:01:35,800 --> 00:01:38,760
as a condition for certain routes.
33
00:01:38,760 --> 00:01:43,690
And we could choose to not render this user profile route
34
00:01:43,690 --> 00:01:47,940
configuration at all if the user is not authenticated.
35
00:01:47,940 --> 00:01:50,860
In which case there's no way for the user to visit
36
00:01:50,860 --> 00:01:55,000
this page if he or she is not authenticated.
37
00:01:55,000 --> 00:01:57,083
And that's exactly what I'll do here.
38
00:01:58,530 --> 00:02:01,660
For this, we need access to our authstatus
39
00:02:01,660 --> 00:02:04,380
which is stored in the context,
40
00:02:04,380 --> 00:02:08,050
and therefore of course we need to use context hook
41
00:02:08,050 --> 00:02:12,570
from React and then here in the app component
42
00:02:12,570 --> 00:02:17,480
we can call use context and tap into this auth context
43
00:02:17,480 --> 00:02:20,993
which we again have to import, just as before,
44
00:02:22,150 --> 00:02:24,143
and then we get our auth context here.
45
00:02:25,560 --> 00:02:30,037
And in this auth context, keep in mind we also have this
46
00:02:30,037 --> 00:02:33,170
isLoggedIn field, which tells us whether the user
47
00:02:33,170 --> 00:02:34,683
is logged in or not.
48
00:02:35,530 --> 00:02:37,950
And now my goal is simple,
49
00:02:37,950 --> 00:02:42,380
if the user is not logged in, this route configuration
50
00:02:42,380 --> 00:02:46,320
for the user profile should simply not be rendered,
51
00:02:46,320 --> 00:02:49,800
it should not be part of this route configuration,
52
00:02:49,800 --> 00:02:52,970
or put in other words, this route configuration
53
00:02:52,970 --> 00:02:57,050
should only be added if the user is logged in.
54
00:02:57,050 --> 00:02:59,180
And therefore, we can render this conditionally
55
00:02:59,180 --> 00:03:02,380
because this is still just a component
56
00:03:02,380 --> 00:03:03,730
that's being rendered.
57
00:03:03,730 --> 00:03:06,660
It might sound weird that we conditionally change
58
00:03:06,660 --> 00:03:08,020
the route configuration,
59
00:03:08,020 --> 00:03:10,290
but since this is just a component,
60
00:03:10,290 --> 00:03:11,600
that is something we can do
61
00:03:11,600 --> 00:03:14,603
and that is a standard way of solving this problem.
62
00:03:15,600 --> 00:03:19,890
So here I can then check if authCtx is logged in
63
00:03:19,890 --> 00:03:22,820
and if that's the case, I render this route,
64
00:03:22,820 --> 00:03:24,313
otherwise, I don't.
65
00:03:25,870 --> 00:03:27,760
Of course we could also make sure
66
00:03:27,760 --> 00:03:32,270
that we therefore only grant access to the auth page
67
00:03:32,270 --> 00:03:35,050
if the user is not logged in,
68
00:03:35,050 --> 00:03:37,260
because if the user is logged in,
69
00:03:37,260 --> 00:03:40,960
visiting the auth page makes no sense.
70
00:03:40,960 --> 00:03:45,570
So then here we can check if not authCtx is logged in,
71
00:03:45,570 --> 00:03:49,463
in which case we grant access to that auth page.
72
00:03:51,280 --> 00:03:54,360
We now also definitely wanna handle the case
73
00:03:54,360 --> 00:03:56,930
that the user enters /profile,
74
00:03:56,930 --> 00:03:59,970
but this configuration is not rendered,
75
00:03:59,970 --> 00:04:04,520
or that in general, the user enters some invalid path.
76
00:04:04,520 --> 00:04:08,190
And we of course can do that by adding another route config
77
00:04:08,190 --> 00:04:10,270
which is always rendered,
78
00:04:10,270 --> 00:04:14,750
which handles this wild card case,
79
00:04:14,750 --> 00:04:17,279
so basically anything the user entered,
80
00:04:17,279 --> 00:04:20,779
if it has not been handled by one of the other routes
81
00:04:20,779 --> 00:04:24,790
and here we could then render a 404 page,
82
00:04:24,790 --> 00:04:28,220
or what I will do here, we redirect the user,
83
00:04:28,220 --> 00:04:32,200
and we can do that with help of that redirect component
84
00:04:32,200 --> 00:04:35,053
which react-router-dom provides to us.
85
00:04:36,010 --> 00:04:41,010
We can simply redirect the user here
86
00:04:41,060 --> 00:04:45,340
to just slash, let's say.
87
00:04:45,340 --> 00:04:46,330
And that will kick in
88
00:04:46,330 --> 00:04:48,780
whenever the user enters something invalid
89
00:04:48,780 --> 00:04:53,763
or if the user enters /profile without being logged in.
90
00:04:55,030 --> 00:04:56,450
Now let's give it a try.
91
00:04:56,450 --> 00:05:00,260
If we save this and I reload, I'm not logged in
92
00:05:00,260 --> 00:05:03,230
because we currently lose our log in status
93
00:05:03,230 --> 00:05:06,190
every time we reload this page,
94
00:05:06,190 --> 00:05:07,950
this is something we'll change soon,
95
00:05:07,950 --> 00:05:10,410
but at the moment it works like this.
96
00:05:10,410 --> 00:05:12,860
And if I now enter /profile,
97
00:05:12,860 --> 00:05:15,283
you see I'm back here on the starting page.
98
00:05:17,100 --> 00:05:19,470
So I am redirected back here.
99
00:05:19,470 --> 00:05:22,000
I can go to the log in page though
100
00:05:22,000 --> 00:05:26,530
and if I then do log in, I can visit /profile,
101
00:05:26,530 --> 00:05:28,090
this now works.
102
00:05:28,090 --> 00:05:31,000
Now if I enter it manually, we reload the page in the end
103
00:05:31,000 --> 00:05:33,700
and therefore we can't visit it in this case,
104
00:05:33,700 --> 00:05:36,410
but again that is something we'll fix soon.
105
00:05:36,410 --> 00:05:40,050
But now we are protecting our routes here.
106
00:05:40,050 --> 00:05:43,520
Now of course you could change this slightly,
107
00:05:43,520 --> 00:05:46,730
you could say that visiting /profile
108
00:05:46,730 --> 00:05:48,820
should always be possible
109
00:05:48,820 --> 00:05:51,270
and you just change conditionally
110
00:05:51,270 --> 00:05:53,430
which page is being rendered.
111
00:05:53,430 --> 00:05:57,210
And if the user is logged in and /profile is rendered,
112
00:05:57,210 --> 00:05:59,323
you visit the user profile page,
113
00:06:00,260 --> 00:06:04,280
otherwise if not authCtx is logged in,
114
00:06:04,280 --> 00:06:07,890
so if the user is not logged in, you redirect
115
00:06:09,240 --> 00:06:14,070
and you redirect to /auth for example, in this case.
116
00:06:14,070 --> 00:06:18,710
Then you have this general redirect or 404 page
117
00:06:18,710 --> 00:06:21,920
for anything invalid, but if the invalid thing
118
00:06:21,920 --> 00:06:25,380
is /profile, then you redirect to auth
119
00:06:25,380 --> 00:06:27,360
if the user is not logged in,
120
00:06:27,360 --> 00:06:31,230
or if the user is logged in, you render user profile.
121
00:06:31,230 --> 00:06:35,100
And that's just an alternative, nothing you have to do.
122
00:06:35,100 --> 00:06:37,550
But now with this change made, for example,
123
00:06:37,550 --> 00:06:42,550
if I now enter /profile here, I'm redirected to /auth,
124
00:06:42,870 --> 00:06:45,040
if I'm not logged in
125
00:06:45,040 --> 00:06:47,560
and on the other hand if I do log in,
126
00:06:47,560 --> 00:06:50,660
I can visit this profile page.
127
00:06:50,660 --> 00:06:52,970
So you can really get creative here
128
00:06:52,970 --> 00:06:56,400
and fine tune this route configuration as you need it
129
00:06:56,400 --> 00:06:59,333
based on the authentication status.
10213
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.