gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:17:08 25 4
gpt4 key购买 nike

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

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

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

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

Mail::send('users.welcomemail', [], function ($message) {
$message->from('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3b6beb2babf93b6abb2bea3bfb6fdb0bcbe" rel="noreferrer noopener nofollow">[email protected]</a>', 'Email');
$message->to('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53363e323a3f13362b323e233f367d303c3e" rel="noreferrer noopener nofollow">[email protected]</a>', 'Email')->subject('Welcome!');
});

return redirect('business/');
}

我已经为设置密码创建了一个新 View (从views/auth/reset.blade.php复制)。

我只是不确定应该向 Controller 写入什么(用于设置密码),因此它的作用类似于重置密码功能。任何想法都会有帮助。

如果可能的话,我想使用laravel中已有的内容..默认用户表和 Controller ,以及password_resets表。

更新 - 解决方案

我已经成功让它发挥作用了。

用户 Controller

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('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="751b141810351018141c195b161a18" rel="noreferrer noopener nofollow">[email protected]</a>', 'My name');
$message->to($contactemail, $contactfirstname)->subject('Welcome!');
});

return redirect('business/');
}

users.welcomemail View

<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');

密码设置 Controller

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/{token} View

@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

最佳答案

当您使用新电子邮件地址创建新帐户时,您还需要在数据库中为该电子邮件地址输入一个唯一值,例如,您可以使用email在数据库中创建一条记录> 和 token 字段,因此在创建记录和存储 email 时也会存储 token (唯一)。要获得唯一的 token ,您可以尝试以下操作:

$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');

现在,在 Controller 中声明方法,例如:

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

关于laravel - 在 Laravel 5 中设置设置密码(类似于重置密码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33001102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com