-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathwith_email_auth.rb
36 lines (28 loc) · 1 KB
/
with_email_auth.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true
# Based on https://github.com/weg-li/weg-li/blob/master/app/controllers/sessions_controller.rb
# Original author: https://github.com/phoet
module WithEmailAuth
def email; end
def email_login
email = normalize_email(params[:email])
if email.present? && valid_looking_email?(email)
token = EmailAuthToken.generate(email)
from = Whitelabel[:email]
label_name = t("label.#{Whitelabel[:label_id]}.name")
label_link = Whitelabel[:canonical_url]
UserMailer.login_link(email, token, from, I18n.locale,
label_name, label_link).deliver_later
redirect_to root_path, notice: t('email_auth.email_sent', email:)
else
flash.now[:alert] = t('email_auth.invalid_email')
render :email, status: :unprocessable_entity
end
end
protected
def normalize_email(email)
email.to_s.strip.downcase
end
def valid_looking_email?(email)
email.match(/\A[\w+\-.]+@[a-z\d-]+(\.[a-z\d-]+)*\.[a-z]+\z/i)
end
end