-1

I want to do authenticate API using grape. For auth I used Devise gem. I try include devise::sessioncontroller into my grape api file but it's caput.

class SignIn < BaseAPI
  resource :sign_in do
    desc 'Sign in page'
    params do
      requires :username, type: String
    end
    post do
      User.authenticate(params)
    end
  end
end

1 Answers1

0

Try the following code. You should be able to authenticate. There are a few extra things you need to set up. Follow this document for more details.

resource :sign_in do
  desc "Authenticate user"
  params do
    requires :login, type: String
    requires :password, type: String
  end
  post :login do

    user = User.find_by_email(params[:login].downcase)
    if user && user.authenticate(params[:password])
      token = TokenGenerator.create(user_id: user.id)
      {token: token.access_token}
    else
      error!('Unauthorized.', 401)
    end

  end
end