Browse Source

adds instagram auth

pull/5/head
Aaron Parecki 10 years ago
parent
commit
430609b900
  1. 3
      composer.json
  2. 45
      composer.lock
  3. 40
      controllers/controllers.php
  4. 11
      lib/helpers.php
  5. 31
      views/settings.php

3
composer.json

@ -9,7 +9,8 @@
"indieauth/client": "0.1.3",
"mpratt/relativetime": ">=1.0",
"firebase/php-jwt": "dev-master",
"ruudk/twitter-oauth": "dev-master"
"ruudk/twitter-oauth": "dev-master",
"andreyco/instagram": "3.*"
},
"autoload": {
"files": [

45
composer.lock

@ -3,8 +3,51 @@
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file"
],
"hash": "502847c033f5a54c69a6a1a51d26e894",
"hash": "f2f8fdb671b52ce22dc0a5e133f9a13d",
"packages": [
{
"name": "andreyco/instagram",
"version": "v3.2",
"source": {
"type": "git",
"url": "https://github.com/Andreyco/Instagram-for-PHP.git",
"reference": "726e724c51301410873d9bae9c659a0597ca47e4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Andreyco/Instagram-for-PHP/zipball/726e724c51301410873d9bae9c659a0597ca47e4",
"reference": "726e724c51301410873d9bae9c659a0597ca47e4",
"shasum": ""
},
"require": {
"ext-curl": "*",
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Andreyco\\Instagram\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-4-Clause"
],
"authors": [
{
"name": "Andrej Badin",
"email": "contact@andrejbadin.com",
"homepage": "http://andrejbadin.com"
}
],
"description": "An easy-to-use PHP Class for accessing Instagram's API.",
"homepage": "https://github.com/Andreyco/Instagram",
"keywords": [
"api",
"instagram"
],
"time": "2014-07-14 19:53:19"
},
{
"name": "firebase/php-jwt",
"version": "dev-master",

40
controllers/controllers.php

@ -333,3 +333,43 @@ $app->get('/auth/twitter/callback', function() use($app) {
$app->redirect('/settings');
}
});
$app->get('/auth/instagram', function() use($app) {
if($user=require_login($app, false)) {
$instagram = instagram_client();
// If there is an existing Instagram auth token, check if it's valid
if($user->instagram_access_token) {
$instagram->setAccessToken($user->instagram_access_token);
$igUser = $instagram->getUser();
if($igUser && $igUser->meta->code == 200) {
$app->response()->body(json_encode(array(
'result' => 'ok',
'username' => $igUser->data->username
)));
return;
}
}
$app->response()->body(json_encode(array(
'result' => 'error',
'url' => $instagram->getLoginUrl(array('basic','likes'))
)));
}
});
$app->get('/auth/instagram/callback', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
$instagram = instagram_client();
$data = $instagram->getOAuthToken($params['code']);
$user->instagram_access_token = $data->access_token;
$user->save();
$app->redirect('/settings');
}
});

11
lib/helpers.php

@ -195,3 +195,14 @@ function relative_time($date) {
}
return $rel->timeAgo($date);
}
function instagram_client() {
return new Andreyco\Instagram\Client(array(
'apiKey' => Config::$instagramClientID,
'apiSecret' => Config::$instagramClientSecret,
'apiCallback' => Config::$base_url . 'auth/instagram/callback',
'scope' => array('basic','likes'),
));
}

31
views/settings.php

@ -10,11 +10,8 @@
<h3>Twitter</h3>
<input type="button" id="twitter-button" value="Checking" class="btn">
<!--
<h3>Instagram</h3>
-->
<input type="button" id="instagram-button" value="Checking" class="btn">
</div>
<script>
@ -60,6 +57,10 @@ function save_facebook_token(token) {
}
$(function(){
$("#facebook-button").click(function(){
FB.login(window.quillHandleFbLogin, {scope:'publish_actions'});
});
$.getJSON("/auth/twitter", function(data){
// Check if we're already authorized with twitter
if(data && data.result == 'ok') {
@ -81,8 +82,26 @@ $(function(){
}
});
$("#facebook-button").click(function(){
FB.login(window.quillHandleFbLogin, {scope:'publish_actions'});
$.getJSON("/auth/instagram", function(data){
// Check if we're already authorized with Instagram
if(data && data.result == 'ok') {
$("#instagram-button").val("Connected").addClass("btn-success");
} else if(data && data.url) {
$("#instagram-button").val("Sign In").data("url", data.url).addClass("btn-warning");
} else {
$("#instagram-button").val("Error").addClass("btn-danger");
}
});
$("#instagram-button").click(function(){
if($(this).data('url')) {
window.location = $(this).data('url');
} else {
$.getJSON("/auth/instagram", {login: 1}, function(data){
window.location = data.url;
});
}
});
});
</script>
Loading…
Cancel
Save