Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow commenting on Gistlog without leaving UI #78

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7e69229
feat: Add Socialite
JacobBennett Aug 17, 2015
12fc7a8
feat: Update user migration and User model for Socialite
JacobBennett Aug 17, 2015
f16c308
feat: Create routes and modify AuthController to accommodate Github l…
JacobBennett Aug 17, 2015
e239a6d
wip: Have Github comments working for an authenticated user
JacobBennett Aug 17, 2015
688354a
whip: only allow logged in users to comment. Swap out test route for …
JacobBennett Aug 18, 2015
afc259e
feat: have comment box working and rudimentary login button working
JacobBennett Aug 20, 2015
1eda04c
feat: comments input refilling after returning from a sign-in flow
JacobBennett Aug 22, 2015
9ce869c
feat: Show and hide comment button when the textarea field is focused
JacobBennett Aug 23, 2015
61edb00
refactor: extract commentForm.js to own file
JacobBennett Aug 23, 2015
1d92395
fix: remove compiled css files
JacobBennett Aug 24, 2015
d7c5b0b
merge update to 5.1 branch
JacobBennett Aug 24, 2015
564b50f
refactor: remove unnecessary method abstraction
JacobBennett Aug 24, 2015
c3c14d5
chore: code cleanup
JacobBennett Aug 24, 2015
3d4f38b
Merge branch 'chore/jb-update-to-5.1' into feature/jb-allow-comments-…
JacobBennett Aug 25, 2015
c7a95a1
chore: remove compiled css
JacobBennett Aug 25, 2015
a7688c0
fix: remove extra space
JacobBennett Aug 29, 2015
31323cd
fix: Update and alphabetize use block, replace session put with sessi…
JacobBennett Sep 7, 2015
4aea31b
feat: focus on comment box if refilling the contents when returning f…
JacobBennett Sep 7, 2015
08afb6d
fix: make email and name fields nullable.
JacobBennett Sep 7, 2015
995ba32
fix: alphabetize property names in less files, and commit compiled css
JacobBennett Sep 7, 2015
fecce4a
chore: refactor feature to focus on comment box
JacobBennett Sep 7, 2015
d1ce148
chore: break if statements to multiple lines, remove extra line from …
JacobBennett Sep 7, 2015
44fd732
chore: update composer lock file
JacobBennett Sep 7, 2015
c7e5f53
Merge branch 'master' into feature/jb-allow-comments-from-gistlog
JacobBennett Sep 7, 2015
06317e6
Merge branch 'master' into feature/jb-allow-comments-from-gistlog
JacobBennett Sep 15, 2015
553539f
fix: update service providers to match suggestions by @deringer. Only…
JacobBennett Sep 15, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ SESSION_DRIVER=file

GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
GITHUB_URL=http://host/auth/github/callback
13 changes: 13 additions & 0 deletions app/Gists/GistClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Github\Client as GitHubClient;
use Github\HttpClient\Message\ResponseMediator;
use Illuminate\Support\Facades\Auth;

class GistClient
{
Expand Down Expand Up @@ -42,4 +43,16 @@ public function getGistComments($gistId)
$response = $this->github->getHttpClient()->get("gists/{$gistId}/comments");
return ResponseMediator::getContent($response);
}

/**
* @param $gistId
* @param $comment
* @return array
*/
public function postGistComment($gistId, $comment)
{
$this->github->authenticate(Auth::user()->token, GitHubClient::AUTH_HTTP_TOKEN);
$response = $this->github->getHttpClient()->post("gists/{$gistId}/comments", json_encode(['body' => $comment]));
return ResponseMediator::getContent($response);
}
}
116 changes: 85 additions & 31 deletions app/Http/Controllers/Auth/AuthController.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,92 @@
<?php namespace Gistlog\Http\Controllers\Auth;

use Gistlog\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
use Gistlog\User;
use Exception;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alphabetize use block plz

use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;
use Laravel\Socialite\Facades\Socialite;
use Validator;

class AuthController extends Controller {

/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/

use AuthenticatesAndRegistersUsers;

/**
* Create a new authentication controller instance.
*
* @param \Illuminate\Contracts\Auth\Guard $auth
* @param \Illuminate\Contracts\Auth\Registrar $registrar
* @return void
*/
public function __construct(Guard $auth, Registrar $registrar)
{
$this->auth = $auth;
$this->registrar = $registrar;

$this->middleware('guest', ['except' => 'getLogout']);
}
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}

/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
session()->put('redirect_to', URL::previous());
return Socialite::driver('github')
->scopes(['gist'])
->redirect();
}

/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
try {
$user = Socialite::driver('github')->user();
} catch (Exception $e) {
return Redirect::to('auth/github');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we give error messages here?

}

$authUser = $this->findOrCreateUser($user);

Auth::login($authUser, true);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line


return redirect($this->referringUrl());
}

private function findOrCreateUser($user)
{
if ($authUser = User::where('github_id', $user->id)->first()) {
return $authUser;
}

return User::create([
'name' => $user->name,
'email' => $user->email,
'github_id' => $user->id,
'avatar' => $user->avatar,
'token' => $user->token
]);
}

public function getLogout()
{
Auth::logout();

return redirect('/');
}

private function referringUrl()
{
$redirect_to = session()->get('redirect_to', 'home');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason we don't just do Session::flash()? (same as session put but disappears after one load)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to flash, but out of curiosity, would flashing the session survive the OAuth ping pong that goes on, or does that all happen in the background? I think that was my original purpose in using put and get on the session.

I tested it out and it seemed to work fine though so I'll stick with flash for now I suppose.

session()->forget('redirect_to');
return $redirect_to;
}
}

9 changes: 9 additions & 0 deletions app/Http/Controllers/GistsController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php namespace Gistlog\Http\Controllers;

use Gistlog\Exceptions\GistNotFoundException;
use Gistlog\Gists\GistClient;
use Gistlog\Gists\GistlogRepository;
use Gistlog\Http\Requests;
use Gistlog\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
Expand Down Expand Up @@ -53,4 +55,11 @@ public function show($username, $gistId)
->with('gistlog', $gistlog)
->with('pageTitle', $gistlog->title . ' | ' . $gistlog->author);
}

public function postComment(Request $request, GistClient $client, $gistId)
{
$this->validate($request, ['comment' => 'required']);
$client->postGistComment($gistId, Input::get('comment'));
return redirect()->back();
}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Kernel extends HttpKernel {
'auth' => 'Gistlog\Http\Middleware\Authenticate',
'auth.basic' => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
'guest' => 'Gistlog\Http\Middleware\RedirectIfAuthenticated',
'csrf' => 'Gistlog\Http\Middleware\VerifyCsrfToken',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation issues but doesn't really bother me all that much since we're bout to re-indent the whole thing anyway

];

}
6 changes: 6 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php


Route::get('/', ['uses' => 'HomeController@index', 'as' => 'home']);

Route::get('logout', 'Auth\AuthController@getLogout');
Route::get('auth/github', 'Auth\AuthController@redirectToProvider');
Route::get('auth/github/callback', 'Auth\AuthController@handleProviderCallback');

Route::post('posts/create', 'GistsController@storeAndRedirect');
Route::post('comment/{gistId}', ['middleware' => ['auth', 'csrf'], 'uses' => 'GistsController@postComment', 'as' => 'comments.store']);

Route::get('{username}/{gistId}', ['uses' => 'GistsController@show', 'as' => 'gists.show']);

Expand Down
2 changes: 1 addition & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
protected $fillable = ['name', 'email', 'github_id', 'avatar', 'token'];

/**
* The attributes excluded from the model's JSON form.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "project",
"require": {
"laravel/framework": "5.1.*",
"laravel/socialite": "^2.0",
"michelf/php-markdown": "~1.4",
"knplabs/github-api": "~1.4",
"symfony/yaml": "~2.6"
Expand Down
2 changes: 2 additions & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@

'Gistlog\Providers\GistClientServiceProvider',
'Gistlog\Providers\ContentParserServiceProvider',
Laravel\Socialite\SocialiteServiceProvider::class
],

/*
Expand Down Expand Up @@ -195,6 +196,7 @@
'URL' => 'Illuminate\Support\Facades\URL',
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',
'Socialite' => Laravel\Socialite\Facades\Socialite::class,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces must be used to indent lines; tabs are not allowed

],

];
1 change: 1 addition & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'github' => [
'client_id' => env('GITHUB_CLIENT_ID'),
'client_secret' => env('GITHUB_CLIENT_SECRET'),
'redirect' => env('GITHUB_URL'),
'token' => env('GITHUB_TOKEN'),
],
];
11 changes: 7 additions & 4 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ class CreateUsersTable extends Migration {
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');

$table->string('github_id')->unique();
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('email');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried running this locally and it didn't bring back an email. Not sure why, but I think we need to make email and name nullable, basd on my experiences with giscus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have the user:email scope set up in the AuthController is the reason it wasn't bringing back the email. Fixed that. It's not completely necessary that we know it, but if you ever wanted to integrate giscus with gistlog it could be helpful.

$table->string('avatar');
$table->string('token');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have live data already, you'll have to add this modification as a new migration; otherwise it won't be run on the live site.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(hm, strike that.. it kinda looks like there's ... no actual database so far? HA. Nevermind. This is fine.)... we'll just need to remember to actually add a db before we merge this one


$table->rememberToken();
$table->timestamps();
});
Expand Down