Class: Passwordless::SessionsController

Inherits:
ApplicationController show all
Includes:
ControllerHelpers
Defined in:
app/controllers/passwordless/sessions_controller.rb

Instance Method Summary collapse

Methods included from ControllerHelpers

#authenticate_by_cookie, #reset_passwordless_redirect_location!, #save_passwordless_redirect_location!, #sign_in, #sign_out

Methods inherited from ApplicationController

#passwordless_controller?

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/passwordless/sessions_controller.rb', line 15

def create
  email_field = authenticatable_class.passwordless_email_field
  email = params.require(:passwordless).fetch(email_field).downcase
  authenticatable =
    authenticatable_class.where("lower(#{email_field}) = ?", email).first

  session = Session.new.tap do |us|
    us.remote_addr = request.remote_addr
    us.user_agent = request.env['HTTP_USER_AGENT']
    us.authenticatable = authenticatable
  end

  if session.save
    Mailer.magic_link(session).deliver_now
  end

  render
end

#destroyObject



55
56
57
58
# File 'app/controllers/passwordless/sessions_controller.rb', line 55

def destroy
  sign_out authenticatable_class
  redirect_to main_app.root_path
end

#newObject



9
10
11
12
13
# File 'app/controllers/passwordless/sessions_controller.rb', line 9

def new
  @email_field = authenticatable_class.passwordless_email_field

  @session = Session.new
end

#showObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/passwordless/sessions_controller.rb', line 34

def show
  # Make it "slow" on purpose to make brute-force attacks more of a hassle
  BCrypt::Password.create(params[:token])

  session = Session.valid.find_by!(
    authenticatable_type: authenticatable_classname,
    token: params[:token]
  )

   session.authenticatable

  enabled = Passwordless.
  destination = dest = reset_passwordless_redirect_location!(User)

  if enabled && destination
    redirect_to dest
  else
    redirect_to main_app.root_path
  end
end