Class: Bible270::SessionsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Bible270::SessionsController
- Defined in:
- app/controllers/bible270/sessions_controller.rb
Overview
Built-in OmniAuth sign-in.
The OmniAuth middleware (registered by the host app) handles the request and callback phases; by the time #create runs, request.env is populated and we just need to turn it into a Reader and remember them.
NOTE: OmniAuth 2.0+ only permits POST to its request-phase routes, so all sign-in controls in these views are forms/buttons rather than links.
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
-
#email_callback ⇒ Object
GET: the reader clicked the link.
-
#email_link ⇒ Object
POST: accept an address and send a one-time link.
- #failure ⇒ Object
- #new ⇒ Object
Instance Method Details
#create ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/controllers/bible270/sessions_controller.rb', line 19 def create auth = request.env["omniauth.auth"] unless auth redirect_to(sign_in_path, alert: "Sign in didn't complete. Please try again.") and return end reader = Reader.from_omniauth(auth) unless reader&.persisted? redirect_to(sign_in_path, alert: "We couldn't set up your reader profile.") and return end destination = safe_origin(request.env["omniauth.origin"]) || after_sign_in_path # Rotate the session id to avoid session fixation, then sign in. reset_session session[:bible270_reader_id] = reader.id @current_reader = reader redirect_to destination, notice: "Welcome, #{reader.display_name}." end |
#destroy ⇒ Object
40 41 42 43 44 |
# File 'app/controllers/bible270/sessions_controller.rb', line 40 def destroy reset_session @current_reader = nil redirect_to(after_sign_out_path, notice: "Signed out.") end |
#email_callback ⇒ Object
GET: the reader clicked the link.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/controllers/bible270/sessions_controller.rb', line 83 def email_callback token = SignInToken.claim!(params[:token]) if token.nil? redirect_to(sign_in_path, alert: "That link has expired or was already used. Please request a new one.") and return end reader = Reader.from_email(token.email, display_name: token.display_name) unless reader&.persisted? redirect_to(sign_in_path, alert: "We couldn't set up your reader profile.") and return end destination = safe_origin(params[:origin]) || after_sign_in_path reset_session session[:bible270_reader_id] = reader.id @current_reader = reader redirect_to destination, notice: "Welcome, #{reader.display_name}." end |
#email_link ⇒ Object
POST: accept an address and send a one-time link.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/controllers/bible270/sessions_controller.rb', line 54 def email_link unless Bible270.config.email_sign_in? redirect_to(sign_in_path, alert: "Email sign-in isn't enabled here.") and return end origin = safe_origin(params[:origin]) address = EmailSignIn.normalize_email(params[:email]) if address.nil? redirect_to(sign_in_path(origin: origin), alert: "That doesn't look like an email address — please check and try again.") and return end name = Bible270.config.email_sign_in_ask_name ? params[:display_name].to_s.strip : nil _record, raw = SignInToken.issue!(address, display_name: name) if raw url = email_sign_in_url(token: raw, origin: origin) minutes = (Bible270.config.email_sign_in_ttl / 60.0).round mail = SignInMailer.magic_link(email: address, url: url, expires_in_minutes: minutes) Bible270.config.email_sign_in_deliver_later ? mail.deliver_later : mail.deliver_now end # Deliberately identical whether or not a link was actually sent, so this # reveals neither who has an account nor that a limit was hit. redirect_to sign_in_path, notice: "Check your inbox — if that address is valid we've sent a sign-in link to #{address}." end |
#failure ⇒ Object
46 47 48 49 |
# File 'app/controllers/bible270/sessions_controller.rb', line 46 def failure = params[:message].presence || "unknown error" redirect_to sign_in_path, alert: "Sign in failed (#{.to_s.tr('_', ' ')})." end |