I'm trying to make a login system for my project using the devise, I chose to do for him, because this project will have access and http api.
For now I can register a User using the following method of Registration class:
def create
if UserService.create_user(params[:username], params[:email])
@user = User.new(user_params)
if @user.save
render json: @user, status: :created
return
end
render json: @user.errors, status: :unprocessable_entity
return
end
render json: "exit", status: :unprocessable_entity
end
and that is the model of my User note that it has all the things that devise adds:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :schedules
end
my problem is in session the class because he can not find the include Devise::InternalHelpers
:
class Api::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, only: [:create]
include Devise::InternalHelpers
before_filter :ensure_params_exist
def create
build_resource
resource = User.find_for_database_authentication(email: params[:email])
return invalid_login_attempt unless resource
if resource.valid_password?(password: params[:password])
sign_in("user", resource)
render(json: {success: true, auth_token: resource.auth_token, login: resource.email})
return
end
invalid_login_attempt
end
def destroy
sign_out(resource_name)
end
protected
def ensure_params_exist
return unless params[:email].blank?
render(json: {success: false, message: "missing email parameter"}, status: 422)
return unless params[:password].blank?
render(json: {success: false, message: "missing password parameter"}, status: 422)
end
def invalid_login_attempt
warden.custom_failure!
render(json: {success: false, message: "Error with your login or password"}, status: 401)
end
private
def create_params
params.permit(:email, :password)
end
end
Another problem is that by the time creates the User he is not generating the token, is as it should work?
Well this is my mistakes how to do this InternalHelpers for API?
Devise::InternalHelpers
has been removed. You should include Devise::Controllers::Helpers instead:
class Api::SessionsController < Devise::SessionsController
prepend_before_filter :require_no_authentication, only: [:create]
include Devise::Controllers::Helpers
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.