Skip to content

Commit

Permalink
refactor: use account.hook API
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Dec 23, 2016
1 parent 67dbb2b commit 7ba7596
Showing 1 changed file with 36 additions and 54 deletions.
90 changes: 36 additions & 54 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,72 +3,54 @@ module.exports = init
function init (hoodie) {
// In order to prevent data loss, we want to move all data that has been
// created without an account (e.g. while offline) to the user’s account
// on signin. So before the signin happens, we temporarily store it in
// a variable (dataFromAccountBeforeSignin) and add it to the store again
// in the post:signin hook below
var dataFromAccountBeforeSignin
var accountIdBeforeSignIn
hoodie.account.on('pre:signin', function (options) {
options.hooks.push(function () {
accountIdBeforeSignIn = hoodie.account.id
return hoodie.store.findAll().then(function (objects) {
dataFromAccountBeforeSignin = objects
})
// on signin. So before the signin happens, we store the user account’s id
// and data and store it again after the signin
hoodie.account.hook.before('signin', function (options) {
options.beforeSignin = {
accountId: hoodie.account.id
}
return hoodie.store.findAll().then(function (docs) {
options.beforeSignin.docs = docs
})
})

hoodie.account.on('post:signin', function (options) {
options.hooks.push(function () {
// when signing in to a newly created account, the account.id
// does not change, so there is no need to clear the local
// store and to migrate data
if (accountIdBeforeSignIn === hoodie.account.id) {
dataFromAccountBeforeSignin = null
return hoodie.store.connect()
}

return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
hoodie.account.hook.after('signin', function (session, options) {
// when signing in to a newly created account, the account.id does not
// change. The same is true when the user changed their username. In both
// cases there is no need to migrate local data
if (options.beforeSignin.accountId === hoodie.account.id) {
return hoodie.store.connect()
}

.catch(function (error) {
dataFromAccountBeforeSignin = null
throw error
})
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })

.then(function () {
var migratedDataFromAccountBeforeSignIn = dataFromAccountBeforeSignin.map(function (object) {
object.createdBy = hoodie.account.id
delete object._rev
return object
})
dataFromAccountBeforeSignin = null
return hoodie.store.add(migratedDataFromAccountBeforeSignIn)
})
.then(function () {
function migrate (doc) {
doc.createdBy = hoodie.account.id
delete doc._rev
return doc
}
return hoodie.store.add(options.beforeSignin.docs.map(migrate))
})

.then(function () {
return hoodie.store.connect()
})
.then(function () {
return hoodie.store.connect()
})
})

// see https://github.com/hoodiehq/hoodie-client-account/issues/65
// for info on the internal pre:* & post:* events
hoodie.account.on('pre:signout', function (options) {
options.hooks.push(function () {
return hoodie.store.push()
.catch(function (error) {
if (error.status !== 401) {
throw error
}

error.message = 'Local changes could not be synced, sign in first'
hoodie.account.hook.before('signout', function () {
return hoodie.store.push()
.catch(function (error) {
if (error.status !== 401) {
throw error
})
}

error.message = 'Local changes could not be synced, sign in first'
throw error
})
})
hoodie.account.on('post:signout', function (options) {
options.hooks.push(function () {
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
})
hoodie.account.hook.after('signout', function (options) {
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
})

hoodie.account.on('unauthenticate', hoodie.store.disconnect)
Expand Down

0 comments on commit 7ba7596

Please sign in to comment.