1
00:00:02,070 --> 00:00:05,410
So let's start this refactoring process

2
00:00:05,410 --> 00:00:07,993
and I'll start by splitting up my routes.

3
00:00:09,180 --> 00:00:11,830
I'll add a new auth.js file

4
00:00:11,830 --> 00:00:14,320
and I'll keep the blog.js file,

5
00:00:14,320 --> 00:00:17,230
but now the blog.js file should only contain the routes

6
00:00:17,230 --> 00:00:20,300
that are blog related, post related,

7
00:00:20,300 --> 00:00:23,903
and auth.js should contain all the auth routes.

8
00:00:25,270 --> 00:00:29,670
So I'll start by copying blog.js, the content in this file,

9
00:00:29,670 --> 00:00:32,110
and pasting this into auth.js.

10
00:00:32,110 --> 00:00:35,450
And now in auth.js, I'm going to remove all the logic

11
00:00:35,450 --> 00:00:37,173
that's related to the blog.

12
00:00:38,580 --> 00:00:41,750
Now, we can argue about this welcome page,

13
00:00:41,750 --> 00:00:45,060
but I would argue it belongs more to the blog part

14
00:00:45,060 --> 00:00:47,700
than to the auth part because I really wanna have

15
00:00:47,700 --> 00:00:50,750
just the authentication-related routes in here,

16
00:00:50,750 --> 00:00:52,093
so I will delete that.

17
00:00:52,930 --> 00:00:56,030
Of course, I'll keep the get signup route here

18
00:00:56,030 --> 00:01:01,030
and get login and post signup and post login

19
00:01:01,080 --> 00:01:03,783
because these are all auth related,

20
00:01:05,500 --> 00:01:08,590
but then I will remove the admin route

21
00:01:08,590 --> 00:01:12,010
because that is primarily about displaying posts

22
00:01:12,010 --> 00:01:14,673
and the post form, so I'll delete that.

23
00:01:16,270 --> 00:01:19,470
I'll keep the post logout route, of course,

24
00:01:19,470 --> 00:01:22,160
but I am going to delete all the other routes

25
00:01:22,160 --> 00:01:23,950
because all these other routes

26
00:01:23,950 --> 00:01:27,140
are only related to managing posts.

27
00:01:27,140 --> 00:01:29,460
So I'll delete all of that

28
00:01:29,460 --> 00:01:34,460
so that the finished auth.js file has the logout route.

29
00:01:35,500 --> 00:01:39,550
And here in VS Code, you can kind of minimize this,

30
00:01:40,590 --> 00:01:41,510
so it's still there,

31
00:01:41,510 --> 00:01:43,870
but it's now easier to see all the routes.

32
00:01:43,870 --> 00:01:48,423
I got the logout route here, then I got the login route,

33
00:01:49,330 --> 00:01:53,960
then I got the signup post route,

34
00:01:53,960 --> 00:01:57,120
and I got the login get route,

35
00:01:57,120 --> 00:01:59,700
and I got the signup get route.

36
00:01:59,700 --> 00:02:04,290
This is now my finished auth.js file.

37
00:02:04,290 --> 00:02:05,600
And all the code is still there.

38
00:02:05,600 --> 00:02:08,889
I just minimized that to bring it all into one screen.

39
00:02:08,889 --> 00:02:12,250
And I don't need the object ID thing here anymore

40
00:02:12,250 --> 00:02:15,570
because we only use that for working with posts,

41
00:02:15,570 --> 00:02:19,010
so it's not required in the auth.js file anymore

42
00:02:19,010 --> 00:02:21,030
because every file should only bring

43
00:02:21,030 --> 00:02:23,170
what it needs inside of that file,

44
00:02:23,170 --> 00:02:26,340
not what's needed in other files.

45
00:02:26,340 --> 00:02:30,360
And I can therefore also get rid of that MongoDB import.

46
00:02:30,360 --> 00:02:32,490
You can see it's slightly grayed out

47
00:02:32,490 --> 00:02:35,523
and that tells you that it's not being used in this file.

48
00:02:36,870 --> 00:02:39,633
So this is the finished auth.js file.

49
00:02:40,760 --> 00:02:43,870
In blog.js, I'm going to do the opposite.

50
00:02:43,870 --> 00:02:48,480
Here, I'm going to drop all the auth.js related logic.

51
00:02:48,480 --> 00:02:52,570
And that, for example, starts with the bcrypt import

52
00:02:52,570 --> 00:02:54,930
because I know that I'm only using bcrypt

53
00:02:54,930 --> 00:02:56,890
for password management

54
00:02:56,890 --> 00:02:59,610
and therefore this won't be related to posts,

55
00:02:59,610 --> 00:03:00,913
so we can delete that.

56
00:03:02,850 --> 00:03:04,500
I will keep the welcome route here

57
00:03:04,500 --> 00:03:07,430
because we deleted it in the auth.js file,

58
00:03:07,430 --> 00:03:11,260
but I will delete this get signup route

59
00:03:11,260 --> 00:03:13,660
and I will delete the get login route

60
00:03:13,660 --> 00:03:17,970
and the post signup route and the post login route

61
00:03:17,970 --> 00:03:20,090
because these are all auth routes.

62
00:03:20,090 --> 00:03:21,553
I'll delete all of them,

63
00:03:22,920 --> 00:03:25,300
but I'll keep the get admin route here

64
00:03:25,300 --> 00:03:29,160
because that is related to posts.

65
00:03:29,160 --> 00:03:32,210
And I will delete the logout route,

66
00:03:32,210 --> 00:03:34,403
but keep all the other routes.

67
00:03:35,720 --> 00:03:39,440
So if I minimize that here as well, I got that home route,

68
00:03:39,440 --> 00:03:44,440
the admin route, the post posts route for adding a new post,

69
00:03:44,980 --> 00:03:47,010
the get posts id edit route

70
00:03:47,010 --> 00:03:49,580
for loading a single post page,

71
00:03:49,580 --> 00:03:52,240
the post route for editing a single post,

72
00:03:52,240 --> 00:03:54,830
and this delete route for deleting a post.

73
00:03:54,830 --> 00:03:57,700
That's now the route setting

74
00:03:57,700 --> 00:04:01,823
or the routes that I wanna have for my blog.js file.

75
00:04:03,850 --> 00:04:06,090
And here are the imports.

76
00:04:06,090 --> 00:04:08,810
Now, I split that into two files,

77
00:04:08,810 --> 00:04:12,930
but, of course, I still want to register all the routes.

78
00:04:12,930 --> 00:04:14,880
So we need to go to app.js

79
00:04:14,880 --> 00:04:19,230
and there I'm currently including only the blog.js file.

80
00:04:19,230 --> 00:04:21,810
Well, now we simply need to add a new constant,

81
00:04:21,810 --> 00:04:26,810
maybe called authRoutes where I require ./routes/auth.

82
00:04:29,150 --> 00:04:32,320
So I'm including both route files now.

83
00:04:32,320 --> 00:04:34,000
And now we just need to make sure

84
00:04:34,000 --> 00:04:37,740
the requests reach all the routes in both files.

85
00:04:37,740 --> 00:04:40,050
So therefore, further down below,

86
00:04:40,050 --> 00:04:42,620
after registering all the main middleware,

87
00:04:42,620 --> 00:04:45,140
I don't just wanna use my blog routes,

88
00:04:45,140 --> 00:04:48,113
but also my auth routes here.

89
00:04:50,060 --> 00:04:53,240
Now, which one you register first, it doesn't matter here.

90
00:04:53,240 --> 00:04:55,930
It would only matter if you had the same path

91
00:04:55,930 --> 00:04:58,870
and the same route defined in both files,

92
00:04:58,870 --> 00:05:02,040
then the first file to be registered would win,

93
00:05:02,040 --> 00:05:03,480
but it would be very rare

94
00:05:03,480 --> 00:05:06,450
that you wanna have the same route in multiple files.

95
00:05:06,450 --> 00:05:09,400
It wouldn't make too much sense in most cases.

96
00:05:09,400 --> 00:05:11,030
So therefore in most cases,

97
00:05:11,030 --> 00:05:14,453
the order of registering your routes doesn't matter.

98
00:05:16,730 --> 00:05:20,010
Okay, so with that, if we quickly test this,

99
00:05:20,010 --> 00:05:22,010
we can still navigate around

100
00:05:22,010 --> 00:05:25,390
and I'll reach the signup and login pages.

101
00:05:25,390 --> 00:05:29,413
So that's still working, but now we refactored our routes.

