Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from nineteen-2/Improve_config_page
Browse files Browse the repository at this point in the history
Improve config page
  • Loading branch information
lbeauvisage authored Mar 19, 2021
2 parents 1f5c37c + 071b7d4 commit 46359e3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@
## 1.0.0

- initial release


## 1.1.0

- Improve Control Panel
21 changes: 18 additions & 3 deletions resources/views/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@
@section('title', 'Instagram Login')

@section('content')

<div class="no-results md:pt-8 max-w-2xl mx-auto">
<div class="w-full">
<h1 class="mb-4">Instagram Connection</h1>

@if($status === 'CONNECTED')
@if(NineteenSquared\Instagram\InstagramApi::STATUS_HAS_ERROR === $status)
<p class="text-red mb-4">
{{ $error }}
</p>
<a href='{{ $logoutUrl }}' class="btn-danger btn">Logout</a>
@elseif(NineteenSquared\Instagram\InstagramApi::STATUS_CONNECTED === $status)
<p class="mb-1">Connected with account : <strong>{{ $userProfile->username }}</strong></p>

<p class="mb-4">Expiration date : {{ $tokenExpireDate->toIso8601String() }}</p>

<p class="mb-2">Last 5 medias from Instagram :</p>

@if(count($instagramMedias) > 0)
<div class="flex mb-4">
@foreach($instagramMedias as $media)
<img src="{{ $media['thumbnail_url'] ?? $media['media_url'] }}" width="100" class="mr-1">
@endforeach
</div>
@endif

<a href='{{ $logoutUrl }}' class="btn-danger btn">Logout</a>
@elseif($status === 'NOT_CONNECTED')
@elseif(NineteenSquared\Instagram\InstagramApi::STATUS_NOT_CONNECTED === $status)
<a href='{{ $loginUrl }}' class="btn-primary btn-lg">Login with Instagram</a>

<div class="rounded p-3 lg:px-7 lg:py-5 shadow bg-white mt-4">
Expand Down
2 changes: 1 addition & 1 deletion routes/cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Route::prefix('nineteen-ig/')->name('nineteen-ig.')->group(function () {
Route::get('/', 'InstagramLoginController@index')->name('index');
Route::get('/logout', 'InstagramLoginController@logout')->name('logout');
Route::get('/logout', 'InstagramLogoutController')->name('logout');
Route::get('/auth', 'InstagramLoginController@callback')->name('callback');
});
44 changes: 23 additions & 21 deletions src/Http/Controllers/InstagramLoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace NineteenSquared\Instagram\Http\Controllers;

use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplayException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use NineteenSquared\Instagram\InstagramApi;
use NineteenSquared\Instagram\InstagramException;
use Statamic\Http\Controllers\CP\CpController;

class InstagramLoginController extends CpController
Expand All @@ -20,38 +22,38 @@ public function index(InstagramApi $instagram)
{
$this->authorize('setup Instagram');

$status = 'NOT_CONFIGURED';
$status = InstagramApi::STATUS_NOT_CONFIGURED;
$userProfile = null;
$error = null;
$instagramMedias = [];

if (config('statamic.instagram.appId') && config('statamic.instagram.appSecret')) {
$status = 'NOT_CONNECTED';
}
if ($instagram->getUserProfile()) {
$status = 'CONNECTED';
try {
// dd($instagram);
$userProfile = $instagram->getUserProfile();
if (! $userProfile) {
$status = InstagramApi::STATUS_NOT_CONNECTED;
} else {
$status = InstagramApi::STATUS_CONNECTED;
$instagramMedias = $instagram->getUserMedia(5);
}
} catch (InstagramBasicDisplayException | InstagramException | \Exception $exception) {
$error = $exception->getMessage();
$status = InstagramApi::STATUS_HAS_ERROR;
}
}

return view('nineteen-ig::index', [
'status' => $status,
'userProfile' => $instagram->getUserProfile(),
'error' => $error,
'instagramMedias' => $instagramMedias,
'userProfile' => $userProfile,
'tokenExpireDate' => $instagram->getExpireDate(),
'logoutUrl' => route('statamic.cp.nineteen-ig.logout'),
'loginUrl' => $instagram->instagram_basic_display->getLoginUrl(),
]);
}

/**
* @param InstagramApi $instagram
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function logout(InstagramApi $instagram)
{
$this->authorize('setup Instagram');

$instagram->logout();

return redirect(route('statamic.cp.nineteen-ig.index'));
}

/**
* @param InstagramApi $instagram
* @param Request $request
Expand Down
19 changes: 19 additions & 0 deletions src/Http/Controllers/InstagramLogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace NineteenSquared\Instagram\Http\Controllers;

use Illuminate\Http\Request;
use NineteenSquared\Instagram\InstagramApi;
use Statamic\Http\Controllers\CP\CpController;

class InstagramLogoutController extends CpController
{
public function __invoke(InstagramApi $instagram, Request $request)
{
$this->authorize('setup Instagram');

$instagram->logout();

return redirect(route('statamic.cp.nineteen-ig.index'));
}
}
23 changes: 17 additions & 6 deletions src/InstagramApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@

use Carbon\Carbon;
use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay;
use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplayException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class InstagramApi
{
const STATUS_NOT_CONFIGURED = 'NOT_CONFIGURED';
const STATUS_NOT_CONNECTED = 'NOT_CONNECTED';
const STATUS_HAS_ERROR = 'HAS_ERROR';
const STATUS_CONNECTED = 'CONNECTED';

/**
* @var InstagramBasicDisplay
*/
Expand Down Expand Up @@ -75,17 +81,22 @@ public function refreshToken() : void

/**
* @return object
* @throws InstagramBasicDisplayException
* @throws InstagramException
*/
public function getUserProfile() : ? object
{
try {
if ($this->instagram_basic_display) {
return $this->instagram_basic_display->getUserProfile();
}
} catch (\Exception $exception) {
if (! $this->instagram_basic_display || ! $this->instagram_basic_display->getAccessToken()) {
return null;
}

return null;
$userProfile = $this->instagram_basic_display->getUserProfile();

if (isset($userProfile->error)) {
throw new InstagramException($userProfile->error->message);
}

return $userProfile;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/InstagramException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace NineteenSquared\Instagram;

class InstagramException extends \Exception
{
}

0 comments on commit 46359e3

Please sign in to comment.