I’m using https://github.com/abraham/twitteroauth library. I already created my app on Twitter, the callbacks are working fine (even on localhost), but one thing that is bugging me is HOW the auto-login happens? I know there’s “user_id” that is stored in the MYSQL database, along with oauth_token and oauth_token_secret, but how do I obtain user_id as soon as the user enters the site, so I can query the database to see if it already exists and what not, without having to popup the authorize twitter popup then reaching the callback, over and over again?
I’ve seen a lot of questions like this one, but no one ever answered it in a satisfying way.
actually, it needs to be a mix of PHP own
$_SESSIONwith OAuth callbacks. After the callback, set the current user ID you inserted on the database or query for “user_id” from Twitter OAuth response (that you should ALSO store on the database, along with the username), then use that for future reference, and using$_SESSIONcontaining the registered user data on your own database.So, for a quick example
so basically, the AUTO LOGIN (like the user is already logged to twitter, and already registered to your website) can’t be done without him clicking the LOGIN WITH TWITTER button, unless you set the lifetime of your
$_SESSIONpretty high (like it happens with twitpic for example).$this->user->is_logged()is checking for$_SESSION['id'](auto increment on MYSQL) and$_SESSION['twitter']['user_id']from twitter infoEDIT: Also, the quickest and cleanest way to do it, is to make an AJAX call on the page load with the Twitter credentials using the Javascript SDK, then set the
$_SESSIONvariables with the info the SDK provided.