Auth :: check () returns false in Laravel 5.4

I use laravel 5.4 as the backend for my application, and for the front-end I use angular. I use laravel authfor authentication.

The problem is Auth::attempt()working fine, and if I print it immediately Auth::user(), then it prints the data, but returns falseif I try to extract it in the next method. But this functionality works fine on a hosted server.

Tested

  • Change session from file to database.
  • Changes to kernel.php (middleware content).
  • Did php artisan make: auth again.
  • Changes to the user table column.
  • Adding private $ primarykey = 'id' to the model.
  • Adding web middleware to all routes.

This is my controller

namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\User;

public function login()
{
 if (Auth::attempt(['email' => $email, 'password' => $password ])) 
        {
            $response = array('response' =>'Succssfully Login!' , 'success' => true);

            return $response;
        }
 }

Here I use Auth::check()in the same controller

public function check() 
{
    if(Auth::check())
        $response = array('response' =>'Authenticated' , 'success'=>true);
    else
        $response = array('response' =>'UnAuthenticated' , 'success'=>false);

    return $response;    
}

I am confused because the same code works fine on the server but doesn't work on localhost. Do I need to make any http related changes to laravel for this?

+4
source share
1 answer

In case your Angular application is outside of laravel and loaded without using the clip template to load your Angular application entry point, i.e. Angular index.html, then what happens is that Laravel cannot establish a session for the application, so the next time you request , your laravel will not be able to recognize the session, so it gives falsewhen you call Auth::check().

(, Angular ), Laravel Passport password, JWT, https://github.com/tymondesigns/jwt-auth < - . ( readme )

Angular - JWT.

0

Source: https://habr.com/ru/post/1695514/


All Articles