密码更改API与Laravel

huangapple go评论64阅读模式
英文:

password change api with laravel

问题

这是你提供的代码的翻译部分:

Api.php

Route::post('/forgetPassword',[UserController::class,'forgetPassword']);

Web.php

Route::get('/resetPassword',[UserController::class,'forgetPasswordLoad']);
Route::post('/resetPassword',[UserController::class,'resetPassword']);

AuthController.php

<?php

namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Models\PasswordReset;
use Mail;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    # 重置密码
    public function forgetPassword(Request $request)
    {
        try {
            $user = User::where('email',$request->email)->get();

            if (count($user)>0) {
                $token = Str::random(40);
                $domain = URL::to('/');
                $url = $domain.'/resetPassword?token='.$token;

                $data['url']=$url;
                $data['email']=$request->email;
                $data['title']="Password Reset";
                $data['body']="Please click on below link to reset password";

                Mail::send('API/forgetPasswordMail',['data'=>$data],function($message) use ($data){
                    $message->to($data['email'])->subject($data['title']);
                });

                $datetime = Carbon::now()->format('Y-m-d H:i:s');
                PasswordReset::updateOrCreate(
                ['email'=>$request->email],
                [
                    'email' => $request->email,
                    'token' => $token,
                    'created_at' =>$datetime
                ]
                );

                return response()->json(['success'=>true,'message'=>'Şifrenizi değiştirmek için lütfen mailinizi kontrol ediniz!']);

            }else {
                return response()->json(['success'=>false,'message'=>'Kullanıcı Bulunamadı']);
            }

        } catch (\Exception $e) {
            return response()->json(['success'=>false,'message'=>$e->getMessage()]);
        }
    }

    # 重置密码视图加载
    public function forgetPasswordLoad(Request $request) 
    {
        $resetData = PasswordReset::where('token',$request->token)->get();
        if (count($resetData)>0) {

            $user = User::where('email',$resetData[0]['email'])->get();
            return view('API/resetPassword',compact('user'));

        }else {
            return view('API/404');
        }
    }

    # 重置密码函数
    public function resetPassword(Request $request)
    {
        try {
            $request->validate([
                'password' => 'required|string|min:6|confirmed'
            ]);

            $user = User::find($request->id);
            $user->password =$request->password;
            $user->save();
            return "<h1>Şifreniz Başarıyla Değiştirildi.Mobil Uygulamaya Dönebilirsiniz</h1>";
        }  catch (\Exception $e) {
            return response()->json(['success'=>false,'message'=>$e->getMessage()]);
        }
    }
}

resetPassword.blade.php

<h1>Proxima Doktor</h1>
<form method="POST">
    @csrf
    <h3>Password Reset</h3>
    <p>Lütfen güçlü bir parola giriniz</p>
    <p>Password</p>
    <input type="hidden" name="id" value="{{$user[0]['id']}}">
    <input type="password" name="password">
    <i class="fa fa-eye-slash"></i>
    <p>Password Again</p>
    <input type="password" name="password_confirmation">
    <i class="fa fa-eye-slash"></i>
    <br><br>
    <input type="submit">
</form>

PasswordReset.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PasswordReset extends Model
{
    use HasFactory;

    public $table = 'password_reset_tokens';
    public $timestamps = false;
    protected $primaryKey = 'email';

    protected $fillable = [
        'email',
        'token',
        'created_at'
    ];
}

希望这可以帮助你解决问题。

英文:

There is no error in the codes I wrote, but the password of the user who receives the mail does not change. The password that changes is only the password of the first user in the users table. No matter what I tried, I could not succeed.

Api.php

Route::post(&#39;/forgetPassword&#39;,[UserController::class,&#39;forgetPassword&#39;]);

Web.php

Route::get(&#39;/resetPassword&#39;,[UserController::class,&#39;forgetPasswordLoad&#39;]);
Route::post(&#39;/resetPassword&#39;,[UserController::class,&#39;resetPassword&#39;]);

AuthController.php

&lt;?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use App\Models\PasswordReset;
use Mail;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
#reset password
public function forgetPassword(Request $request)
{
try {
$user = User::where(&#39;email&#39;,$request-&gt;email)-&gt;get();
if (count($user)&gt;0) {
$token = Str::random(40);
$domain = URL::to(&#39;/&#39;);
$url = $domain.&#39;/resetPassword?token=&#39;.$token;
$data[&#39;url&#39;]=$url;
$data[&#39;email&#39;]=$request-&gt;email;
$data[&#39;title&#39;]=&quot;Password Reset&quot;;
$data[&#39;body&#39;]=&quot;Please click on below link to reset password&quot;;
Mail::send(&#39;API/forgetPasswordMail&#39;,[&#39;data&#39;=&gt;$data],function($message) use ($data){
$message-&gt;to($data[&#39;email&#39;])-&gt;subject($data[&#39;title&#39;]);
});
$datetime = Carbon::now()-&gt;format(&#39;Y-m-d H:i:s&#39;);
PasswordReset::updateOrCreate(
[&#39;email&#39;=&gt;$request-&gt;email],
[
&#39;email&#39; =&gt; $request-&gt;email,
&#39;token&#39; =&gt; $token,
&#39;created_at&#39; =&gt;$datetime
]
);
return response()-&gt;json([&#39;success&#39;=&gt;true,&#39;message&#39;=&gt;&#39;Şifrenizi değiştirmek i&#231;in l&#252;tfen mailinizi kontrol ediniz!&#39;]);
}else {
return response()-&gt;json([&#39;success&#39;=&gt;false,&#39;message&#39;=&gt;&#39;Kullanıcı Bulunamadı&#39;]);
}
} catch (\Exception $e) {
return response()-&gt;json([&#39;success&#39;=&gt;false,&#39;message&#39;=&gt;$e-&gt;getMessage()]);
}
}
#reset password view load
public function forgetPasswordLoad(Request $request) 
{
$resetData = PasswordReset::where(&#39;token&#39;,$request-&gt;token)-&gt;get();
if (count($resetData)&gt;0) {
$user = User::where(&#39;email&#39;,$resetData[0][&#39;email&#39;])-&gt;get();
return view(&#39;API/resetPassword&#39;,compact(&#39;user&#39;));
}else {
return view(&#39;API/404&#39;);
}
}
#reset password func
public function resetPassword(Request $request)
{
try {
$request-&gt;validate([
&#39;password&#39; =&gt; &#39;required|string|min:6|confirmed&#39;
]);
$user = User::find($request-&gt;id);
$user-&gt;password =$request-&gt;password;
$user-&gt;save();
return &quot;&lt;h1&gt;Şifreniz Başarıyla Değiştirildi.Mobil Uygulamaya D&#246;nebilirsiniz&lt;/h1&gt;&quot;;
}  catch (\Exception $e) {
return response()-&gt;json([&#39;success&#39;=&gt;false,&#39;message&#39;=&gt;$e-&gt;getMessage()]);
}
// PasswordReset::where(&#39;email&#39;,$user-&gt;email)-&gt;delete();
}
}

resetPassword.blade.php


&lt;h1&gt;Proxima Doktor&lt;/h1&gt;
&lt;form method=&quot;POST&quot;&gt;
@csrf
&lt;h3&gt;Password Reset&lt;/h3&gt;
&lt;p&gt;L&#252;tfen g&#252;&#231;l&#252; bir parola giriniz&lt;/p&gt;
&lt;p&gt;Password&lt;/p&gt;
&lt;input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;{{$user[0][&#39;id&#39;]}}&quot;&gt;
&lt;input type=&quot;password&quot; name=&quot;password&quot;&gt;
&lt;i class=&quot;fa fa-eye-slash&quot;&gt;&lt;/i&gt;
&lt;p&gt;Password Again&lt;/p&gt;
&lt;input type=&quot;password&quot; name=&quot;password_confirmation&quot;&gt;
&lt;i class=&quot;fa fa-eye-slash&quot;&gt;&lt;/i&gt;
&lt;br&gt;&lt;br&gt;
&lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

PasswordReset.php

&lt;?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PasswordReset extends Model
{
use HasFactory;
public $table = &#39;password_reset_tokens&#39;;
public $timestamps = false;
protected $primaryKey = &#39;email&#39;;
protected $fillable = [
&#39;email&#39;,
&#39;token&#39;,
&#39;created_at&#39;
];
}

I have shared all my related files, thank you if anyone can help.

答案1

得分: 0

在你的代码中,你忽略了一件事,那就是你没有将密码属性放在受保护的$fillable变量内。

尝试这样做:

protected $fillable = [
    'email',
    'token',
    'created_at',
    'password'
];

另外,也许你不需要将created_at变量放在数组内。

英文:

Looking your code in the model, you miss one thing, you aren't putting the password attribute inside the protected $fillable variable.

Try doing this:

protected $fillable = [
&#39;email&#39;,
&#39;token&#39;,
&#39;created_at&#39;,
&#39;password&#39;
];

Also, maybe you don't need to put the created_at variable inside de array.

huangapple
  • 本文由 发表于 2023年3月4日 01:11:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定