Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use account.hook API #112

Merged
merged 2 commits into from
Dec 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading