问题
I'm making a dashboard and pulling some info from the SoundCloud API. I'm relatively new to this API and I got as far as being able to pull the SoundCloud Widget per track listed. My problem is that everytime I refresh, I get signed out (I think). Is there a way for the session to remain even if the user for example refreshes the page? This is what I have right now:
function connectToSoundCloud(){
//INTIALIZE AUTH WITH SC
SC.initialize({
client_id: sc_client_id,
redirect_uri: sc_redirect_uri
});
//INITIATE AUTH POPUP
SC.connect(function() {
SC.get('/me', function(me) {
if (me.username) {
document.getElementById('btn-disconnectFromSoundCloud').style.display="";
document.getElementById('btn-connectToSoundCloud').style.display="none";
}
$.ajax({
type: "GET",
url: "http://api.soundcloud.com/tracks.json?client_id="+sc_client_id+"",
error: function(result, status){
},
success: function(result, status){
//CODE TO DISPLAY TRACKS HERE
}
});
});
});
}
Any help is appreciated. Cheers.
回答1:
Have you concidered jStorage
? Download it from here. Import the jStorage.js file to your code and you are good to go. <script src="YOUR_FOLDER/jStorage.js"></script>
Then you can store the logged in user like this:
SC.connect(function() {
SC.get('/me', function(me) {
$.jStorage.set('userInfo',me); // store the user to storage
console.log("Logged in userId: "+ me.id);
console.log('Logged in username: '+me.username);
location.reload(); // reloads the page
});
});
Then whenever you want to get info about the logged in user, just use:
var loggedInUser = $.jStorage.get('userInfo');
To sign out, just use
$.jStorage.deleteKey('userInfo'); // delete the logged in user
location.reload(); // reloads the page
来源:https://stackoverflow.com/questions/22216815/js-how-to-prevent-losing-session-per-refresh-after-logging-in-in-soundcloud-api