使用pin将用户登录到laravel

时间:2016-05-12 19:34:01

标签: php mysql laravel authentication login

我有一个非常简单的记分板应用程序,我在其中通过链接向用户发送电子邮件。现在看起来像是http://localhost:8888/users/evqGeyCgo4tIG2C。这里/users/之后的字符串是一个生成并通过电子邮件发送给用户的唯一引脚。

因此用户只需点击链接即可在后台我尝试检索用户的电子邮件和密码并尝试登录。但遗憾的是,我无法使用存储在数据库中的哈希密码来登录。

这是我的代码:

方式1:

$user = User::select('email','password')->where('pin',$pin)->first();

        if (Auth::login($user)) {
            // Authentication passed...
            return redirect()->intended('/user');
        } 

方式2:

$user = User::select('email','password')->where('pin',$pin)->first();

        if (Auth::attempt(['email' => $user->email, 'password' => $user->password])) {
            // Authentication passed...
            return redirect()->intended('/user');
        }

不幸的是,由于我无法使用哈希密码,因此第二种方法失败了。第一种方式我从这里看,但我没有看到重定向触发。从这里:https://laravel.com/docs/4.2/security#manually

我不介意任何其他方式/方法让用户使用pin登录。如果任何一个用户看到其他用户个人资料,我不介意。

所以你可能会认为在url中传递引脚也可以被其他用户用来登录。但我并不担心。它只是用户看到的游戏分数。

2 个答案:

答案 0 :(得分:2)

我认为您需要的是按用户ID登录用户:

filter = "[data-completed!=true]";

答案 1 :(得分:0)

你非常接近,我会做这样的事情

// based on the fact that you have a Request object
$user = User::where('email', $request->input('email'))
        ->where('password', \Hash::make($request->input('password')))
        ->where('pin', $request->input('pin'))
        ->first();

if(! $user) {
    // you do not have a user that was found, so return some error
    return redirect()->back()->withInput()->withErrors('Invalid user');
}

// since everything is good, just log the user in
Auth::login($user);

// redirect
return redirect()->route('whatever');