课程: React: Creating and Hosting a Full-Stack Site (2022)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Protecting the upvote and comment endpoints

Protecting the upvote and comment endpoints

- [Instructor] The next two endpoints are going to be a little bit trickier, I believe. So, first of all, let's take a look at how to refactor this up vote endpoint so that we make sure that only users who haven't up-voted the article before can do so. Well, first of all, one thing that we should notice is that the up vote endpoint and the comments endpoint are both going to have the same initial criteria. And that is, if the user isn't already logged in we don't want the user to be able to make requests to either of these endpoints. So, what we can actually do is add another piece of middleware right above these two routes that will only apply to these two routes. And that's going to look like this. We're just going to say app dot use. And then we're going to say request response and next. And for the body of this we're going to say, if request dot user, right? In other words, if the user exists and has included the…

内容