2017-02-27 75 views

回答

1

這裏是我想出了

的解決方案,我把那些鉤子在我的網站/的init.php文件

// change login name input label to e-mail-address 
$wire->addHookAfter('ProcessLogin::buildLoginForm', function(HookEvent $event) { 
    // on liner as we don't change anything else 
    $event->return->get('login_name')->set('label', $event->_('E-Mail-Address')); 
}); 

// hook into session::login to get user by mail 
$wire->addHookBefore('Session::login', function(HookEvent $event) { 
    // need to get email from $input as processLogin::execute is pageName sanitizing 
    $email = $event->input->post->email('login_name'); 
    // stop here if login_name not a valid email 
    if (!$email) return; 
    // new selector arrays don't seem to work on $user so using $pages here 
    $user = $event->pages->get([['email', $email]]); 
    // if valid user set login name argument 
    if ($user->id) $event->setArgument('name', $user->name); 
}); 

裸記住,電子郵件是不是唯一的領域,所以如果你不保證電子郵件地址的唯一性,這將無法正常工作,你可以稍微改變它,以克服這一點,雖然..

看看https://processwire.com/talk/topic/1838-login-using-e-mail-rather-than-username-and-general-login-issues/在哪裏瑞安公佈一些更多的信息和可能的解決方案重複的電子郵件地址爲 和https://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/有關前端登錄策略的更多信息

+0

請謹慎使用$ email作爲選擇器,而不必先對其進行消毒。 此外,這是我的這個版本:https://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/?page=4#comment-89616其中檢查輸入的名稱實際上是處理掛鉤之前的電子郵件地址。 – adrian

+0

'$ email = $ event-> input-> post-> email('login_name');'是電子郵件清理值?混合WireInput和消毒劑 – Can

相關問題