Profile.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. class Profile extends Component
  5. {
  6. public $name, $email, $password, $passwordConfirm, $update = false, $editPassword = false, $errorConfirm;
  7. protected $rules = [
  8. 'name' => 'required',
  9. 'email' => 'required'
  10. ];
  11. protected $messages = [
  12. 'name.required' => 'Il nome è obbligatorio',
  13. 'email.required' => 'La mail è obbligatoria',
  14. 'password.required' => 'La password è obbligatoria',
  15. 'passwordConfirm.required' => 'La conferma password è obbligatoria',
  16. ];
  17. public function resetFields(){
  18. //$this->name = '';
  19. //$this->email = '';
  20. $this->errorConfirm = '';
  21. $this->password = '';
  22. $this->newPassword = '';
  23. }
  24. public function mount()
  25. {
  26. $this->name = \Auth::user()->name;
  27. $this->email = \Auth::user()->email;
  28. }
  29. public function render()
  30. {
  31. return view('livewire.profile');
  32. }
  33. public function edit()
  34. {
  35. //$this->resetFields();
  36. $this->update = true;
  37. }
  38. public function editPwd()
  39. {
  40. $this->resetFields();
  41. $this->editPassword = true;
  42. }
  43. public function save()
  44. {
  45. $this->validate();
  46. try {
  47. $user = \Auth::user();
  48. $user->name = $this->name;
  49. $user->email = $this->email;
  50. if ($this->password != '')
  51. {
  52. $user->password = bcrypt($this->password);
  53. }
  54. $user->save();
  55. session()->flash('success','Dato creato');
  56. $this->resetFields();
  57. $this->update = false;
  58. } catch (\Exception $ex) {
  59. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  60. }
  61. }
  62. public function updatePwd()
  63. {
  64. $this->errorConfirm = '';
  65. $this->validate(['password' => 'required', 'passwordConfirm' => 'required']);
  66. try {
  67. $user = \Auth::user();
  68. if ($this->password == $this->passwordConfirm)
  69. {
  70. $user->password = bcrypt($this->password);
  71. $user->save();
  72. session()->flash('success','Dato creato');
  73. $this->resetFields();
  74. $this->update = false;
  75. }
  76. else
  77. {
  78. $this->errorConfirm = 'Le password non coincidono';
  79. }
  80. } catch (\Exception $ex) {
  81. session()->flash('error','Errore (' . $ex->getMessage() . ')');
  82. }
  83. }
  84. public function cancel()
  85. {
  86. $this->resetFields();
  87. $this->update = false;
  88. $this->editPassword = false;
  89. }
  90. }