Class: Kadmin::AuthController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- Kadmin::AuthController
- Defined in:
- app/controllers/kadmin/auth_controller.rb
Constant Summary collapse
- SESSION_KEY =
'kadmin.user'.freeze
Endpoints collapse
-
#failure ⇒ Object
GET /auth/failure.
-
#login ⇒ Object
GET /auth/login.
-
#logout ⇒ Object
GET /auth/logout DELETE /auth/logout.
-
#save ⇒ Object
GET /auth/:provider/callback POST /auth/:provider/callback.
- #unauthorized ⇒ Object
Helpers collapse
Instance Method Details
#failure ⇒ Object
GET /auth/failure
47 48 49 50 |
# File 'app/controllers/kadmin/auth_controller.rb', line 47 def failure flash.alert = params[:message] redirect_to action: :login end |
#login ⇒ Object
GET /auth/login
10 11 12 |
# File 'app/controllers/kadmin/auth_controller.rb', line 10 def login render 'kadmin/auth/login' end |
#logout ⇒ Object
GET /auth/logout DELETE /auth/logout
16 17 18 19 |
# File 'app/controllers/kadmin/auth_controller.rb', line 16 def logout session.delete(SESSION_KEY) redirect_to action: :login end |
#omniauth_provider_link ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/controllers/kadmin/auth_controller.rb', line 75 def omniauth_provider_link auth_prefix = "#{Kadmin.config.mount_path}/auth" provider_link = "#{auth_prefix}/#{Kadmin::Auth.omniauth_provider}" origin = params[:origin] # if the referer is a auth route, then we risk ending in an endless loop if origin.blank? referer = request.referer if referer.blank? origin = Kadmin.config.mount_path else uri = URI(referer) origin = referer unless uri&.path&.start_with?(auth_prefix) end end provider_link = "#{provider_link}?origin=#{CGI.escape(origin)}" unless origin.blank? return provider_link end |
#save ⇒ Object
GET /auth/:provider/callback POST /auth/:provider/callback
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/controllers/kadmin/auth_controller.rb', line 23 def save auth_hash = request.env['omniauth.auth'] if auth_hash.blank? Kadmin.logger.error('No authorization hash provided') flash.alert = I18n.t('kadmin.auth.error') redirect_to action: :login return end email = auth_hash.dig('info', 'email') if Kadmin::Auth.users.exists?(email) session[SESSION_KEY] = email redirect_url = request.env['omniauth.origin'] redirect_url = Kadmin.config.mount_path unless valid_redirect_url?(redirect_url) else flash.alert = I18n.t('kadmin.auth.unauthorized_message') redirect_url = url_for(action: :login) end redirect_to redirect_url end |
#unauthorized ⇒ Object
52 53 54 55 56 57 |
# File 'app/controllers/kadmin/auth_controller.rb', line 52 def render 'kadmin/error', format: ['html'], locals: { title: I18n.t('kadmin.auth.unauthorized'), message: I18n.t('kadmin.auth.unauthorized_message') } end |