2013-03-13 182 views
3

用戶註冊後,我發送了一封電子郵件來激活帳戶,但用戶可以登錄,即使帳戶沒有被激活。如何驗證用戶帳戶是否已激活?註冊後激活用戶帳戶

if (Auth::attempt(array('email' => $email, 'password' => $password))) { 
    // Success login 
} else { 
    // Login fail 
} 
+0

如果一個未激活的用戶可以登錄,這並不一定是壞事。您只需確保他們沒有權限執行他們不應該做的事情。 – dualed 2013-03-14 11:45:08

回答

4

您必須查詢數據庫。

,如果您有以下數據庫架構:

users 
id  name email active 
--  ---- ----- ------ 
1  alex [email protected] 0 

您不妨做到以下幾點

if(! User::where_email($email)->only('active')) // Adapt the where to your needs 
    echo "User not active" 
else 
    echo "User active" 

我寫了一個Laravel REST API演示服務(我用它),這實際上有一個它檢查用戶是否被激活的部分:在這裏找到https://github.com/w0rldart/lRapi/blob/master/api/controllers/api/user.php

+0

很酷的答案,但它似乎像你的github回購消失了... – driechel 2013-04-22 10:30:51

+1

我更新了鏈接。是一個新版本,具有不同的文件夾結構。檢查一下 – Alex 2013-04-23 09:14:34

0

用密鑰發送鏈接到用戶電子郵件,密鑰必須是唯一的,例如散列o將他們的電子郵件地址與您的應用程序密鑰一起作爲鹽。有一個端點,該鏈接指向使用上述過程檢查鏈接是否有效的端點。如果您生成的密鑰與電子郵件鏈接中的密鑰相匹配,您可以驗證其帳戶並將其重定向到登錄頁面。

5
if (Auth::attempt(array('email' => $email, 'password' => $password, 'active' => 1))) 
{ 
    // The user is active, not suspended, and exists. 
} 

Authenticating Users

在Laravel 5.3,你可能要覆蓋Illuminate\Foundation\Auth\AuthenticatesUserscredentials方法在App \ HTTP \ \控制器驗證\ LoginController中添加'active' => 1

+0

你能告訴我在Laravel 5.3中添加這個的位置嗎? – 4ndro1d 2016-10-10 11:01:54

+0

鏈接被破壞,這是L 5.3鏈接:https://laravel.com/docs/5.3/authentication#authentication-quickstart – 2016-10-12 13:10:51