Current User Endpoint

In this lesson, we're going to create an endpoint for current user

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:10] We have added one more property to the user model, which is the courses that they have purchased. We can return the courses with the user DTO, which we are returning when the user is logging in.

    [00:11 - 01:08] So what we can do is we can create another endpoint, which will be called current user, which will return the same user DTO, but they will make this request authorized so that only users who have logged in can make this request. So let's go to the user's controller. And create a new HTTP get request. So let's make it here. Let's make HTTP get request with name current user. And we are making this request an authorized request. So let's write authorized. This method will return user DTO. So let's write public async task of type action result. And from here, we are returning user DTO.

    [01:09 - 01:45] And let's call this method get current user. And inside we need a user for which we will call user manager. So here, let's write where user is equal to user manager dot find by name async. And we are going to use user dot identity dot name. We know it's not going to be null because it's an authorized request.

    [01:46 - 02:57] We also need a basket. So let's write where basket is equal to await extract basket. And again, we can pass user dot identity dot name. Now we need to get courses which were added to the user's account. If you remember, we have created a user courses table, which is working as a join between the user table and the cost table. That's right. A new variable called courses. And make this equal to context dot user courses dot as queryable. We need to make this variable because we want to use the wear condition on this. Now inside the user DTO. So let me create a new user DTO or we can simply copy the logic. And inside our current user method, since we are returning user DTO, let's also add basket, which will be simply the basket.

    [02:58 - 03:16] We are getting an error because we need to map it and return it. So let's use m apper dot map. We want to map it from the basket to the basket DTO. And now we can pass the basket.

    [03:17 - 03:51] And now finally, we can use courses. Now we can use this courses. And here we can use the wear. The user ID is equal to user dot ID, which means ID of this user. And from here , we can select the courses. So let me write dot select. And now I can select the course .

    [03:52 - 04:23] And we can make it a list because it's list of items. And I think we should go to user DTO. And here we need to add courses. So here, let's return list of course. And let's call it courses. And let's import list using system collections generic.

    [04:24 - 04:42] And also, course using entity. And here we see an error because we forgot to add a weight here. And we can return the same from the login function because here we are just returning the basket.

    [04:43 - 04:59] So we can simply copy the logic and paste it there. So let's start with the courses. And here we can write courses. You can also copy this logic.

    [05:00 - 05:14] And paste it here. By doing this, whenever a user locks in, the user will get all the courses they have purchased.

    [05:15 - 05:36] And they won't have to specifically call the get current user. They can simply get all the baskets, the courses whenever they log in. Now talking about client, let's go to the agent file. So here, let me write agent. And we have to create a new method for the current user.

    [05:37 - 05:53] So here we can write current user. And it's a get request. So I can write requests dot get. And the URL is user slash current user.

    [05:54 - 06:08] And this request will return a user. So I can write user here. Now we can create an async thunk function inside user slice. So let's go to the user slice.

    [06:09 - 07:01] And here we can create a new create async thunk function. And let's write export const fetch current user. And like always, it will be equal to create async th unk, which will return a user. And here we are not passing any arguments. The name can be user slash fetch current user. And now we can write a function. So async, since we don't need any data, let's use an underscore as a first argument and thunk API as the second one. And API should be all caps .

    [07:02 - 07:29] Now, so first of all, what we can change is change the name of get user to set user, because now we are going to change some logic. So let's call it set user and also this to be set user. And inside the function, we need to include the action and removing this logic.

    [07:30 - 08:29] We want state dot user to be equal to action dot payload. And inside of H current user function, we can dispatch the set user. So thunk API dot dispatch. And here I can dispatch set user. And here we can pass local storage dot get item with the key user. And since we want to pass it, we can cut it and write Jason dot parse and pass this. We see an error because it can be null. So let's pass an extra mission mark. Now let's use the try catch block for the asynchronous requests.

    [08:30 - 09:05] So let's write try and catch errors. And we are simply logging the errors. Now coming back to the try block, first of all, we will write const user dto to be equal to await agent dot users dot current user. And now we have also included courses.

    [09:06 - 09:36] So we have to go to the user model and include courses property, which is again going to be optional because it can be null. And it should be array of course. So let's also import course. Coming back to the user slice. Now let's de structure everything from our user dto which should be const. So we want to de structure the basket courses.

    [09:37 - 10:35] And everything else is a user. So here we can write user dto. Now first of all, we want to check if there is basket, which means basket is not null. We want to dispatch the set basket function with this basket. The same way we want another reducer function which can set user courses. So what we can do is we can create another state property called user courses, which will be of type course array. And let 's import course model from the modules. And the initial property can be an empty array. Now we can create another reducer function. Let's do it here. Let's call it set user courses.

    [10:36 - 11:07] And again, we need state and action. Now we want to set state dot user courses to be action dot payload. And now what we can check is if there are courses. So if courses are not null, we want to dispatch the set user courses.

    [11:08 - 11:42] We don't see it because we have to export it. So let's write set user courses. And now if we write set user courses, it's available and we can pass the courses. Finally, we can set it inside our local storage. So I can write local storage dot set item. And the key is going to be user. And the value is Jason dot stringify the user. And finally, we will return the user.

    [11:43 - 12:14] And rather than logging it, you want to return thunk API dot reject with value. And inside, we can mention error to be the error. Now looking at our extra reducers, the fulfill state can be same. So what we can do is here we can mention fetch current user dot fulfilled because it is also returning a user. So the logic is same here.

    [12:15 - 13:09] We will create a new add case for the rejected state. So when it's ending, let me write a new add case, which is builder dot add case. And this is for fetch current user dot rejected. And after this, we will mention the state. Inside, we want to set the user to be null. So state dot user to be null. We want to remove the token from the local storage. So local storage dot remove item with the key user. And since it's failing, we can show a notification, which can say session has been expired. So let me write notification. Let's import it from Andy dot error .

    [13:10 - 14:24] And the message can be session has been expired, because this will happen only when the key has been expired. And the user has to log in again. Also, we need to call the fetch current user function only when there is token. So we can add a condition here. So let's check if let me write the condition. And if there is no token, so I can write if there is no local storage dot, get item user, we can simply return false. And let's check if everything is fine. And one more thing, the add case should always come before add match. So let's simply cut it from here and paste it on top. Now coming back to the app dot p six file, rather than using get user, we can use fetch current user. So let's delete this. And here we can write fetch current user.