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
- before_action :set_user, only [:show, :edit, :update, :destroy]
- def auth
- success = ->(user) { render json: user }
- error = -> { render json: { errors: ["wrong username or password"] }, status: :unauthorized }
- UseCase::User::CheckPassword.new.call(user, params, success: success, failure: error)
- end
- private
- def set_user
- @user = repo.find(params[:username])
- end
- def repo
- @repo ||= UserRepository.new(params)
- end
- #app/repositories/user_repository.rb
- class UserRepository
- def new_entity(attrs = nil)
- User.new(attrs)
- end
- def find(attr)
- (attr.is_a?(String)) ? User.find_by_username(attr) : User.find(attr)
- end
- def check_password(attr)
- end
- end
- # app/use_cases/use_case/base.rb
- module UseCase
- class Base
- attr_reader :repository
- def initialize(repo=nil)
- @repository = repo
- end
- end
- end
- # app/use_cases/use_case/user/check_password.rb
- module UseCase
- module User
- class CheckPassword < UseCase::Base
- def call(user, attrs, callbacks)
- user.check_password(attrs[:password]) ? callbacks[:success].call(user) : callbacks[:failure].call()
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement