I use Grape to implement an API service of my app. But how can I call devise method sign_in in Grape?
Asked
Active
Viewed 696 times
2
WildCat
- 1,961
- 6
- 24
- 35
-
Please look into this http://stackoverflow.com/questions/26623980/user-authentication-with-grape-and-devise – rick Apr 27 '15 at 14:29
-
1@rick , thanks. But I do not want to use token. And MY QUESTION IS how to auth user with username and password, which is before generating token. – WildCat Apr 27 '15 at 14:31
-
@WildCat, You figured out how to do it? – Artyom Zankevich May 13 '15 at 17:38
2 Answers
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