hoodiehq / camp

:circus_tent: Welcome to Hoodie Camp!
https://hoodie.camp
Apache License 2.0
99 stars 55 forks source link

`signin` is not fired when logging in via token #120

Closed gr2m closed 7 years ago

gr2m commented 7 years ago

🐞 The Bug

Hoodie allows to sign in with a one-time token which must have been added to the user account on the server before, like so

account.signIn({
  username: username,
  token: 'MY_SECRET_TOKEN'
})

But as described in https://github.com/hoodiehq/hoodie-account-client/issues/144, the 'signin' event is not triggered, instead 'reauthenticate' is triggered.

To reproduce the bug, create a new Hoodie app as described in our quick start guide. As this is just for debugging purpose, I would recommend to start the app in memory by default, for that you can add

  "hoodie": {
    "inMemory": true
  }

to the created package.json file.

Next, let’s create a test account each time we start the app and add a token to it. For that, create an app-specific plugin. Create a hoodie/server.js file with the following content

module.exports = function (server, options, next) {
  var api = server.plugins.account.api

  api.accounts.add({
    username: 'test',
    password: 'test'
  })

  .then(function (account) {
    console.log('test account created')

    return api.account(account.id).tokens.add({
      id: 'MY_SECRET_TOKEN',
      type: 'login',
      timeout: 7200 // 2h
    })
  })

  .then(function (token) {
    console.log('token added to test account: ' + token.id)
    next()
  })

  .catch(function (error) {
    console.log(error)
    next()
  })
}

module.exports.attributes = {
  name: 'test'
}

Now when you start the app, you should see the following in the terminal

test account created
token added to test account: MY_SECRET_TOKEN

Now open http://localhost:8080 in your browser, open the console in the web developer tools and run this

hoodie.account.on('signin', function () {console.log('signin event')})
hoodie.account.on('reauthenticate', function () {console.log('reauthenticate event')})
hoodie.account.signIn({token: 'MY_SECRET_TOKEN'})

You should now see reauthenticate event being logged. And that is the bug, because it should have logged signin event instead.

Now, git clone the hoodie-account-client repository in a different folder and install its dependencies

cd ~ # change directory to somewhere outside of the hoodie app you created before
git clone git@github.com:hoodiehq/hoodie-account-client.git
cd hoodie-account-client
npm install

Then copy the current path, on Mac/Linux you can do that with the pwd command

Then change again into the directory of your app and run npm link /path/to/hoodie-account/client (where /path/to/hoodie-account/client is the path you copied above). Now when you restart your test app, it will bundle your local hoodie-account-client copy, so you can make changes and see if it fixes the problem.

That’s basically it :slightly_smiling_face:

:clipboard: Step by Step

πŸ€”β“ Questions

Ping us in the Hoodie Chat or on Twitter

Alice-anjali commented 7 years ago

We will start right away :-)

sohini-roy commented 7 years ago

resolved in https://github.com/hoodiehq/hoodie-account-client/pull/157 please review :)