This is a migrated thread and some comments may be shown as answers.

backend logout issues

4 Answers 189 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
AlexFStop
Top achievements
Rank 1
AlexFStop asked on 15 May 2014, 05:33 AM
I am not having any success logging out of my Backend (Everlive) DB. Here is the code (in which the successful login is immediately followed by a failed logout):

var el = new Everlive({apiKey: '<API key>'}); 
Everlive.$.Users.login('<Backend>', // username
      '<password>') // password
    .then(function (data) { },
    function(error) {alert('Unable to connect to network/database - please try again later');});
el.Users.logout().then
    (function() {alert('Successful logout');}, // success
     function() {alert('Failed logout');}); // error

I always get to the 'failed logout' alert. I've checked all the opening and closing () and {} and they seem to be OK. Obviously 
Can anyone help? 
Thanks in advance.

4 Answers, 1 is accepted

Sort by
0
Anton Dobrev
Telerik team
answered on 15 May 2014, 07:48 AM
Hello Alex,

The reason for the behavior you are experiencing is that the result for the user login success/error is still not returned at the moment of invoking the logout function.

After the successful login of a user, the JS SDK will use the access_token of the user to authenticate each subsequent request to the API on behalf of this user. Same applies for the logout method - the request must be authenticated with the following authorization header : 
{Authorization : Bearer <access_token_here>}

You need to invoke the logout function when the result from the login returns.

For example:
Everlive.$.Users.login('username', // username
    'password') // password
.then(function (data) {
    alert("Access token: " + data.result.access_token);
    el.Users.logout().then(function () {
            alert('Successful logout');
        }, // success
        function () {
            alert('Failed logout');
        });
});

The relevant parts of our documentation are:
Please, let us know if you have questions.

Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
AlexFStop
Top achievements
Rank 1
answered on 15 May 2014, 08:40 AM
Anton, thanks for your prompt reply, as always.
If I understand you correctly, my error was that I tried to logout before the login had completed. So I moved the logout to a point in the code (the $(document).ready function, after successfully reading 11 records from my backend (earlier in the same function), and it still fails.
It's clear to me that the login has returned by this point, otherwise I wouldn't have been able to read the records. And since I used the el variable (returned by the login statement el = new Everlive({apiKey: '<API key>'});) in the queries in the same function, there's no issue of scope.
Also, it doesn't matter whether I use this:
Everlive.$.Users.logout().then
    (function () {alert('Successful logout');}, // success
         function () {alert('Failed logout'); // failure
        });

or this:
el.Users.logout().then
    (function () {alert('Successful logout');}, // success
         function () {alert('Failed logout'); // failure
        });

Obviously I'm missing something very basic here, but I am totally at a loss as to what that might be.
Thanks again for your help.

0
Anton Dobrev
Telerik team
answered on 15 May 2014, 11:33 AM
Hi Alex,

Could you please use the Network tab in the AppBuilder developer console or a network capture tool like Fiddler to detect the network traffic? The request to http://api.everlive.com/v1/your-api-key/oauth/logout should have attached the authorization header with the bearer token in it in order to logout successfully the user. If the bearer token is not available, the current user's access_token is still not available to the Everlive global object.

The Everlive.$.Users syntax will access the first globally available instance of the Everlive object, so it is pretty much the same as el.Users.

Could you please check if the permissions of the retrieved content type require authenticated access?

In addition, you can trigger a custom event when the user is authenticated successfully and bind the logout function to it. Thus you will be sure that the logout() function will be invoked when the user has been already authenticated - pretty much as the previous example with then().

Here is how to implement this approach:

1. We will use the DOM in order to trigger our custom event:

var EventsAggregator = $(document);

When the user authenticates successfully trigger a custom event:
Everlive.$.Users.login('username', // username
    'password') // password
.then(function (data) {
        EventsAggregator.trigger('user/logged');
    },
    function (error) {
        alert('Unable to connect to network/database - please try again later');
        NotLoggedInToDB = true;
    });

Bind the logout function to the occurrence of the event:
function logout() {
        Everlive.$.Users.logout().then
        (function () {alert('Successful logout');}, // success
         function () {alert('Failed logout');       // failure
        });
    }
     
    EventsAggregator.bind('user/logged', function(){
        logout();
    });

In order to avoid similar quirks, you may want to move the main logic in the success handler of the login() method.

Please, let us know if this helps.


Regards,
Anton Dobrev
Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
0
AlexFStop
Top achievements
Rank 1
answered on 15 May 2014, 12:38 PM
Thanks again for the quick turnaround. I followed your advice and moved things around a bit, and it works just fine now.
Thanks again for all your help.
Tags
JavaScript SDK
Asked by
AlexFStop
Top achievements
Rank 1
Answers by
Anton Dobrev
Telerik team
AlexFStop
Top achievements
Rank 1
Share this question
or