11import { GleapFrameManager , GleapNotificationManager } from "./Gleap" ;
2- import { loadFromGleapCache , saveToGleapCache } from "./GleapHelper" ;
2+ import { eraseGleapCookie , getGleapCookie , loadFromGleapCache , saveToGleapCache , setGleapCookie } from "./GleapHelper" ;
33
44export default class GleapSession {
55 apiUrl = "https://api.gleap.io" ;
66 sdkKey = null ;
77 updatingSession = false ;
8+ useCookies = true ;
89 session = {
910 gleapId : null ,
1011 gleapHash : null ,
@@ -94,6 +95,12 @@ export default class GleapSession {
9495 saveToGleapCache ( `session-${ this . sdkKey } ` , null ) ;
9596 } catch ( exp ) { }
9697
98+ if ( this . useCookies ) {
99+ try {
100+ eraseGleapCookie ( `session-${ this . sdkKey } ` ) ;
101+ } catch ( exp ) { }
102+ }
103+
97104 this . ready = false ;
98105 this . session = {
99106 id : null ,
@@ -128,6 +135,9 @@ export default class GleapSession {
128135 }
129136
130137 saveToGleapCache ( `session-${ this . sdkKey } ` , session ) ;
138+ if ( this . useCookies ) {
139+ setGleapCookie ( `session-${ this . sdkKey } ` , encodeURIComponent ( JSON . stringify ( session ) ) , 365 ) ;
140+ }
131141
132142 this . session = session ;
133143 this . ready = true ;
@@ -136,10 +146,24 @@ export default class GleapSession {
136146 } ;
137147
138148 startSession = ( attemp = 0 ) => {
139- // Check if session is already ready.
140- const cachedSession = loadFromGleapCache ( `session-${ this . sdkKey } ` ) ;
141- if ( cachedSession ) {
142- this . validateSession ( cachedSession ) ;
149+ // Check if we already have a session cookie.
150+ try {
151+ if ( this . useCookies ) {
152+ const sessionCookie = getGleapCookie ( `session-${ this . sdkKey } ` ) ;
153+ if ( sessionCookie ) {
154+ const sessionData = JSON . parse ( decodeURIComponent ( sessionCookie ) ) ;
155+
156+ this . validateSession ( sessionData ) ;
157+ }
158+ }
159+ } catch ( exp ) { }
160+
161+ if ( ! ( this . session && this . session . gleapId && this . session . gleapId . length > 0 ) ) {
162+ // Check if session is cached.
163+ const cachedSession = loadFromGleapCache ( `session-${ this . sdkKey } ` ) ;
164+ if ( cachedSession ) {
165+ this . validateSession ( cachedSession ) ;
166+ }
143167 }
144168
145169 const self = this ;
0 commit comments