Skip to content

Commit

Permalink
fix: prevent signIn if session present
Browse files Browse the repository at this point in the history
* test: no sign in while signed in

* test: prevent signIn if session present

* test: remove redundant test

* test: more descriptive test name

* test: typo

* test: fix typo
  • Loading branch information
espy authored May 31, 2017
1 parent d3f860e commit a2bd802
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/sign-in.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ function signIn (state, options) {
})

.then(function (cache) {
// If a different user is signed in than the one trying to signIn, throw an error
if (cache.session && cache.username !== options.username) {
return Promise.reject(new Error('You must sign out before signing in'))
}

return state.hook('signin', options, function (options) {
return internals.request({
url: state.url + '/session',
Expand Down
35 changes: 33 additions & 2 deletions test/integration/sign-in-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('sign in', function (t) {
account.signIn(options)

.then(function (signInResult) {
t.pass('signes in')
t.pass('signs in')
t.is(signInResult.username, '[email protected]')

var storeAccount = store.getObject('account')
Expand Down Expand Up @@ -62,7 +62,7 @@ test('sign in', function (t) {
})

.then(function (signOutResult) {
t.pass('signes out')
t.pass('signs out')

t.is(signOutResult.username, '[email protected]')

Expand All @@ -85,3 +85,34 @@ test('sign in', function (t) {

.catch(t.error)
})

test('prevent sign-in when already signed in', function (t) {
store.clear()
t.plan(1)

var account = new Account({
url: baseURL,
id: 'abc4567'
})

nock(baseURL)
.put('/session')
.reply(201, signInResponse)

account.signIn(options)

.then(function (signInResult) {
return account.signIn({
username: '[email protected]',
password: 'secret'
})
})

.then(function () {
t.fail('Sign in must fail when already signed in')
})

.catch(function (error) {
t.is(error.message, 'You must sign out before signing in')
})
})

0 comments on commit a2bd802

Please sign in to comment.