@@ -6,27 +6,30 @@ function init (hoodie) {
6
6
// on signin. So before the signin happens, we store the user account’s id
7
7
// and data and store it again after the signin
8
8
hoodie . account . hook . before ( 'signin' , function ( options ) {
9
- options . beforeSignin = {
10
- accountId : hoodie . account . id
11
- }
12
- return hoodie . store . findAll ( ) . then ( function ( docs ) {
13
- options . beforeSignin . docs = docs
9
+ return Promise . all ( [
10
+ hoodie . account . get ( 'id' ) ,
11
+ hoodie . store . findAll ( )
12
+ ] ) . then ( function ( results ) {
13
+ options . beforeSignin = {
14
+ accountId : results [ 0 ] ,
15
+ docs : results [ 1 ]
16
+ }
14
17
} )
15
18
} )
16
19
17
- hoodie . account . hook . after ( 'signin' , function ( session , options ) {
20
+ hoodie . account . hook . after ( 'signin' , function ( account , options ) {
18
21
// when signing in to a newly created account, the account.id does not
19
22
// change. The same is true when the user changed their username. In both
20
23
// cases there is no need to migrate local data
21
- if ( options . beforeSignin . accountId === hoodie . account . id ) {
24
+ if ( options . beforeSignin . accountId === account . id ) {
22
25
return hoodie . store . connect ( )
23
26
}
24
27
25
- return hoodie . store . reset ( { name : 'user/' + hoodie . account . id } )
28
+ return hoodie . store . reset ( )
26
29
27
30
. then ( function ( ) {
28
31
function migrate ( doc ) {
29
- doc . createdBy = hoodie . account . id
32
+ doc . createdBy = account . id
30
33
delete doc . _rev
31
34
return doc
32
35
}
@@ -50,28 +53,50 @@ function init (hoodie) {
50
53
} )
51
54
} )
52
55
hoodie . account . hook . after ( 'signout' , function ( options ) {
53
- return hoodie . store . reset ( { name : 'user/' + hoodie . account . id } )
56
+ return hoodie . store . reset ( )
54
57
} )
55
58
56
59
hoodie . account . on ( 'unauthenticate' , hoodie . store . disconnect )
57
60
hoodie . account . on ( 'reauthenticate' , hoodie . store . connect )
58
61
59
62
// handle connection status changes
60
63
hoodie . connectionStatus . on ( 'disconnect' , function ( ) {
61
- if ( ! hoodie . account . isSignedIn ( ) ) {
62
- return
63
- }
64
- hoodie . store . disconnect ( )
64
+ hoodie . account . get ( 'session' )
65
+
66
+ . then ( function ( session ) {
67
+ if ( session ) {
68
+ hoodie . store . disconnect ( )
69
+ }
70
+ } )
65
71
} )
66
72
hoodie . connectionStatus . on ( 'connect' , function ( ) {
67
- if ( ! hoodie . account . isSignedIn ( ) ) {
73
+ hoodie . account . get ( 'session' )
74
+
75
+ . then ( function ( session ) {
76
+ if ( session ) {
77
+ hoodie . store . connect ( )
78
+ }
79
+ } )
80
+ } )
81
+
82
+ hoodie . account . get ( 'session' )
83
+
84
+ . then ( function ( session ) {
85
+ // signed out
86
+ if ( ! session ) {
87
+ return
88
+ }
89
+
90
+ // signed in, but session was invalid
91
+ if ( session . invalid ) {
92
+ return
93
+ }
94
+
95
+ // hoodie.connectionStatus.ok is false if there is a connection issue
96
+ if ( hoodie . connectionStatus . ok === false ) {
68
97
return
69
98
}
70
- hoodie . store . connect ( )
71
- } )
72
99
73
- // hoodie.connectionStatus.ok is false if there is a connection issue
74
- if ( hoodie . account . isSignedIn ( ) && hoodie . connectionStatus . ok !== false ) {
75
100
hoodie . store . connect ( )
76
- }
101
+ } )
77
102
}
0 commit comments