简体   繁体   中英

test if user is logged in laravel 5.7

I am making a test but it fails when it tries to check if a user is logged in:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use App\User;

class RegisterTest extends TestCase
{

    use RefreshDatabase;

    /*.....
    more test about registering
     ....*/    

    /** @test */
    function redirect_to_home_page_and_logged_in_after_login()
    {                   

        $user = factory(User::class)->create([
            'name' => 'Test',
            'email' => 'test@hotmail.com', 
            'password' => '123456'
        ]);     

        $response = $this->post('login', [
            'email' => 'test@hotmail.com',
            'password' => '123456'          
        ]);

        //this works
        $response->assertRedirect('/');

        //this fails 
        $this->assertTrue(Auth::check());


    }
}

And this is my controller HomeController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{


    public function index()
    {                    

        if (Auth::check()){

            return view('home');    
        }

        return view('welcome');

    }
}

And this is my routes/web.php

Route::get('/', 'HomeController@index');    
Auth::routes();

I am not sure what I am doing wrong. What can I do?. I am using laravel 5.7 and phpunit 5.7.1 Also in my app/Htpp/Auth/LoginController.php I did this:

protected $redirectTo = '/'; 

Thank you.

In addition to hashing your password you could also just post to the register route and create a new account.

/** @test */
function redirect_to_home_page_and_logged_in_after_register()
{                      
    $response = $this->post('register', [
        'name' => 'Test',
        'email' => 'test@hotmail.com',
        'password' => '123456'          
    ]);

    //this works
    $response->assertRedirect('/');

    //this fails 
    $this->assertTrue(Auth::check());
}

I guess you may also have a requirement to do it both ways:

/** @test */
function redirect_to_home_page_and_logged_in_after_login()
{                

    $user = factory(User::class)->create([
        'name' => 'Test',
        'email' => 'test@hotmail.com', 
        // note you need to use the bcrypt function here to hash your password
        'password' => bcrypt('123456')
    ]);      

    $response = $this->post('login', [
        'name' => 'Test',
        'email' => 'test@hotmail.com',
        'password' => '123456'          
    ]);

    //this works
    $response->assertRedirect('/');

    //this fails 
    $this->assertTrue(Auth::check());
}

Creating a user requires you to take care of the hashing of the password. You can simply do it by using php's password_hash function. And use Auth::login($user); to login.

Like so:

$user = User::create(['email' => 'r@o.b', 'password' => password_hash('123456', 1)]);
Auth::login($user); //You should be logged in :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM