Important things to know when creating authorization with sessions

apellum

Drew Pellum

Posted on November 4, 2021

Important things to know when creating authorization with sessions

When creating authorization, there are some important setup steps to make sure that your sessions save.

Setup

The backend setup is crucial if you want to be able to save your sessions.

Cors: Make sure that you have enabled the cors gem and also set up cors.rb

Cors

It is important to make sure that “credentials” are true.
You also need to make sure that your application.rb file is set up

application.rb

The important lines of code are lines 25, 26, 30, and 33. Lines 25 and 26 must be above 30 and 33.

Make sure to create your session routes and create your session controller.

Your session custom routes will need to be a post and a destroy.

post ‘/login’, to: ‘sessions#create’
delete ‘/logout’, to: ‘sessions#destroy’

In the sessions controller your create method will look something like this

Sessions Controller

On your front-end, you will need to use withCredentials: true in your fetches

withCredentials

With this set up, you should be able to refresh the page and stay logged in

💖 💪 🙅 🚩
apellum
Drew Pellum

Posted on November 4, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related