Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add password reset system, add lang to response messages
- Loading branch information
1 parent
b149321
commit edf7b30
Showing
10 changed files
with
295 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use Carbon\Carbon; | ||
use App\Notifications\PasswordResetRequest; | ||
use App\Notifications\PasswordResetSuccess; | ||
use App\User; | ||
use App\PasswordReset; | ||
|
||
class PasswordResetController extends Controller | ||
{ | ||
/** | ||
* Create token password reset | ||
* | ||
* @param [string] email | ||
* @return [string] message | ||
*/ | ||
public function create(Request $request) | ||
{ | ||
$request->validate([ | ||
'email' => 'required|string|email', | ||
]); | ||
|
||
$user = User::where('email', $request->email)->first(); | ||
|
||
if (!$user) | ||
return response()->json([ | ||
'message' => __('passwords.user') | ||
], 404); | ||
|
||
$passwordReset = PasswordReset::updateOrCreate(['email' => $user->email], [ | ||
'email' => $user->email, | ||
'token' => str_random(60) | ||
]); | ||
|
||
if ($user && $passwordReset) | ||
$user->notify(new PasswordResetRequest($passwordReset->token)); | ||
|
||
return response()->json([ | ||
'message' => __('passwords.sent') | ||
]); | ||
} | ||
|
||
/** | ||
* Find token password reset | ||
* | ||
* @param [string] $token | ||
* @return [string] message | ||
* @return [json] passwordReset object | ||
*/ | ||
public function find($token) | ||
{ | ||
$passwordReset = PasswordReset::where('token', $token)->first(); | ||
|
||
if (!$passwordReset) | ||
return response()->json([ | ||
'message' => __('passwords.token') | ||
], 404); | ||
|
||
if (Carbon::parse($passwordReset->updated_at)->addMinutes(720)->isPast()) { | ||
$passwordReset->delete(); | ||
return response()->json([ | ||
'message' => __('passwords.token') | ||
], 404); | ||
} | ||
|
||
return response()->json($passwordReset); | ||
} | ||
|
||
/** | ||
* Reset password | ||
* | ||
* @param [string] email | ||
* @param [string] password | ||
* @param [string] password_confirmation | ||
* @param [string] token | ||
* @return [string] message | ||
* @return [json] user object | ||
*/ | ||
public function reset(Request $request) | ||
{ | ||
$request->validate([ | ||
'email' => 'required|string|email', | ||
'password' => 'required|string|confirmed', | ||
'token' => 'required|string' | ||
]); | ||
|
||
$passwordReset = PasswordReset::where('token', $request->token)->first(); | ||
|
||
if (!$passwordReset) | ||
return response()->json([ | ||
'message' => __('passwords.token') | ||
], 404); | ||
|
||
$user = User::where('email', $request->email)->first(); | ||
|
||
if (!$user) | ||
return response()->json([ | ||
'message' => __('passwords.user') | ||
], 404); | ||
|
||
$user->password = bcrypt($request->password); | ||
$user->save(); | ||
|
||
$passwordReset->delete(); | ||
|
||
$user->notify(new PasswordResetSuccess($passwordReset)); | ||
|
||
return response()->json($user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
class PasswordResetRequest extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
protected $token; | ||
|
||
/** | ||
* Create a new notification instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct($token) | ||
{ | ||
$this->token = $token; | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
/** | ||
* Get the mail representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return \Illuminate\Notifications\Messages\MailMessage | ||
*/ | ||
public function toMail($notifiable) | ||
{ | ||
$url = url('/api/password/find/'.$this->token); | ||
|
||
return (new MailMessage) | ||
->line('You are receiving this email because we received a password reset request for your account.') | ||
->action('Reset Password', url($url)) | ||
->line('If you did not request a password reset, no further action is required.'); | ||
} | ||
|
||
/** | ||
* Get the array representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function toArray($notifiable) | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Notifications\Notification; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Notifications\Messages\MailMessage; | ||
|
||
class PasswordResetSuccess extends Notification implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Create a new notification instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function via($notifiable) | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
/** | ||
* Get the mail representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return \Illuminate\Notifications\Messages\MailMessage | ||
*/ | ||
public function toMail($notifiable) | ||
{ | ||
return (new MailMessage) | ||
->line('You are receiving this email because changed your password for your account.') | ||
->line('If you did change password, no further action is required.') | ||
->line('If you did not change password, protect your account.'); | ||
} | ||
|
||
/** | ||
* Get the array representation of the notification. | ||
* | ||
* @param mixed $notifiable | ||
* @return array | ||
*/ | ||
public function toArray($notifiable) | ||
{ | ||
return [ | ||
// | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class PasswordReset extends Model | ||
{ | ||
protected $fillable = [ | ||
'email', 'token' | ||
]; | ||
} |
Oops, something went wrong.