2

I use Grape to implement an API service of my app. But how can I call devise method sign_in in Grape?

WildCat
  • 1,961
  • 6
  • 24
  • 35

2 Answers2

0

In the Grape API base

# Enable session middleware for auth: https://stackoverflow.com/a/35428068/2771889
use ActionDispatch::Session::CookieStore
helpers do
  def session
    env['rack.session']
  end
end

The API

helpers Devise::Controllers::SignInOut

resource :users do
  # ...
end

Note that this adds the Session layer to the API which will cause some overhead that Grape by default would avoid. For a full auth solution see: https://stackoverflow.com/a/70683351/2771889

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
-1

You need to write API which will post user_name & password and return user toke in API response

As

http://localhost:3000/api/v0/auth/sign_up

POST parameter

user[email]=p@p.com
user[password]=p123

In AuthController

def sign_up
   #create user and user token 
   render json: { token: <generated user token>}  
end  

You migth need to write similar api for sign_in as well Use this token for subsequent API call to authorize user

Pramod Shinde
  • 1,802
  • 1
  • 15
  • 28