Class: Lato::AuthenticationController

Inherits:
ApplicationController show all
Defined in:
app/controllers/lato/authentication_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#error, #index, #not_found, #offline, #switch_locale

Methods included from Componentable

#lato_index_collection

Methods included from Layoutable

#active_navbar, #active_sidebar, #hide_sidebar, #page_class, #page_classes, #page_title, #show_sidebar

Methods included from Sessionable

#authenticate_session, #limit_requests, #not_authenticate_session, #session_create, #session_destroy

Instance Method Details

#accept_invitationObject

Accept invitation



180
181
182
# File 'app/controllers/lato/authentication_controller.rb', line 180

def accept_invitation
  @user = Lato::User.new(email: @invitation.email)
end

#accept_invitation_actionObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/lato/authentication_controller.rb', line 184

def accept_invitation_action
  @user = Lato::User.new(registration_params)

  respond_to do |format|
    if @user.accept_invitation(params.permit(:id, :accepted_code))
      session_create(@user.id)

      format.html { redirect_to lato.root_path }
      format.json { render json: @user }
    else
      format.html { render :accept_invitation, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#authentication_methodObject

Authentication method choice



203
# File 'app/controllers/lato/authentication_controller.rb', line 203

def authentication_method; end

#authentication_method_actionObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/lato/authentication_controller.rb', line 205

def authentication_method_action
  method = params[:method]
  
  respond_to do |format|
    case method
    when 'authenticator'
      session[:authentication_method] = 'authenticator'
      format.html { redirect_to lato.authentication_authenticator_path }
      format.json { render json: { redirect: lato.authentication_authenticator_path } }
    when 'webauthn'
      session[:authentication_method] = 'webauthn'
      format.html { redirect_to lato.authentication_webauthn_path }
      format.json { render json: { redirect: lato.authentication_webauthn_path } }
    else
      format.html { redirect_to lato. }
      format.json { render json: { error: 'Invalid method' }, status: :unprocessable_entity }
    end
  end
end

#authenticatorObject

Authenticator



228
229
230
231
# File 'app/controllers/lato/authentication_controller.rb', line 228

def authenticator
  @user = Lato::User.find_by_id(session[:authentication_user_id])
  return respond_to_with_not_found unless @user
end

#authenticator_actionObject



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/controllers/lato/authentication_controller.rb', line 233

def authenticator_action
  @user = Lato::User.find_by_id(session[:authentication_user_id])

  respond_to do |format|
    if @user.authenticator(params.require(:user).permit(:authenticator_code))
      clear_authentication_session
      session_create(@user.id)

      format.html { redirect_to lato.root_path }
      format.json { render json: @user }
    else
      format.html { render :authenticator, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#recover_passwordObject

Recover password



142
143
144
# File 'app/controllers/lato/authentication_controller.rb', line 142

def recover_password
  @user = Lato::User.new
end

#recover_password_actionObject



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/controllers/lato/authentication_controller.rb', line 146

def recover_password_action
  @user = Lato::User.new

  respond_to do |format|
    if @user.request_recover_password(params.require(:user).permit(:email))
      format.html { redirect_to lato.authentication_update_password_path(id: @user.id) }
      format.json { render json: @user }
    else
      format.html { render :recover_password, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#signinObject

Signin



22
23
24
# File 'app/controllers/lato/authentication_controller.rb', line 22

def 
  @user = Lato::User.new
end

#signin_actionObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/lato/authentication_controller.rb', line 26

def 
  @user = Lato::User.new

  respond_to do |format|
    if @user.(params.require(:user).permit(:email, :password).merge(
      ip_address: request.remote_ip,
      user_agent: request.user_agent
    ))
      redirect_path = determine_authentication_redirect(@user)
      if redirect_path
        format.html { redirect_to redirect_path }
        format.json { render json: @user }
      else
        session_create(@user.id)
        format.html { redirect_to lato.root_path }
        format.json { render json: @user }
      end
    else
      format.html { render :signin, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#signoutObject

Signout



109
# File 'app/controllers/lato/authentication_controller.rb', line 109

def signout; end

#signout_actionObject



111
112
113
114
115
116
117
118
# File 'app/controllers/lato/authentication_controller.rb', line 111

def signout_action
  session_destroy

  respond_to do |format|
    format.html { redirect_to lato.root_path }
    format.json { render json: {} }
  end
end

#signupObject

Signup



85
86
87
# File 'app/controllers/lato/authentication_controller.rb', line 85

def 
  @user = Lato::User.new
end

#signup_actionObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/lato/authentication_controller.rb', line 89

def 
  @user = Lato::User.new(registration_params)
  return unless verify_hcaptcha(:signup)

  respond_to do |format|
    if @user.(ip_address: request.remote_ip, user_agent: request.user_agent)
      session_create(@user.id)

      format.html { redirect_to lato.root_path }
      format.json { render json: @user }
    else
      format.html { render :signup, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#update_passwordObject

Update password



163
# File 'app/controllers/lato/authentication_controller.rb', line 163

def update_password; end

#update_password_actionObject



165
166
167
168
169
170
171
172
173
174
175
# File 'app/controllers/lato/authentication_controller.rb', line 165

def update_password_action
  respond_to do |format|
    if @user.update_password(params.require(:user).permit(:code, :password, :password_confirmation))
      format.html { redirect_to lato., notice: I18n.t('lato.authentication_controller.update_password_action_notice') }
      format.json { render json: @user }
    else
      format.html { render :update_password, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#verify_emailObject

Verify email



123
124
125
# File 'app/controllers/lato/authentication_controller.rb', line 123

def verify_email
  @code = params[:code]
end

#verify_email_actionObject



127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/lato/authentication_controller.rb', line 127

def verify_email_action
  respond_to do |format|
    if @user.verify_email(params.require(:user).permit(:code))
      format.html { redirect_to lato.root_path, notice: I18n.t('lato.authentication_controller.verify_email_action_notice') }
      format.json { render json: @user }
    else
      format.html { render :verify_email, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#web3_signinObject



50
51
52
53
# File 'app/controllers/lato/authentication_controller.rb', line 50

def 
  @user = Lato::User.new
  session[:web3_nonce] = SecureRandom.hex(32)
end

#web3_signin_actionObject



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/lato/authentication_controller.rb', line 55

def 
  @user = Lato::User.new

  respond_to do |format|
    if @user.(params.require(:user).permit(:web3_address, :web3_signed_nonce).merge(
      ip_address: request.remote_ip,
      user_agent: request.user_agent,
      web3_nonce: session[:web3_nonce]
    ))
      session[:web3_nonce] = nil
      redirect_path = determine_authentication_redirect(@user)
      if redirect_path
        format.html { redirect_to redirect_path }
        format.json { render json: @user }
      else
        session_create(@user.id)
        format.html { redirect_to lato.root_path }
        format.json { render json: @user }
      end
    else
      session[:web3_nonce] = nil
      format.html { render :web3_signin, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

#webauthnObject

WebAuthn



253
254
255
256
# File 'app/controllers/lato/authentication_controller.rb', line 253

def webauthn
  @options = @user.webauthn_authentication_options
  session[:webauthn_challenge] = @options.challenge
end

#webauthn_actionObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'app/controllers/lato/authentication_controller.rb', line 258

def webauthn_action
  respond_to do |format|
    if @user.webauthn_authentication(params.require(:user).permit(:webauthn_credential), session[:webauthn_challenge])
      clear_authentication_session
      session_create(@user.id)

      format.html { redirect_to lato.root_path }
      format.json { render json: @user }
    else
      @options = @user.webauthn_authentication_options
      session[:webauthn_challenge] = @options.challenge
      format.html { render :webauthn, status: :unprocessable_entity }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end