DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,673 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Drew Pellum
Drew Pellum

Posted on

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

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.