Is there like a Laravel Passport in rails?

Viewed 300

I'm just curious about the api security in rails. I just want to ask if there is like a laravel passport in rails?

For those who does not know about Laravel Passport, I have a brief description below:

APIs typically use tokens to authenticate users and do not maintain session state between requests. Laravel makes API authentication a breeze using Laravel Passport, which provides a full OAuth2 server implementation for your Laravel application in a matter of minutes. Passport is built on top of the League OAuth2 server that is maintained by Andy Millington and Simon Hamp.


Here is my rails login codes, does the session[:user_id] is enough to authenticate and to check authenticated user?

user = User.find_by(email: params[:email])

if user && user.authenticate(params[:password])
  session[:user_id] = user.id
  render json: { code: 200, data: user }, status: :ok
end
1 Answers

The de facto gem for authentication in Rails is devise. If you are building a web application, then devise is the answer to all your authentication needs, including out of the box support for OmniAuth.

If you are building an API however, then you'll have to use a token based authentication system. One of the more popular gems for token based authentication, based on the devise gem itself, is devise_token_auth.

In fact, setting up a hybrid approach, with your web pages using devise and your APIs using devise_token_auth is pretty straight forward to set up and works beautifully. I've created a sample project that does just this here.

Related