Laravel Socialite - handleProviderCallback() method not showing stored session - php

I am trying to integrate Socialite in my Laravel project. I am trying to store a session in buyerSignup.blade.php file and then trying to get that session value in Socialite's handleProviderCallback() method. But it is not showing any value don't know why. Although the other method
redirectToProvider() showing the session value.
I need that session value in handleProviderCallback() method to process it and take actions based upon the value of session. Below is a actual of code that I am using.
Storing a session value in buyerSignup.blade.php
#php
\Session::put('buyerSignupFb','true');
#endphp
Trying to get the above stored value in LoginController's handleProviderCallback() method.
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
use App\sellerData;
use App\buyerData;
use App\sellerDealCat;
use App\subCatData;
use App\Session;
use App\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use Socialite;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->middleware('guest:seller')->except('logout');
$this->middleware('guest:buyer')->except('logout');
}
public function redirectToProvider()
{
return Socialite::driver('facebook')->redirect();
}
public function handleProviderCallback()
{
//cant get the value of the session defined in buyerSignup.blade.php
// echo doesn't work too
return \Session()->get('buyerSignupFb');
}
}
Any suggestion what I am doing wrong or how to make it work. TIA

U doing all good, but not enought, first u set a redirect provider
public function redirectToProvider()
{
return Socialite::driver('facebook')->stateless()->redirect();
}
it's good, when user will give access to his token, u need to handle callback in handleProviderCallback(), where u need to exchange user auth code to acces token. All of this socialite makes automatically, all what u need its just call it
public function handleProviderCallback()
{
$externalUser = Socialite::driver('facebook')->stateless()->user();
\\check if user exists, if not create
$auth->login($user, true);
if ($user->type = 'seller'){$this->redirectTo = '/forSellers'} else {$this->redirectTo = '/forOtherGroup'}
return redirect($this->redirectPath());
}

Related

How to change the redirect when entering the Login Route via a back button after authentication in Laravel?

I have a middleware that redirects to the Login page and everything works fine:
*If I try to enter a page restricted by this middleware I am sent to login page and then I'm sent to that page.
*If I type /login directly, I will be redirected to /about.
I'm using a custom guard called "client".
Here is my code:
<?php
namespace App\Http\Middleware;
use Illuminate\Support\Facades\Auth;
use Closure;
class ClientLoggedIn
{
public function handle($request, Closure $next)
{
if (!Auth::guard('client')->check()) {
session(['url.intended' => url()->full()]);
return redirect()->route('login');
}
return $next($request);
}
}
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
class LoginController extends Controller
{
use AuthenticatesUsers;
protected function redirectTo()
{
$intendedURL = request()->session()->get('url.intended');
if (isset($intendedURL)) {
return $intendedURL;
}
return '/about';
}
public function __construct()
{
$this->middleware('guest:client')->except('logout');
}
protected function guard()
{
return Auth::guard('client');
}
public function showLoginForm()
{
return view('auth.login');
}
}
Whenever I click the back button after the authentication, however, a redirect to the site home (localhost:8000/) occurs (the back button refers to /login)
My question is: is there some configuration or callback that sets it to the home? I thought it would be the redirectTo, but it doesn't seem to be it since it is already set to /about. For what I know, when I click back doesn't occur a request to the server, so, how it works and how to change it?
Thanks in advance.

Laravel 6 session() helper is not working?

I have this very simple class:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function __construct()
{
// $this->middleware('auth');
}
public function home(Request $request)
{
echo "setting help key";
session()->put('help', 'me');
session(['sos' => 'me']);
dump(session('help'));
dump(session('sos'));
}
public function home2(Request $request)
{
dump(session('help'));
dump(session('sos'));
}
//...
which dumps the vars successfully in the home() page, but when i access to the home2() page, it fails. Maybe it has something to do with me disabling the default middleware('auth'), but i'm not sure (also, if that is the case, how to use sessions without forcing the login)
try
session(['key' => 'value']); // to store
session()->get('key'); // to get the value
session()->forget('key'); // to unset the session attribute
read also: https://laravel.com/docs/5.8/session (change the Laravel version at the top-right corner...)
P.S. Sometimes you can't just dump data. Try var_export or what's better, just debug.

How to pass user's data from LoginController to my UserController

I want to return Users data after registering to UserController through LoginController.
Below is the LoginController, I have modified the default email login with student's index number:
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\BaseController;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends BaseController {
public function username()
{
return 'index_no';
}
public function redirectTo(Request $request)
{
$id = $request->input('id');
return redirect()->route('users', ['id' => $id]);
}
}
While redirecting I want the users id, but I get this:
FatalThrowableError error Too few arguments to function App\Http\Controllers\Auth\LoginController::redirectTo()
You are defining an argument for the redirectTo() function that, by default, expects no params. Try this instead:
public function redirectTo()
{
return redirect()->route('users', ['id' => auth()->id()]);
}
The explanation is, given the fact that this redirect will happen after a successfull login, the user is already logged in, so you are able to use the Auth facade or auth() helper to retrieve the info of the logged in user.
i just needed to eliminate the redirect() method
public function redirectTo()
{
return route('auth.clearance',['id'=>auth()->id()]);
}

how to completely logout in laravel 5.5 controller?

After I logout of my laravel application, in the browser I press the button to backward (go back) and then I see the dashboard.
I want to eliminate this "session" that laravel mantein if I go back.
can anyone help me?
EDIT: I have two login files, one is inside the Controllers/Auth and another is inside the Controller/. I'm sure this is not a good practice, but it's keeping my system up and running. how to solve this?
Controllers/Auth/LoginController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* #return void
*/
private $user;
}
my Login Controllers/LoginController.php ->
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
class LoginController extends Controller
{
private $user;
public function logout(){
Auth::logout();
\Session::flash('success',"logout");
return redirect()->route('login');
}
}
my DashboardController ->
use App\Authorization;
use App\BackLog;
use App\Key;
use App\isKeyInUse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
class DashboardController extends Controller
{
public function index() {
return view('dashboard');
}
}
my web.php ->
<?php
Route::get('/', 'LoginController#login')->name('login');
Route::get('auth/logout', 'Auth\LoginController#logout')->name('logout');
Route::get('/dashboard', 'DashboardController#index')->name('dashboard')->middleware('auth');
Route::post('/dashboard/getKey', 'DashboardController#getKey')->name('dashboard.key')->middleware('auth');
This is happening because caching. to prevent that we can create a middleware that intercepts every request and set the cache to expire in0 time and thus it will force the page to reload when the user press the back button here's the steps to create the middleware :
first
create a middleware i will call it MyAuth:
php artisan make:middleware MyAuth
second
register the middleware in app/Http/kernel.php
protected $routeMiddleware = [
...,
'my_auth' => \App\Http\Middleware\MyAuth::class,
];
third
in the newly created middleware app/Http/Middleware/MyAuth.php
public function handle($request, Closure $next, $guard = null)
{
$response = $next($request);
return $response
->withHeaders([
'Cache-Control' => 'no-store, no-cache, must-revalidate',
'Pragma'=> 'no-cache',
'Expires' => '0'
]);
}
}
Then
you can add your middleware like so:
Route::group(['middleware' => 'my_auth'], function() {
// All your routes you want to be secure
});
This code has been derived from this video
You are missing Request in logout function
public function logout(Request $request){
Auth::logout();
\Session::flash('success',"logout");
return redirect()->route('login');
}
And write in your dashboard controller
public function __construct()
{
$this->middleware('auth');
}
Insert these lines to your Dashboard controller and then check:
public function __contruct()
{
$this->middleware('auth');
}
This will check user is logged in or not? If user is loggedout, then it send to specific login page as you defined in auth middleware.
Pressing the Back button of your browser will load the previously loaded document. It is just visible but will not work for sure. For this you just have to override back press event from javascript.
See link How to Detect Browser Back Button event - Cross Browser
In Laravel 7.x, you can logout from the controller by using the following command:
Auth::logout()

how to protect to delete previous value of class element

I have controller class in laravel in which i have a function create() and a variable attachment i am calling function by ajax
my class code is.
class AttachmentController extends Controller
{
public $_attachments;
public function create()
{
$this->_attachments[]= 'test';
var_dump($this->_attachments);
}
problem is every time when i call it by ajax it return me "test" at 0 index of attachment array . but i want if i call create function 1st time it give me test on 0 index but when next time when i call it . it give me "test" on both 0 and 1 index and so on ..
how it is possible please help me
To preserve the values between requests you need to store them somewhere, an alternative is to use sessions, as the example below, for more information see https://laravel.com/docs/session
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class AttachmentController extends Controller
{
public function create(Request $request)
{
$request->session()->push('attachments', 'test');
var_dump($request->session()->get('attachments'));
}
}
Try something like this.
$_attachments[] = Session::get('test');
$_attachments[] = 'test';
Session::get('test', $_attachments);
dd(Session::get('test'));
let me know if this is what you want.
Since HTTP driven applications are stateless, sessions provide a way
to store information about the user across requests.
class AttachmentController extends Controller
{
public function create()
{
$attachments = Session::get('attachments', array());
$attachments[] = 'test';
Session::put($attachments);
var_dump($this->_attachments);
}
}
Further reading: Laravel Session

Categories