Skip to content

Commit

Permalink
feat(signin): remit reauthenticate instead of signin if user is the same
Browse files Browse the repository at this point in the history
  • Loading branch information
espy committed Jan 17, 2016
1 parent bdc41af commit c69f2e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 8 additions & 1 deletion lib/sign-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = signIn

var Promise = require('lie')
var clone = require('lodash/clone')
var get = require('lodash/get')

var internals = module.exports.internals = {}
internals.deserialise = require('../utils/deserialise')
Expand Down Expand Up @@ -32,6 +33,12 @@ function signIn (state, options) {
}
}

// If the username hasn’t changed, emit 'reauthenticate' instead of 'signin'
var emitEvent = 'signin'
if (get(state, 'account.username') === options.username) {
emitEvent = 'reauthenticate'
}

state.account = {
username: data.account.username,
session: {
Expand All @@ -48,7 +55,7 @@ function signIn (state, options) {
account: state.account
})

state.emitter.emit('signin', clone(state.account))
state.emitter.emit(emitEvent, clone(state.account))

return clone(data.account)
})
Expand Down
8 changes: 2 additions & 6 deletions test/unit/sign-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test('signIn with request error', function (t) {

// account.signIn() to emit `reauthenticate` event when unauthenticated

test.only('signIn with same username', function (t) {
test('signIn with same username', function (t) {
t.plan(2)

var state = {
Expand All @@ -131,11 +131,7 @@ test.only('signIn with same username', function (t) {
})
simple.mock(signIn.internals, 'serialise').returnWith('serialised')
simple.mock(signIn.internals, 'deserialise').returnWith({
id: 'session123',
account: {
id: 'deserialise id',
username: 'deserialise username'
}
id: 'session123'
})
simple.mock(signIn.internals, 'saveAccount').callFn(function () {})

Expand Down

0 comments on commit c69f2e1

Please sign in to comment.