Class: Bible270::SessionsController

Inherits:
ApplicationController show all
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

Instance Method Details

#createObject



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(, alert: "Sign in didn't complete. Please try again.") and return
  end

  reader = Reader.from_omniauth(auth)
  unless reader&.persisted?
    redirect_to(, alert: "We couldn't set up your reader profile.") and return
  end

  destination = safe_origin(request.env["omniauth.origin"]) || 

  # 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

#destroyObject



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_callbackObject

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(,
                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(, alert: "We couldn't set up your reader profile.") and return
  end

  destination = safe_origin(params[:origin]) || 

  reset_session
  session[:bible270_reader_id] = reader.id
  @current_reader = reader

  redirect_to destination, notice: "Welcome, #{reader.display_name}."
end

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(, 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((origin: origin),
                alert: "That doesn't look like an email address — please check and try again.") and return
  end

  name = Bible270.config. ? params[:display_name].to_s.strip : nil
  _record, raw = SignInToken.issue!(address, display_name: name)

  if raw
    url = (token: raw, origin: origin)
    minutes = (Bible270.config. / 60.0).round
    mail = SignInMailer.magic_link(email: address, url: url, expires_in_minutes: minutes)
    Bible270.config. ? 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 ,
              notice: "Check your inbox — if that address is valid we've sent a sign-in link to #{address}."
end

#failureObject



46
47
48
49
# File 'app/controllers/bible270/sessions_controller.rb', line 46

def failure
  message = params[:message].presence || "unknown error"
  redirect_to , alert: "Sign in failed (#{message.to_s.tr('_', ' ')})."
end

#newObject



12
13
14
15
16
17
# File 'app/controllers/bible270/sessions_controller.rb', line 12

def new
  redirect_to(, notice: "You're already signed in.") and return if signed_in?

  @providers = Bible270.config.omniauth_providers
  @origin = safe_origin(params[:origin])
end