Skip to content

Feature request: login! method #327

@Spone

Description

@Spone

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestto be implemented in v1This issue or pull request will be resolved in the v1 rework, but has not yet been completed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions