This is what I call when my site is loaded:
(function($){})(window.jQuery);
$(document).ready(function () {
window.addEventListener('cloudkitloaded', function() {
CloudKit.configure({
containers: [{
containerIdentifier: 'iCloud.pl.blueworld.fieldservice',
apiToken: 'fc363369412ad6c99020e7e0ed3e2be121008e34534cff7debe1f4f7f829d152',
environment: 'production'
}]
});
var container = CloudKit.getDefaultContainer();
var privateDB = container.privateCloudDatabase;
this.gotoAuthenticatedState = function(userInfo) {
if(userInfo.isDiscoverable) {
//success
} else {
//failure
}
container
.whenUserSignsOut()
.then(this.gotoUnauthenticatedState);
};
this.gotoUnauthenticatedState = function(error) {
//do something of sign out
container
.whenUserSignsIn()
.then(this.gotoAuthenticatedState)
.catch(this.gotoUnauthenticatedState);
};
container.setUpAuth().then(function(userInfo) {
if(userInfo) {
this.gotoAuthenticatedState(userInfo);
} else {
this.gotoUnauthenticatedState();
}
});
});
});
And when I login with success, there correct button appears:
But every time when I refresh the page this button changes:
What to do to keep session and possibility to fetch records until user manually will log out?
How to check if there exists current session (user already authenticated itself) and use it?