Created
September 26, 2016 06:45
-
-
Save mercuryseries/b21885fe1e951ec96ac93a475d723e8f to your computer and use it in GitHub Desktop.
Laravel Basic Authentication Trait
This file contains hidden or 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
| <?php | |
| namespace App\Traits; | |
| use Illuminate\Support\Facades\Hash; | |
| use Illuminate\Support\Facades\Validator; | |
| trait HasSecurePassword | |
| { | |
| /** | |
| * Validation error messages with fields as keys. | |
| * | |
| * @var array | |
| */ | |
| protected $errors; | |
| /** | |
| * Validation error messages. | |
| * | |
| * @var array | |
| */ | |
| protected $error_messages; | |
| /** | |
| * User's password virtual attribute | |
| * | |
| * @var array | |
| */ | |
| protected $password; | |
| /** | |
| * User's password confirmation virtual attribute | |
| * | |
| * @var array | |
| */ | |
| protected $password_confirmation; | |
| /** | |
| * Get all validation error messages with fields as keys. | |
| * | |
| * @return array | |
| */ | |
| public function getErrors() | |
| { | |
| return $this->errors; | |
| } | |
| /** | |
| * Get all validation error messages. | |
| * | |
| * @return array | |
| */ | |
| public function getErrorMessages() | |
| { | |
| return $this->error_messages; | |
| } | |
| /** | |
| * Get the user's password. | |
| * | |
| * @return string | |
| */ | |
| protected function getPasswordAttribute() | |
| { | |
| return $this->password; | |
| } | |
| /** | |
| * Get the user's password confirmation. | |
| * | |
| * @return string | |
| */ | |
| protected function getPasswordConfirmationAttribute() | |
| { | |
| return $this->password_confirmation; | |
| } | |
| /** | |
| * Set the user's password. | |
| * | |
| * @param string | |
| */ | |
| protected function setPasswordAttribute($value) | |
| { | |
| $this->password = $value; | |
| $this->attributes['password_digest'] = Hash::make($value); | |
| } | |
| /** | |
| * Set the user's password confirmation. | |
| * | |
| * @param string | |
| */ | |
| protected function setPasswordConfirmationAttribute($value) | |
| { | |
| $this->password_confirmation = $value; | |
| } | |
| /** | |
| * Authenticate an user. | |
| * | |
| * @param string $password | |
| * @return boolean | |
| */ | |
| public function authenticate($password) | |
| { | |
| if(Hash::check($password, $this->password_digest)) { | |
| return $this; | |
| } | |
| return false; | |
| } | |
| /** | |
| * Check if the user's current state is valid. | |
| * | |
| * @return boolean | |
| */ | |
| public function isValid() | |
| { | |
| $fillable_attributes = collect($this->attributes)->only($this->fillable)->toArray(); | |
| $data = array_merge(['password' => $this->password, 'password_confirmation' => $this->password_confirmation], $fillable_attributes); | |
| $validator = Validator::make($data, $this->rules); | |
| $errors = $validator->errors(); | |
| $this->errors = $errors->toArray(); | |
| $this->error_messages = $errors->all(); | |
| return $validator->passes(); | |
| } | |
| /** | |
| * Bootstrap our trait. | |
| * | |
| * @return void | |
| */ | |
| public static function bootHasSecurePassword() | |
| { | |
| static::saving(function($user) { | |
| return $user->isValid(); | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment