DEV Community

AnthonyH00
AnthonyH00

Posted on

User Authentication, Rails Backend

Ever wonder how that sign-in window, login screen, and/or authentication request is created? I definitely did. It would be more accurate to say, prior to my coding bootcamp, I attempted to learn coding on my own. Authentication was one of the topics, I found myself being really confused with. I didn't understand it, and couldn't find the exact information I was looking for. Armed with a little more knowledge (and practice), I hope this will help you better understand how to implement user Authentication, focusing on Rails backend.

First, let's define Authentication. Whenever you attempt to login to a program or application, your credentials are checked against the server it was setup/monitored on. If the check passes, your account is authenticated and your account will login. Basically, it's confirming you are the account that you're logging into.
Now that we have defined Authentication. How can we setup code that implements it? There are several ways to do this. I prefer using sessions, so we'll implement this process now.
Step 1:
First ensure a SessionController is setup. Plenty of ways to create a SessionController, but I use the terminal command,'rails g controller Sessions'. This will be used to check the account credentials being typed and the actions that follow. See this Ex:
Image description
using data from your database, along with the params hash and session ID, I setup a cookie in the session hash to handle the action for checking to see if the information typed matches the credentials in the database.
Step 2:
Once the controller has been set, you'll have to create the POST route which will submit the user inputted form on the frontend, to the route specified. See the following EX:
Image description

Step 3:
Ensure error checking is implemented. So when a user types an incorrect username and/or password combination, they'll receive a descriptive error.

There are more steps to the Authentication process on the frontend (navigating to the route/site, entering credentials, etc) but I wanted to give an overview of the backend steps that I've used that works. Personally, finding this information had been difficult at times, as I was just learning the material. I hope this can be of some reference for all beginners.

Top comments (0)