send a notification (through ActiveSupport::Instrument) when someone has signed up, signed in or signed out

did committed Jul 07, 2017
commit 361e7b45a7b5e26d0ab9fb7594651f33defb4605
Showing 3 changed files with 25 additions and 11 deletions
locomotive/steam/middlewares/auth.rb b/lib/locomotive/steam/middlewares/auth.rb +3 -1
@@ @@ -45,7 +45,7 @@ module Locomotive::Steam
def sign_in(options)
return if authenticated?
- status, entry = services.auth.sign_in(options)
+ status, entry = services.auth.sign_in(options, request)
if status == :signed_in
store_authenticated(entry)
@@ @@ -58,6 +58,8 @@ module Locomotive::Steam
def sign_out(options)
return unless authenticated?
+ services.auth.sign_out(load_authenticated_entry, request)
+
store_authenticated(nil)
append_message(:signed_out)
locomotive/steam/services/auth_service.rb b/lib/locomotive/steam/services/auth_service.rb +21 -9
@@ @@ -19,13 +19,7 @@ module Locomotive
end
if entry.errors.empty?
- ActiveSupport::Notifications.instrument('steam.auth.signup',
- site: site,
- entry: entry,
- locale: entries.locale,
- request: request
- )
-
+ notify(:signed_up, entry, request)
context[options.type.singularize] = entry
send_welcome_email(options, context)
end
@@ @@ -33,7 +27,7 @@ module Locomotive
[entry.errors.empty? ? :entry_created : :invalid_entry, entry]
end
- def sign_in(options)
+ def sign_in(options, request)
entry = entries.all(options.type, options.id_field => options.id).first
if entry
@@ @@ -41,12 +35,21 @@ module Locomotive
password = ::BCrypt::Engine.hash_secret(options.password, entry.send(options.password_field).try(:salt))
same_password = secure_compare(password, hashed_password)
- return [:signed_in, entry] if same_password
+ if same_password
+ notify(:signed_in, entry, request)
+ return [:signed_in, entry]
+ end
end
:wrong_credentials
end
+ def sign_out(entry, request)
+ notify(:signed_out, entry, request)
+
+ :signed_out
+ end
+
# options is an instance of the AuthOptions class
def forgot_password(options, context)
entry = entries.all(options.type, options.id_field => options.id).first
@@ @@ -134,6 +137,15 @@ EMAIL
res == 0
end
+ def notify(action, entry, request)
+ ActiveSupport::Notifications.instrument("steam.auth.#{action}",
+ site: site,
+ entry: entry,
+ locale: entries.locale,
+ request: request
+ )
+ end
+
# Module inject to the content entry to enable
# related authentication methods.
#
spec/unit/services/auth_service_spec.rb +1 -1
@@ @@ -147,7 +147,7 @@ describe Locomotive::Steam::AuthService do
describe '#sign_in' do
- subject { service.sign_in(auth_options) }
+ subject { service.sign_in(auth_options, nil) }
it 'returns :wrong_credentials if no entry matches the email' do
expect(entries).to receive(:all).with('accounts', { 'email' => 'john@doe.net' }).and_return([])