Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # please improve this rails action,
- # if you'd like to put some code in another file please state so:
- # authentication API
- def auth
- user = User.find(params[:username])
- if user.check_password(params[:password])
- render json: user
- else
- render json: { errors: ["wrong username or password"] }, status: :unauthorized
- end
- end
- ######################################
- #
- # my Code
- #
- before_action :find_user, only: :auth
- def auth
- user_valid = @user.check_password(params[:password])
- if user_valid
- render json: @user
- else
- render json: { errors: @user.errors }, status: 401
- end
- end
- private
- def find_user
- @user = User.find(params[:username])
- render json: { errors: ["User not found, please check your username"] }, status: 401 unless @user
- end
- ######################################
- #
- # this is user.rb model
- #
- def check_password password_from_params
- #
- # if we use devise we can do this
- #
- # if valid_password?(password_from_params)
- # true
- # else
- # self.errors.add(:password, "wrong password")
- # end
- if password.eql? password_from_params
- true
- else
- self.errors.add(:password, "wrong password")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement