簡體   English   中英

auth :: attempt($ credentials)在laravel 5.1中的其他控制器中不起作用

[英]auth::attempt($credentials) not work in other controller in laravel 5.1

我在嘗試將Laravel與Django應用程序連接時有一個自定義需求。 當前,我沒有使用laravel的默認登錄發布方法來建立用戶會話,而是嘗試訪問Auth :: attempt($ credentials);。 通過這種方式,我能夠在自定義登錄控制器中建立用戶會話,而在其他控制器中則無法建立會話。

登錄控制器:

 $credentials = array('email' => $userjson["email"],'password' => $password);
    Auth::attempt($credentials);
    if(Auth::guest())
     echo "guest";
    else
     return redirect()->intended('/dashboard');

結果:重定向到儀表板頁面(這意味着已建立會話)

儀表板控制器

     if(Auth::check())
              echo "true";
            else
                echo "false";

結果: false(這意味着未建立會話)

有人可以幫我解決這個問題嗎?

使用此代碼。我們需要確保在類的頂部導入Auth外觀。有關更多幫助,請訪問https://laravel.com/docs/5.2/authentication並准備好主題“手動驗證用戶”。 謝謝

namespace App\Http\Controllers;

use Auth;

class OtherController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */
    public function authenticate()
    {
        if (Auth::attempt(['email' => $email, 'password' => $password])) {
            // Authentication passed...
            return redirect()->intended('dashboard');
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM