Laravel 5.1 social Authentication is not working properly - laravel

Recently i am working on laravel 5.1 framework and trying to make user login by Social Authentication like facebook login ..
i followed this tutorial from youtube Laravel 5.1 Socialite Authentication
but get this Exception
InvalidStateException in AbstractProvider.php line 191:
in AbstractProvider.php line 191
at AbstractProvider->user() in SocialAuthController.php line 61
at SocialAuthController->github_Callback()
at call_user_func_array(array(object(SocialAuthController), 'github_Callback'), array()) in Controller.php line 256
at Controller->callAction('github_Callback', array()) in ControllerDispatcher.php line 164
Here is my code
// facebook Sociolite for routing
Route::get('/auth/facebook', 'Auth\SocialAuthController#redirectToProvider');
Route::get('callback_facebook', 'Auth\SocialAuthController#handleProviderCallback');
// AuthController
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Socialite;
use Auth;
use redirect;
class AuthController extends Controller
{
class SocialAuthController extends Controller{
// For Facebook
public function redirectToProvider(){
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback(){
$curl = curl_init();
curl_setopt($curl , CURLOPT_SSL_VERIFYPEER, false);
$user = Socialite::driver('facebook')->user();
$data = ['name'=>$user->name, 'email'=>$user->email, 'password'=>$user->token];
$userDB = User::where('email',$user->email)->first();
if (!is_null($userDB)){
Auth::login($userDB);
}
else{
Auth::login($this->create($data));
}
return redirect('/pages/profile');
}
}
}
config\service.php
'facebook' => [
'client_id' => '513845902100524',
'client_secret' => '135699237e16cd3a50d2cbfec3a5e58c',
'redirect' => 'http://localhost:8000/callback_facebook',
],

You can get detail documentation from laravel official site. Socialite tutorial here.

Related

Passport details api in laravel work fine on local host but not shared hosting

I have created api with passport. My all api works fine on localhost. But when I deploy on shared hosting my login and register api is working fine but details api not working. I rewrite htacess rule but nothing happened. Why this problem occured?
Here is my sample code
Controller Class
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Support\Facades\Auth;
use Validator;
class UserController extends Controller
{
public $successStatus = 200;
public function login(){
if(Auth::attempt(['mobile' => request('mobile'), 'password' => request('password')])){
$user = Auth::user();
$success['token'] = $user->createToken('quiz')-> accessToken;
return response()->json(['success' => $success], $this-> successStatus);
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
public function details()
{
$user = Auth::user()`enter code here`;
return response()->json(['success' => $user], $this-> successStatus);
}
}
Route
<?php
use Illuminate\Http\Request;
Route::post('register', 'UserController#register');
Route::post('login', 'UserController#login');
Route::group(['middleware' => 'auth:api'], function()
{
Route::post('details', 'UserController#details');
});
});
Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
use HasApiTokens;
protected $fillable = [
'name', 'mobile', 'password','city','image',
];
protected $hidden = [
'password', 'remember_token',
];
}
I get error InvalidArgumentException: Route [login] not defined.
Please make a change to the Apache config filr or .htaccess file with the below, hoping it will work:
RewriteEngine On
RewriteCond %{HTTP:Authorization} ^(.+)$
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
for Details visit: https://github.com/laravel/passport/issues/532

Laravel test Passport::actingAs($user) use routes?

I have custom passport user login validation (i made it following this) so i make my custom /oauth/token with this route:
/routes/auth.php
Route::post('/oauth/token', [
'uses' => 'Auth\CustomAccessTokenController#issueUserToken'
]);
/app/controllers/auth/CustomAccessTokenController.php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Psr\Http\Message\ServerRequestInterface;
class CustomAccessTokenController extends Controller
{
public function issueUserToken(ServerRequestInterface $request)
{
$httpRequest = request();
if ($httpRequest->grant_type == 'password') {
$user = User::where('email', $httpRequest->username)->first();
return $this->issueToken($request);
}
}
}
If i make a manual POST request to domain.com/oauth/token is correctly handled by the custom controller but when i use Passport::actingAs($user); in a phpunit test not. This Passport::actingAs(); use the routes or have other way to get the authentication token?
You should be able to get the authentication token using
$this->actingAs($user, 'api');

laravel 5.4 login attempts don't work

I am trying to limit my login attempts but still not working
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
and this is ThrottlesLogin.php
protected function hasTooManyLoginAttempts(Request $request)
{
return $this->limiter()->tooManyAttempts(
$this->throttleKey($request), 3, 2
);
}
and i know in laravel 5.4 the AuthenticatesUsers call by default thethrottlesLogin but still dont limit login attempts.
and thanks
You need to use ThrottlesLogins trait in LoginController

Getting this error continuously: MethodNotAllowedHttpException in RouteCollection.php line 218:

When I tried to log in, on my website. It responded with the message:
FatalErrorException in UserController.php line 37:
Class 'App\Http\Controllers\Auth' not found
But, the file there is controllers folder in Http containing Auth where there are four files.
UserController:
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Input;
//use Illuminate\Support\Facades\Flash;
use InvalidConfirmationCodeException;
use Flash;
//use Mail;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class UserController extends Controller
{
public function getDashboard()
{
return view('dashboard');
}
public function postSignUp(Request $request)
{
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user -> email = $email;
$user -> name = $name;
$user -> password = $password;
Auth::login('$user');
$user->save();
return redirect()->route('dashboard')
}
public function postSignIn(Request $request)
{
if (Auth::attempt(['email' => $request['email'], 'password' => $request['password']])) {
return redirect()->route('dashboard');
}
return redirect()->back();
}
}
also, I am getting an error while signup:
MethodNotAllowedHttpException in RouteCollection.php line 218:
And I am trying to solve this error from a very long time but no success till now.
Please help me with both the errors.
change Auth::login($user) --> \Auth::login($user)
and let me know if you are getting anything in database after signup . :)
To get rid off
FatalErrorException in UserController.php line 37: Class
'App\Http\Controllers\Auth' not found
Add use Auth at the top
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Auth;
Change Auth::login($User) to \Auth::login($user), your $user variable is incorrect as well.
Check your signup route, is it doing a POST (most likely it is) and you're sending a GET request?
You need add
use Auth;
to your Controller.
I highly doubt that the Auth class is inside your Controllers directory. The Auth you are looking for, is probably the Auth facade, can be included this way:
use Illuminate\Support\Facades\Auth;

Laravel 5.1 BadMethodCallException in Controller.php line 283 Method [create] does not exist

I want to create user by using social authentication like Facebook authentication and I followed this tutorial Socialite Authentication in Laravel 5.1 from youtube.
After using this tutorial I got an Exception which is
BadMethodCallException in Controller.php line 283:
Method [create] does not exist.
in Controller.php line 283
at Controller->__call('create', array(array('name' => null, 'email' => 'abdur.razzaq06#gmail.com', 'password' => '4dbf14f26a97584375b7b2c84b6a4786cb1b4a43'))) in SocialAuthController.php line 84
at SocialAuthController->create(array('name' => null, 'email' => 'abdur.razzaq06#gmail.com', 'password' => '4dbf14f26a97584375b7b2c84b6a4786cb1b4a43')) in SocialAuthController.php line 84
at SocialAuthController->github_Callback()
at call_user_func_array(array(object(SocialAuthController), 'github_Callback'), array()) in Controller.php line 256
at Controller->callAction('github_Callback', array()) in ControllerDispatcher.php line 164
at ControllerDispatcher->call(object(SocialAuthController), object(Route), 'github_Callback') in ControllerDispatcher.php line 112
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
What can I do to fix it?
Here is my code:
app\routes.php
// facebook Sociolite
Route::get('/auth/facebook', 'Auth\SocialAuthController#redirectToProvider');
Route::get('callback_facebook', 'Auth\SocialAuthController#handleProviderCallback');
App\Http\Auth|SocialAuthController.php
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Foundation\Validation\ValidatesRequests;
use App\User;
use Validator;
//use ValidatesRequests;
use Socialite;
use Auth;
use redirect;
class SocialAuthController extends Controller
{
// For Facebook
public function redirectToProvider(){
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback(){
$curl = curl_init();
curl_setopt($curl , CURLOPT_SSL_VERIFYPEER, false);
$user = Socialite::driver('facebook')->user();
$data = ['name'=>$user->name, 'email'=>$user->email, 'password'=>$user->token];
$userDB = User::where('email',$user->email)->first();
if (!is_null($userDB)){
Auth::login($userDB);
}
else{
Auth::login($this->create($data)); // line 84
// Auth::login($this->data($data));
}
return redirect('/pages/profile');
}
}
vendor/laravel/framework/src/Illuminate/Routing/Controller.php
<?php
namespace Illuminate\Routing;
use Closure;
use BadMethodCallException;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
abstract class Controller
{
public function __call($method, $parameters)
{
throw new BadMethodCallException("Method [$method] does not exist."); // line 283
}
}
Line 84 calls the create method of your controller. Is it defined? The exception you get implies that it isn't.
You have to actually implement the create method in your SocialAuthController

Resources