Strapi Testing User Login/Logout

Josh burger
5 min readNov 13, 2021

Last week I published a blog about creating strapi collections. This was a way to teach you a little about how collections work using strapi. Today I am going to continue the blog by creating a collection user and adding a new user to the database. After, I will then test logging the user in and logging the user out with endpoints.

I'm a little ahead of the game with creating users and the attributes/fields that I want for a user. In this blog, all we need for a user is the email and password to create and test. Go ahead and create a new collection type user, with the fields of email, password, then username. Once you do that, save it to the database.

After you have created your user collection type and saved it, you can now go back to the main screen in the dashboard. When at the main screen click on the user's collection type on the left-hand side. When you get there you should see that we have no users created in our database.

At this point now we can create a new user and test the user's login and logout. Let us get started with creating a single user with an email and password. In the top right corner click on “Add New Users”. You might notice that I have a lot more fields for my users no need to worry about them. Just worry about filling out the email, password, and username.

So now that we have our user's email, password, and username set, we also have a confirmed button we want to toggle to let our backend know the user is confirmed. The same goes for the Role on the right-hand side. The role allows permissions to have the user authenticated or public. If I have all my routes authenticated then my user would have to log in to have access to that route. If my user is set to public, they will not have access to authenticated routes.

After it is all set and done, click Save in the top right corner to save the user in the database. It should tell you that the user saved, if not it will tell you what fields are required to fill out. Once saved click back on the user's collection type to see a list of all our users.

Now that we have a user in our database, we can try to log that user in using postman and endpoints. The endpoints we will be using today are “http://localhost:1337/auth/local” and “http://localhost:1337/auth/logout

The first one is for logging the user in, the second one is for logging the user out.

To test these endpoints I choose to use postman so that we can see the response when we log in and log out. If you don't have “postman” you can install and setup at this link. “https://www.postman.com/

One postman has installed and started up the first thing we want to do is test the first endpoint with our newly created user. This request will be a post request because we want to log the user in.

When you have the correct endpoint and request set, we then want to come down into the body of the request. Click on raw then switch to JSON at the end. We are going to create our own object to send with the request at the endpoint, which will be our user name and password.

Now inside of the raw JSON we want to send with post request, we need to create an object with the user's NAME and PASSWORD. Whatever you named the user and set the password to be is what we will use to send with the request.

Once the request is sent we should then receive a promise that we can look at and inspect to see if the user was logged in or not.

As you can tell once I send the request this is what I got back from it. A status of 200 is successful. I can also see the status of Authorized, as well as the user's id, email, and role of authenticated. After the user is logged in and we know they are, we then want to log that user out.

This route is a custom route for logging a user out. You can create custom routes for your application using strapi. I'm not going into how to create custom routes but I will link the documentation for learning how. “https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#routing

This is how our custom logout route works and looks like

With the custom route in place, we now have the ability to log the user out. If we go back to postman and create another endpoint to http://localhost:1337/auth/logout and make a get request we should now receive a message that our user has successfully logged out.

After we send the GET request to the specific endpoint, we get a message saying “Successfully logged out”. This is how to get you up and going with creating a user and testing if you can log the user in and out. I hope you enjoyed my blog and learned something today. If you haven't read my other blog about creating collections in strapi I’ll have the link below.

--

--