GrahamCampbell/Laravel-GitHub

token coming from socialite

njames opened this issue · 10 comments

Hi Graham, I would like to use your api to manage repos but have the users tokens coming from socialite used instead of configured connections. Does the manager have the ability to do this or is this outside this scope of this product?

Thanks,
Nigel

Yes, you can do this. :)

I'd recommend using the factory class to do this. That's exactly how StyleCI works.

<?php

use GrahamCampbell\GitHub\GitHubFactory;

app(GitHubFactory::class)->make([/* */]);

Maybe I need to put a couple of examples on the readme since it's such a common use case.

Brilliant thanks for this @GrahamCampbell

The way StyleCI does it is like this:

image

image

Where the bottom line there is:

return $this->factory->make(array_merge(['token' => $token, 'method' => 'token', 'cache' => true, 'backoff' => true], $options));

Thanks Graham, very generous of you to share that.

Once i've created a client like this:
$client = app(GitHubFactory::class)->make(['token' => $user->github_token, 'method' => 'token', 'cache' => true]);
should i be able to access it through your github facade like so:
GitHub::$client->organizations() ?

No, you can't use the facade with the instance you've created. You;ll need to keep track of that instance yourself.

$user = Auth::user();
$token = $user->github_token;
$client = new \Github\Client();
$client->authenticate($token, 'http_token');
dd($client->api('current_user')->organizations());

The difference between my code and the facade is that the facade creates the httpclient with current_user as a parameter and i was trying to access the clients organizations without a current user, which doesn't exist. Thank you graham

@GrahamCampbell I just wanted to say a few things:

  1. you're awesome! Thanks for writing all the things you write!
  2. Thanks for this example! I hope you end up adding this to the readme, as it is immensely helpful :)