Reputation: 1560
I am using GrahamCampbell's wrapper of the Gitlab API and it has configuration variables for token, and method. The token being the secret token used to authenticate against the api, and method being the type of authentication used. I am using oauth, so my method is set to 'oauth.'
However that token is either generated by a request or stored as an encrypted string in my database on the user model in the oauth workflow
where can I feed that oauth token into the gitlab config in the user login workflow to allow me to make requests to the api on behalf of the currently logged in user?
Obviously, I can't just set the config item to 'token' => $user->gitlab_token, as the config is accessed before most of the laravel classes exist.
Or am I incorrect in any of my assumptions?
Upvotes: 0
Views: 365
Reputation: 114
You can do this with listener or middlewares .
in php your app run even you call the Web Server than you can change configs in every api call and other client not effect this .
@apokryfos answer is good.
Upvotes: 0
Reputation: 40683
I think there are multiple ways to do this. I'll offer an alternative:
When a user is authenticated via the session the Authenticated
event is fired, therefore you can listen for this and set the token accordingly. In your EventServiceProvider
add:
Event::listen(Authenticated::class, function (Authenticated $event) {
config([ 'gitlab.token' => $event->user->token ]);
});
This will ensure the configuration is updated when a user is authenticated.
Upvotes: 1
Reputation: 1560
The answer to this, in my case, came from the issues (https://github.com/GrahamCampbell/Laravel-GitLab/issues/27) on the package I am using. You should use the factory to pass the config in when you make the call.
$gitlab = GitLab::getFactory()->make([
'token' => $user->gitlab_token,
'method' => 'oauth',
'url' => 'http://git.company.com/api/v4/'
]);
$projects = $gitlab->projects()->all();
Upvotes: 0