英文:
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('/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
{
#reset password
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()]);
}
}
#reset password view load
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');
}
}
#reset password func
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()]);
}
// PasswordReset::where('email',$user->email)->delete();
}
}
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'
];
}
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 = [
'email',
'token',
'created_at',
'password'
];
Also, maybe you don't need to put the created_at variable inside de array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论