skip to Main Content

I’m trying to use WordPress as a frontend CMS/Shop and Laravel as a platform that provide more advanced services to users that acquire them from WP frontend shop. After logging to WordPress they’ll have in account a link to Laravel dashboard where they have all they’re acquired services. For extracting data from WP I’m using Corcel but I need users already logged in to be automatically authenticated in Laravel.

All discussions I found are related to using same users, or login the other way, from Laravel to WP. Wp and laravel are on same domain, wp in root and laravel in a /laravel dir and they;re working ok. I tried a hook on login to call laravel inside wp but this would run a parallel instance of Laravel.

this in WP as a hook in funtions.php not working:

include $_SERVER['DOCUMENT_ROOT'].'/wplaravel/vendor/autoload.php';$app = include $_SERVER['DOCUMENT_ROOT'].'/wplaravel/bootstrap/app.php';$kernel = $app->make('IlluminateContractsHttpKernel');$kernel->handle($request = IlluminateHttpRequest::capture());

$id = (isset($_COOKIE[$app['config']['session.cookie']]) ? $app['encrypter']->decrypt($_COOKIE[$app['config']['session.cookie']], false) : null);

if ($id) {
    $app['session']->driver()->setId(explode('|', $id)[1]);
    $app['session']->driver()->start();

    // Session::all();
    // $app['auth']->getSession(); //  IlluminateSessionStore
    // Auth::user();
    // $app['auth']->user();
    
    // $user = AppUser::find(1);
  $userl = AppModelsUser::where('email',$user->data->user_email)->first();
  Auth::login($userl);
  //session()->put('wpuser', $user->data->user_email);
  //print_r(session()->all());

}

I need to have a user that login to WordPress to be able to be authorized in Laravel without logging in there also.

2

Answers


  1. First of all your idea is completely wrong if you are trying login from wp to laravel.

    Here you need to do following

    1.Redirect the user to laravel application with a wp session or verified secret

    wp_redirect( "laravelapp.com?secure=secretcodes );
    

    2.Verify the secret from laravel with the given secret

     $verify = $request->secure;
       if($verify == "secretcodes"){
         //verify your own logic here
       }
    

    3.Find the desired user and use this code bellow in any controller that you redirect from wp to laravel

    $user = User::find(1);
    Auth::login($user);
    

    Then it will be logged in into laravel applicaiton

    Login or Signup to reply.
  2. If your WordPress users also have a mobile number, this might be a good option for you.

    It works like this in Laravel :

    $user = User::query()->where(('mobile',$requset->mobile)->first();
    
    Auth::login($user);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search