Logout from LinkedIn API
Asked Answered
U

8

24

I have worked with LinkedIn API and need guidance about logging out from Linkedin API once I click Logout in my application. I have tried the following code:

function closeSession(){
    IN.User.logout();
}
<a href="logout.php" class="myButton" onclick="closeSession()" id="logout-link">Logout In LinkedIn</a>

I have also tried:

$('logout-link').click(function() {  
    IN.Event.on(IN,'logout', function() {
        window.location.href = [site-logout-url];
    });
    IN.User.logout();
});

I tried to destroy the session in a browser by calling session_destroy()

Finally, I tried the answer I got from Stack Overflow for this question. I didn't get any right solution. Can anyone tell me the solution?

Unlimited answered 18/12, 2012 at 4:11 Comment(2)
Reffered this?Mousebird
I can see that your DOM query is not correct and basically it's not able to attach the onclick event. You need to add a hash # when you're querying a DOM element by id: $('#logout-link').click(...)Boden
T
1

Make sure that you have include

<script type="text/javascript" src="http://platform.linkedin.com/in.js"></script>  

on your home page, i.e. where you are logging out.

Transonic answered 18/12, 2012 at 4:40 Comment(0)
N
0

Seems your onclick is not being called, try doing:

function closeSession(){
   IN.User.logout();
   return true;
}
<a href="logout.php" class="myButton" onclick="return closeSession();" id="logout-link">Logout In LinkedIn</a>

OR

<a href="#" class="myButton" onclick="closeSession()" id="logout-link">
    Logout In LinkedIn
</a>

And js

function closeSession() {
  if ( typeof IN === 'object' && typeof IN.User === 'object' && IN.User.isAuthorized() ) {
     //user logged in linkedin
    //call linkedin logout with websites logout function as callback
     alert("I m inside linkedin object check.."); //do you see this
     IN.User.logout(logout);
  }
  else {
     logout();
  }
}
function logout() {
  window.location.href = "URL_TO_YOUR_LOGOUT.PHP";
}
Naughton answered 18/12, 2012 at 4:19 Comment(2)
I have checked that by placing alert(); into that method ,Alert was displayed .Unlimited
@Unlimited see the edited code, try alert()'ing inside if condition of isAuthorized() in the code to see if the IN object actually exist and user is actually authorized with linkedin, hope that worksNaughton
V
0

I think you are redirected before your javascript gets executed, so try this way ? JavaScript Code :

function closeSession(){
    IN.User.logout();
    location.href="logout.php";
}

HTML Code :

<span style="cursor:pointer" onclick="closeSession()">Logout In LinkedIn</span>
Verduzco answered 18/12, 2012 at 4:28 Comment(1)
tell me what happend, does the first line of function executed or not, is the page redirected to logout.phpVerduzco
S
0
To make it work



<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: <?php echo sfConfig::get('app_linkedin_api_key'); ?>
authorize: true
</script>
<script>
IN.Event.on(IN, "logout", function() {onLinkedInLogout();});

function onLinkedInLogout(){
// User is logged out
window.location.href='<?php echo url_for("@homepage");?>'
}
</script>
<a href="#" onclick="IN.User.logout()"><?php echo __("Logout");?></a>

<div class="signin"><script type="in/Login" data-onAuth="onLinkedInAuth"></script>

<script type="text/javascript">

function onLinkedInAuth() {
IN.API.Profile("me")
.fields("id")
.result( function(me) {
var id = me.values[0].id;
//Do stuff like redirect...
})
}
</script>
Spoiler answered 28/12, 2012 at 13:15 Comment(0)
B
0

There seems to be 2 different issues here. You can't logout of linked in from your server side script unless you call their function. When you land on logout.php you should have your session_destroy(); on top of the page. Make sure there isn't any session_start() on logout.php

Benthos answered 16/9, 2014 at 5:31 Comment(0)
F
0

You need to load the LinkedIn JavaScript platform before trying to call IN.User.logout(), you should run the code only after the javascript library has loaded completely.

<script type="text/javascript" src="http://platform.linkedin.com/in.js">
  api_key: mykey
  authorize: true
  onLoad: onLoad
</script>

<script type="text/javascript">
function onLoad() {
  try {
    IN.User.logout();
  } catch (err) {
    console.log(err);
  }
  location.href="index.php";
}
</script>
Faliscan answered 23/6, 2016 at 8:36 Comment(0)
P
0
<script type="text/javascript" src="http://platform.linkedin.com/in.js">
api_key: mykey
authorize: true
onLoad: onLoad
</script>

           <script type="text/javascript">
                 function onLoad() {
                  try {
              IN.User.logout();
                       } catch (err) {
                  console.log(err);
                          }
                  location.href="index.php";
                    }
                     </script>
Piotr answered 6/10, 2016 at 13:50 Comment(0)
K
-3

use session_destroy() in the code.

Kowtko answered 22/12, 2014 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.