I'm using Laravel 8 to develop my project and I would like to use Google authentication system for my users to login.
So I downloaded package via composer require laravel/socialite command and added my information on .env:
GOOGLE_CLIENT_ID=example
GOOGLE_SECRET_KEY=example
GOOGLE_CALLBACK_URL=http://localhost:8000/auth/google/callback
And then I defined them on config/services.php:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_SECRET_KEY'),
'redirect' => 'GOOGLE_CALLBACK_URL',
],
After that I created a controller on my auth/ directory which is called GoogleAuthController and goes like this:
use Laravel\Socialite\Facades\Socialite;
class GoogleAuthController extends Controller
{
public function redirect()
{
return Socialite::driver('google')->redirect();
}
}
And finally at my login blade:
Login with Google
But the problem with this is that it, whenever I test this, it says:
Error 400: invalid_request
Invalid parameter value for redirect_uri: Missing scheme: GOOGLE_CALLBACK_URL
So why am I receiving this error ? How to fix it ?
I really appreciate any idea or suggestion from you guys...
Thanks in advance.
Also if you want to take a look at my routes, here it is:
Route::get('/auth/google', [App\Http\Controllers\Auth\GoogleAuthController::class, 'redirect'])->name('auth.google');
Your config is wrong:
'redirect' => 'GOOGLE_CALLBACK_URL',
Should be
'redirect' => env('GOOGLE_CALLBACK_URL'),
And of course this redirect should point to Google, not your localhost.
Related
Facebook, LinkedIn, Google, GitHub, GitLab, Bitbucket and many others support OAuth2 for user-based authentication. but Twitter still using Oauth1 only .
if ($provider === "twitter") {
return Socialite::driver($provider)
->userFromTokenAndSecret(
env("TWITTER_ACCESS_TOKEN"),
env("TWITTER_ACCESS_TOKEN_SECRET")
)
->redirect();
Running into this issue :
Symfony\Component\Debug\Exception\FatalThrowableError
Call to undefined method Laravel\Socialite\One\User::redirect()
I want to be redirected into the callback :
https://example.com/api/login/twitter/callback
Any Help !!
You can use Socialite::with($provider)->redirect(); to redirect. But you have to first store the callback URL in your config/services.php file with your ids.
'twitter' => [
'client_id' => env('TWITTER_CLIENT_ID'),
'client_secret' => env('TWITTER_CLIENT_SECRET'),Client Secret
'redirect' => env('TWITTER_CALLBACK_URL'),
],
Then inside your callback function Socialite::driver($provider)->user(); to get the user.
Because my web app has 2 separate logins, but both want to use the Facebook login methods, but there is a problem when I want to callback to 2 different controllers but that doesn't work.
config file .env
FACEBOOK_APP_LOGIN_CALLBACK_URL=http://localhost/auth/callback/facebook
FACEBOOK_APP_LOGIN_CALLBACK_URL_CLIENT=http://localhost/client/auth/callback/facebook
config file services.php
'facebook' => [
'client_id' => env('FACEBOOK_APP_LOGIN_ID'),
'client_secret' => env('FACEBOOK_APP_LOGIN_SECRET'),
'redirect' => env('FACEBOOK_APP_LOGIN_CALLBACK_URL'),
],
By default when logging in http://localhost/auth/callback/facebook will be used, it works fine.
But in the second login method I want to change the redirect URL to http://localhost/client/auth/callback/facebook in the Controller, in Controller I did like this but did not work
public function redirectToProvider($social) {
$redirectUrl = "http://localhost/client/auth/callback/facebook";
return Socialite::driver($social)->redirectUrl($redirectUrl)->redirect();
}
Facebook has returned an error:
Client error: `POST https://www.googleapis.com/oauth2/v4/token` resulted in a `400 Bad Request` response: { "error": "invalid_grant", "error_description": "Malformed auth code." }
How to change the Url redirect dynamically?
Thanks everyone!
I have this strange issue with using Laravel Socialite to log users in via Google API.
Everything configurations seem normal and ordinary, but I keep getting error Missing required parameter: client_id. Not only that, sometimes I tried again and the error became Missing required parameter: redirect_uri despite all these parameters have been supplied.
This is how it is set up:
service.php
'google' => [
'client_id' => 'xxxxxxxxxx-x98asxxxx913ofk5tqvgq7lqfpgdi5u2.apps.googleusercontent.com',
'client_secret' => 'mklOWJFhpbCzTKxxxx-xxxx',
'redirect' => 'http://myapp.com/auth/google/callback'
],
routes.php
Route::get('/auth/{social_channel}', 'Auth\AuthController#redirect_to_provider');
Route::get('/auth/{social_channel}/callback', 'Auth\AuthController#handle_provider_callback');
AuthController.php
/**
* Redirect the user to the Google authentication page.
*
* #return Response
*/
public function redirect_to_provider($social_channel)
{
return Socialite::driver($social_channel)->redirect();
}
/**
* Obtain the user information from GitHub.
*
* #return Response
*/
public function handle_provider_callback($social_channel, SocialAccountService $service)
{
$user = $service->create_or_get_user($social_channel, Socialite::driver($social_channel)->user());
Auth::login($user);
return redirect()->to('/go');
}
Facebook Login works for me, just this Google Login issue is a stubborn and bizarre problem.
The app is deployed on Forge (Ubuntu, Nginx).
Can anybody spot anything wrong with this at all?
With env() function in the services.php file, you must use GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET in the .env file, without white spaces.
GOOGLE_CLIENT_ID=xxxxxxxxxxxx
GOOGLE_CLIENT_SECRET=xxxxxxxxxxxxxx
in the services file, use this
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
I just solved the same problem, my first solution is by passing the client_id and secret on the controller using the ->with(). Code goes like this:
return Socialite::driver('google')
->with(
['client_id' => 'xxxxxxxx'],
['client_secret' => 'xxxxxxxx'],
['redirect' => 'http://localhost:8000/Yourcallback'])
->redirect();
OR just by removing the env() on services.php
'client_id' => env('xxxxxxx'),
'client_secret' => env('xxxxxxx'),
with this
'client_id' => 'xxxxxxx',
'client_secret' => 'xxxxxxx',
Its seems that Socialite does not recoginized env() function.
For people out there that are using Forge, you may need to add the google .env information in the environment tab on your Forge panel. This .env information is not pushed through Github to Forge.
I try to use the package Laravel\Socialite in my system in Lumen (5.1)
I added this in the config\services.php file :
<?php
//Socialite
'facebook' => [
'client_id' => '##################',
'client_secret' => '##################',
'redirect' => 'http://local.dev/admin/facebook/callback',
],
In bootstrap\app.php file :
class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite');
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);
Then I created a controller for the facebook authentication :
<?php
namespace App\Http\Controllers\Facebook;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\Factory as Socialite;
class FacebookController extends Controller
{
public function redirectToProviderAdmin()
{
return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect();
}
public function handleProviderCallbackAdmin()
{
$user = Socialite::driver('facebook')->user();
}
}
And in the routes.php :
$app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\FacebookController#redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\FacebookController#handleProviderCallbackAdmin');
I just followed the documentation, changing according to my needs. When I go to page http://local.dev/admin/facebook/login, I get the following error :
Non-static method Laravel\Socialite\Contracts\Factory::driver() cannot be called statically, assuming $this from incompatible context
Indeed, according to the code, driver function must be instanciate.
EDIT : And if I try to instanciate this class, I get the following error :
Cannot instantiate interface Laravel\Socialite\Contracts\Factory
How do you make this module to work?
here's how that works in my case
in services.php file
'facebook' => [
'client_id' => '***************',
'client_secret' => '***************',
'redirect' => ""
],
i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example
"http://example.com:8000/my-callback/";
also check your config/app.php. in providers array
Laravel\Socialite\SocialiteServiceProvider::class,
in aliases array
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
my routes look like this
Route::get('facebook', 'Auth\AuthController#redirectToProvider');
Route::get('callback', 'Auth\AuthController#handleProviderCallback');
here's auth controllers methods. put in top
use Socialite;
//იობანი როტ
public function redirectToProvider(Request $request)
{
return Socialite::with('facebook')->redirect();
}
public function handleProviderCallback(Request $request)
{
//here you hadle input user data
$user = Socialite::with('facebook')->user();
}
my facebook app
giga.com:8000 is my localhost (so its the same localhost:8000)
as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just
http://your-domain-name.com:8000/callback
if you work on localhost, you should name your domain in config/services.php
mine look like this
'domain' => "your-domain.com",
after everything run these commands
php artisan cache:clear
php artisan view:clear
composer dump-autoload
restart your server, clear your browser cookies. hope it helps
I'm trying to send an email to all the users in the database, but I'm getting the following error: [GuzzleHttp\Exception\ClientException] Client error: 400.
Here's my code:
$users = App\User::all();
foreach($users as $user) {
$code = new App\Code();
$code->code = str_random(10);
$code->save();
Mail::send('emails.code', ['code' => $code], function($message) use ($user)
{
$message->to($user->email)->from('foo.bar#gmail.com', 'Foo Bar')->subject('New code, new chances!');
});
}
[GuzzleHttp\Exception\ClientException] Client error: 400
is for URL not found
Check for host url in config/mail.php, host url might be wrong
Not sure if you have got it working but I had the exact same problem today and I had no idea what went wrong as I did the exact same implementation just in my previous Laravel project with mailgun. Then I found the issue in the configuration.
So, here is the glitch..I don't know in which version it was changed, but in config/services.php the mailgun config is now like this,
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
where it was like this before as far as I can remember,
'mailgun' => [
'domain' => env('MAIL_DOMAIN'),
'secret' => env('MAIL_SECRET'),
],
so in my .env file, I was only referencing MAIL_DOMAIN and MAIL_SECRET environment variables but I just realised in the services.php file, it's actually referencing different environment variables - MAILGUN_DOMAIN and MAILGUN_SECRET.
So I've just added these environment variables, cleared the config cache and it is working perfectly now.
(I am working with Laravel 5.1.23 version BTW)
Hope this helps. Thanks.