Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: login! method #327

Open
Spone opened this issue Oct 27, 2022 · 3 comments · May be fixed by #332
Open

Feature request: login! method #327

Spone opened this issue Oct 27, 2022 · 3 comments · May be fixed by #332
Labels
enhancement New feature or request to be implemented in v1 This issue or pull request will be resolved in the v1 rework, but has not yet been completed.

Comments

@Spone
Copy link
Contributor

Spone commented Oct 27, 2022

I'd like to suggest adding a login! method, that would behave like login except it would raise an exception if the login fails.

This is similar to the save / save! methods in Rails.

It would be helpful in cases where you want to code the "happy path", and handle exceptions separately. For instance instead of:

class Api::V1::SessionsController < ApplicationController
  def create
    if login(params[:email], params[:password])
      render json: { message: "You're logged in!" }
    else
      render json: { message: "Wrong credentials" }
    end
  end
end

you could have:

class Api::V1::SessionsController < ApplicationController
  rescue_from Sorcery::InvalidCredentials, with: :invalid_credentials

  def create
    login!(params[:email], params[:password])
    
    render json: { message: "You're logged in!" }
  end

  private

  def invalid_credentials
    render json: { message: "Wrong credentials" }
  end
end

A few notes:

  • I used render json for the sake of simplicity but view templates would be used instead
  • Sorcery::InvalidCredentials is just a suggestion, I'm open to ideas for a better name
@joshbuker joshbuker added enhancement New feature or request to be implemented in v1 This issue or pull request will be resolved in the v1 rework, but has not yet been completed. labels Oct 28, 2022
@Spone
Copy link
Contributor Author

Spone commented Nov 2, 2022

@joshbuker would you be open to reviewing a PR for this? I can allocate some time to preparing a PR.

@joshbuker
Copy link
Member

@Spone Yes, if you create a PR, ping me on it, and I'll try to set aside time to work on it.

@Spone Spone linked a pull request Jan 18, 2023 that will close this issue
3 tasks
@Spone
Copy link
Contributor Author

Spone commented Jan 18, 2023

@joshbuker please see #332

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request to be implemented in v1 This issue or pull request will be resolved in the v1 rework, but has not yet been completed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants