首页 文章

在Laravel 5中设置设置密码(类似于重置密码)

提问于
浏览
2

当用户登录时,我已为该用户添加了创建新用户的功能(指定电子邮件地址,无需提供密码) .

完成后,我希望新用户收到一封电子邮件(如带有链接的重置密码电子邮件),将用户发送到“设置密码”页面(类似于“重置密码”视图) .

我想出了如何在商店功能(UserController)中向新用户发送常规电子邮件:

public function store(UsersRequest $request)
{
    $user = User::create(Request::all());

    Mail::send('users.welcomemail', [], function ($message) { 
        $message->from('email@example.com', 'Email');
        $message->to('email@example.com', 'Email')->subject('Welcome!');
    });

    return redirect('business/');
}

我创建了一个设置密码的新视图(从views / auth / reset.blade.php复制) .

我只是不确定我应该写什么(用于设置密码)到我的控制器,所以它就像重置密码功能 . 任何想法都会有帮助 .

如果可能的话,我想使用已经存在于laravel中的内容..默认用户表和控制器,以及password_resets表 .

update - solution

我成功地使它成功了 .

UsersController

public function store(UsersRequest $request)
{
    $user = User::create(Request::all());

    $contactfirstname = $user->first_name;
    $contactemail = $user->email;

    $token = hash_hmac('sha256', str_random(40), config('app.key'));

    DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' =>  \Carbon\Carbon::now()->toDateTimeString()]);

    Mail::send('users.welcomemail', ['user' => $user, 'token' => $token], function ($message) use ($contactfirstname, $contactemail)
    { 
        $message->from('name@email.com', 'My name');
        $message->to($contactemail, $contactfirstname)->subject('Welcome!');
    });

    return redirect('business/');
}

users.welcomemail视图

<h1>Hi! {{ $user->first_name }}</h1>

<p>We'd like to personally welcome you. Thank you for registering!</p>

<p>Please click the link below to set your account password and get access to your account :</p>

<p><a href="{{ URL::to('auth/passwordset/' .  $token) }}">{{ URL::to('auth/passwordset/' .  $token) }}</a></p>

路线

('/auth/passwordset/{token}', 'PasswordSetupController@passwordset');

passwordSetupController

class PasswordsetController extends Controller {

    /*
    |--------------------------------------------------------------------------
    | Passwordset Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles password setups for new users
    |
    */

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    public function passwordset($token)
    {
        return view('users.passwordset')->with(['token' => $token]);
    }
}

passwordset / 视图

@extends('app')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Set Password</div>
                <div class="panel-body">
                    @if (count($errors) > 0)
                        <div class="alert alert-danger">
                            <strong>Whoops!</strong> There were some problems with your input.<br><br>
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form class="form-horizontal" role="form" method="POST" action="{{ url('/password/reset') }}">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="hidden" name="token" value="{{ $token }}">

                        <div class="form-group">
                            <label class="col-md-4 control-label">E-Mail Address</label>
                            <div class="col-md-6">
                                <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Password</label>
                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-md-4 control-label">Confirm Password</label>
                            <div class="col-md-6">
                                <input type="password" class="form-control" name="password_confirmation">
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-6 col-md-offset-4">
                                <button type="submit" class="btn btn-primary">
                                    Set Password
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

1 回答

  • 3

    使用新电子邮件地址创建新帐户时,还需要在数据库中为该电子邮件添加唯一值,例如,您可以使用 emailtoken 字段在数据库中创建记录,以便在创建记录时存储 email 也存储令牌(唯一) . 要获得唯一令牌,您可以尝试这样做:

    $token = hash_hmac('sha256', str_random(40), config('app.key'));
    

    现在将记录保存在数据库中并发送带有链接的电子邮件,链接可能如下所示:

    http://example.com/set/password/the-token-you-created-for-this-user
    

    现在,只需为链接创建路由,例如:

    get('/set/password/{token}', 'PasswordSetupController@getSetPassword');
    

    现在,在控制器中,声明方法,例如:

    public function getSetPassword($token)
    {
        // find the token from the database
        // if you can find a record, for example:
        $model = SomeModel::whereToken($token)->first();
    
        if($model) {
            // The matching $token is found. So show a view to set the password
            // with a form textbox and submit button, set form action, add route
            // for that action. Also, add a hidden field in the form for token,
            // so you can check it again on form submission
        }
    }
    

    创建表单处理方法,例如:

    public function postSetPassword()
    {
        if($token = Input::get('hidden_token_field')) {
            // Match it again and if matches then save the password and delete
            // the hashed record or update the hashed field, just figure it out.
        }
    }
    

    路线可能是:

    post('/set/password', 'PasswordSetupController@postSetPassword');
    

    这是一个抽象的想法,但你应该能够完成所需的工作 . 我告诉你你要的想法 . 希望它能帮到你 . 确保根据上面的路线设置表单方法 POST .

相关问题