Class: RailsBase::Users::SessionsController

Inherits:
Devise::SessionsController
  • Object
show all
Defined in:
app/controllers/rails_base/users/sessions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /user/sign_in



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/rails_base/users/sessions_controller.rb', line 16

def create
  # Warden/Devise will try to sign the user in before we explicitly do
  # Sign out the user when this happens so we can sign them back in later
  sign_out(current_user) if current_user

  authenticate = RailsBase::Authentication::AuthenticateUser.call(email: params[:user][:email], password: params[:user][:password])

  if authenticate.failure?
    @user = User.new(email: params[:user][:email])
    flash[:alert] = authenticate.message
    render template: 'rails_base/devise/sessions/new'
    return
  end

  mfa_decision = RailsBase::Authentication::DecisionTwofaType.call(user: authenticate.user)
  if mfa_decision.failure?
    redirect_to RailsBase.url_routes.new_user_session_path, email: params[:user][:email], alert: mfa_decision.message
    return
  end

  if mfa_decision.set_mfa_randomized_token
    session[:mfa_randomized_token] =
      RailsBase::Mfa::EncryptToken.call(
        user: authenticate.user,
        expires_at: mfa_decision.token_ttl,
        purpose: mfa_decision.mfa_purpose,
      ).encrypted_val
  end

  if mfa_decision.
    (authenticate.user)
    if mfa_decision.add_mfa_button
      RailsBase::RequestLink.add(link: RailsBase.url_routes.(openmfa: true), text: "Enable MFA")
    end

    # only referentially redirect when we know the user should sign in
    redirect_to(redirect_from_reference || RailsBase.url_routes.authenticated_root_path, mfa_decision.flash)
    return
  end

  ####
  # User needs MFA
  ####
  add_mfa_event_to_session(event: RailsBase::MfaEvent.(user: authenticate.user))
  redirect_to(mfa_decision.redirect_url, mfa_decision.flash)
end

#destroyObject

DELETE /user/sign_out



64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/rails_base/users/sessions_controller.rb', line 64

def destroy
  session[:mfa_randomized_token] = nil

  # force the user to sign out
  sign_out(current_user)
  reset_session

  admin_reset_session!

  flash[:notice] = 'You have been succesfully signed out'
  redirect_to RailsBase.url_routes.unauthenticated_root_path
end

#hearbeat_with_authObject

POST /heartbeat



84
85
86
# File 'app/controllers/rails_base/users/sessions_controller.rb', line 84

def hearbeat_with_auth
  heartbeat
end

#hearbeat_without_authObject

GET /heartbeat



78
79
80
81
# File 'app/controllers/rails_base/users/sessions_controller.rb', line 78

def hearbeat_without_auth
  skip_capture_reference!
  heartbeat
end

#newObject

GET /user/sign_in



10
11
12
13
# File 'app/controllers/rails_base/users/sessions_controller.rb', line 10

def new
  @user = User.new
  render template: 'rails_base/devise/sessions/new'
end