@@ -2,9 +2,8 @@ export default class Session {
2
2
apiUrl = "https://api.gleap.io" ;
3
3
sdkKey = null ;
4
4
session = {
5
- id : null ,
6
- hash : null ,
7
- type : null ,
5
+ gleapId : null ,
6
+ gleapHash : null ,
8
7
name : "" ,
9
8
email : "" ,
10
9
} ;
@@ -33,18 +32,17 @@ export default class Session {
33
32
} ;
34
33
35
34
injectSession = ( http ) => {
36
- if ( http ) {
35
+ if ( http && this . session ) {
37
36
http . setRequestHeader ( "Api-Token" , this . sdkKey ) ;
38
- http . setRequestHeader ( "Gleap-Id" , this . session . id ) ;
39
- http . setRequestHeader ( "Gleap-Hash" , this . session . hash ) ;
37
+ http . setRequestHeader ( "Gleap-Id" , this . session . gleapId ) ;
38
+ http . setRequestHeader ( "Gleap-Hash" , this . session . gleapHash ) ;
40
39
}
41
40
} ;
42
41
43
42
clearSession = ( renewSession = true ) => {
44
43
try {
45
- localStorage . removeItem ( `bb-session-id` ) ;
46
- localStorage . removeItem ( `bb-session-hash` ) ;
47
- localStorage . removeItem ( `bb-session-type` ) ;
44
+ localStorage . removeItem ( `gleap-id` ) ;
45
+ localStorage . removeItem ( `gleap-hash` ) ;
48
46
} catch ( exp ) { }
49
47
50
48
this . session = {
@@ -61,39 +59,22 @@ export default class Session {
61
59
}
62
60
} ;
63
61
64
- startSession = ( userId , userHash , userData ) => {
62
+ startSession = ( ) => {
65
63
const self = this ;
66
64
return new Promise ( ( resolve , reject ) => {
67
65
const http = new XMLHttpRequest ( ) ;
68
- http . open ( "POST" , this . apiUrl + "/sessions" ) ;
66
+ http . open ( "POST" , self . apiUrl + "/sessions" ) ;
69
67
http . setRequestHeader ( "Content-Type" , "application/json;charset=UTF-8" ) ;
70
- http . setRequestHeader ( "Api-Token" , this . sdkKey ) ;
71
-
72
- // Set the guest id & hash
68
+ http . setRequestHeader ( "Api-Token" , self . sdkKey ) ;
73
69
try {
74
- const sessionType = localStorage . getItem ( `bb-session-type` ) ;
75
- const sessionId = localStorage . getItem ( `bb-session-id` ) ;
76
- const sessionHash = localStorage . getItem ( `bb-session-hash` ) ;
77
- if ( sessionType === "GUEST" ) {
78
- if ( sessionId && sessionHash ) {
79
- http . setRequestHeader ( "Guest-Id" , sessionId ) ;
80
- http . setRequestHeader ( "Guest-Hash" , sessionHash ) ;
81
- }
82
- } else {
83
- // Existing session from cache.
84
- if ( sessionId && sessionHash && ! userId && ! userHash ) {
85
- http . setRequestHeader ( "User-Id" , sessionId ) ;
86
- http . setRequestHeader ( "User-Hash" , sessionHash ) ;
87
- }
70
+ const gleapId = localStorage . getItem ( `gleap-id` ) ;
71
+ const gleapHash = localStorage . getItem ( `gleap-hash` ) ;
72
+ if ( gleapId && gleapHash ) {
73
+ http . setRequestHeader ( "Gleap-Id" , gleapId ) ;
74
+ http . setRequestHeader ( "Gleap-Hash" , gleapHash ) ;
88
75
}
89
76
} catch ( exp ) { }
90
77
91
- // Additionally set the user id
92
- if ( userId && userHash ) {
93
- http . setRequestHeader ( "User-Id" , userId ) ;
94
- http . setRequestHeader ( "User-Hash" , userHash ) ;
95
- }
96
-
97
78
http . onerror = ( error ) => {
98
79
self . clearSession ( false ) ;
99
80
reject ( ) ;
@@ -105,9 +86,8 @@ export default class Session {
105
86
const sessionData = JSON . parse ( http . responseText ) ;
106
87
107
88
try {
108
- localStorage . setItem ( `bb-session-id` , sessionData . id ) ;
109
- localStorage . setItem ( `bb-session-hash` , sessionData . hash ) ;
110
- localStorage . setItem ( `bb-session-type` , sessionData . type ) ;
89
+ localStorage . setItem ( `gleap-id` , sessionData . gleapId ) ;
90
+ localStorage . setItem ( `gleap-hash` , sessionData . gleapHash ) ;
111
91
} catch ( exp ) { }
112
92
113
93
self . session = sessionData ;
@@ -131,7 +111,62 @@ export default class Session {
131
111
}
132
112
}
133
113
} ;
134
- http . send ( JSON . stringify ( userData ) ) ;
114
+ http . send ( JSON . stringify ( { } ) ) ;
115
+ } ) ;
116
+ } ;
117
+
118
+ identifySession = ( userId , userData ) => {
119
+ const self = this ;
120
+ return new Promise ( ( resolve , reject ) => {
121
+ // Wait for gleap session to be ready.
122
+ this . setOnSessionReady ( function ( ) {
123
+ const http = new XMLHttpRequest ( ) ;
124
+ http . open ( "POST" , self . apiUrl + "/sessions/identify" ) ;
125
+ http . setRequestHeader ( "Content-Type" , "application/json;charset=UTF-8" ) ;
126
+ http . setRequestHeader ( "Api-Token" , self . sdkKey ) ;
127
+ try {
128
+ const gleapId = localStorage . getItem ( `gleap-id` ) ;
129
+ const gleapHash = localStorage . getItem ( `gleap-hash` ) ;
130
+ if ( gleapId && gleapHash ) {
131
+ http . setRequestHeader ( "Gleap-Id" , gleapId ) ;
132
+ http . setRequestHeader ( "Gleap-Hash" , gleapHash ) ;
133
+ }
134
+ } catch ( exp ) { }
135
+
136
+ http . onerror = ( ) => {
137
+ reject ( ) ;
138
+ } ;
139
+ http . onreadystatechange = function ( e ) {
140
+ if ( http . readyState === XMLHttpRequest . DONE ) {
141
+ if ( http . status === 200 || http . status === 201 ) {
142
+ try {
143
+ const sessionData = JSON . parse ( http . responseText ) ;
144
+
145
+ try {
146
+ localStorage . setItem ( `gleap-id` , sessionData . gleapId ) ;
147
+ localStorage . setItem ( `gleap-hash` , sessionData . gleapHash ) ;
148
+ } catch ( exp ) { }
149
+
150
+ self . session = sessionData ;
151
+ self . ready = true ;
152
+
153
+ resolve ( sessionData ) ;
154
+ } catch ( exp ) {
155
+ reject ( exp ) ;
156
+ }
157
+ } else {
158
+ self . clearSession ( false ) ;
159
+ reject ( ) ;
160
+ }
161
+ }
162
+ } ;
163
+ http . send (
164
+ JSON . stringify ( {
165
+ ...userData ,
166
+ userId,
167
+ } )
168
+ ) ;
169
+ } ) ;
135
170
} ) ;
136
171
} ;
137
172
}
0 commit comments