簡體   English   中英

使用MongoDB進行Laravel身份驗證

[英]Laravel authentication with MongoDB

我正在嘗試使用Laravel和MongoDB進行身份驗證。

問題出在我的控制器上。 此代碼不進行身份驗證:

    if(!Input::exists('username') or !Input::exists('password')) {
        App::abort(403, 'Forbidden request');
    }

    $credentials = array(
        'username' => Input::get('username', null),
        'password' => Input::get('password', null),
    );

    if(Auth::validate($credentials)) {
        return 'Success';
    } else {
        return 'Failed';
    }

但這很好用:

   if(!Input::exists('username') or !Input::exists('password')) {
        App::abort(403, 'Forbidden request');
    }

    $credentials = array(
        'username' => Input::get('username', null),
        'password' => Input::get('password', null),
    );

    $users = User::all();
    $found = false;
    foreach($users as $user) {
        if($user->username == $credentials['username']
            and Hash::check($credentials['password'], $user->password)) {
            $found = true;
            break;
        }
    }

    if($found) {
        return 'Success';
    } else {
        return 'Failed';
    }

兩個控制器都具有相同的usernamepassword 第一個總是打印Failed ,但是第二個給出真實的結果。

問題是您使用的是Auth::validate ,它僅檢查憑據是否正確,而不登錄用戶。您需要使用Auth::attempt進行完整的登錄。 請參閱http://laravel.com/docs/security#authenticating-users上的文檔。

OK,請始終記住您的模型必須實現UserInterface。

暫無
暫無

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

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