Browse Source
* Devise doesn't play nice with Turbo yet * Move errors above header * Add specific field error messages if email or password omitted * Add email validation * Update app/controllers/users/sessions_controller.rb Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com> * Update app/controllers/users/sessions_controller.rb Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com> * Update app/controllers/users/sessions_controller.rb Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com> * Update spec/features/user_spec.rb Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com> * Remove default you need to sign in or sign up message Co-authored-by: Paul Robert Lloyd <paulrobertlloyd@users.noreply.github.com>pull/125/head
Daniel Baark
3 years ago
committed by
GitHub
8 changed files with 89 additions and 11 deletions
@ -0,0 +1,24 @@ |
|||||||
|
class Users::SessionsController < Devise::SessionsController |
||||||
|
def create |
||||||
|
self.resource = resource_class.new |
||||||
|
if params.dig("user", "email").empty? |
||||||
|
resource.errors.add :email, "Enter an email address" |
||||||
|
elsif !email_valid?(params.dig("user", "email")) |
||||||
|
resource.errors.add :email, "Enter an email address in the correct format, like name@example.com" |
||||||
|
end |
||||||
|
if params.dig("user", "password").empty? |
||||||
|
resource.errors.add :password, "Enter a password" |
||||||
|
end |
||||||
|
if resource.errors.present? |
||||||
|
render :new, status: :unprocessable_entity |
||||||
|
else |
||||||
|
super |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
private |
||||||
|
|
||||||
|
def email_valid?(email) |
||||||
|
email =~ URI::MailTo::EMAIL_REGEXP |
||||||
|
end |
||||||
|
end |
@ -0,0 +1,10 @@ |
|||||||
|
module DeviseHelper |
||||||
|
def flash_to_model_errors(resource) |
||||||
|
if flash.alert |
||||||
|
if flash.alert != I18n.t("devise.failure.unauthenticated") |
||||||
|
resource.errors.add :base, flash.alert |
||||||
|
end |
||||||
|
flash.discard |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue