11<?php
22defined ('BASEPATH ' ) OR exit ('No direct script access allowed ' );
33use Model \User ;
4+ use Model \PasswordReset ;
5+
46
57class Authentication extends CI_Controller {
68 public function index (){
@@ -9,7 +11,20 @@ public function index(){
911 redirect (route ('admin.dashboard ' ));
1012 }
1113
12- $ this ->load ->view ('admin/auth/login ' );
14+ View::load ('admin/auth/login ' ,[],'auth ' );
15+ }
16+
17+ public function forget_form (){
18+ if (Auth::check ()){ redirect (route ('admin.dashboard ' )); }
19+
20+ View::load ('admin/auth/forgot-email ' ,[],'auth ' );
21+ }
22+
23+ public function reset_password_form ($ token ){
24+ if (Auth::check ()){ redirect (route ('admin.dashboard ' )); }
25+
26+ $ data ['token ' ] = $ token ;
27+ View::load ('admin/auth/reset-password ' ,$ data ,'auth ' );
1328 }
1429
1530 public function logout (){
@@ -35,7 +50,7 @@ public function check_login(){
3550 } else if (!$ user ->status ){
3651 $ json ['errors ' ]['email ' ] = 'User must be active for login ' ;
3752 } else if (!bcrypt_check ($ data ['password ' ], $ user ->password )){
38- $ json ['errors ' ]['email ' ] = 'User must be active for login ' ;
53+ $ json ['errors ' ]['email ' ] = 'Invalid Email Address or Password ' ;
3954 } else {
4055 $ this ->session ->set_userdata ('login_admin ' , $ user ->id );
4156 $ json ['redirect ' ] = route ("admin.dashboard " );
@@ -44,4 +59,98 @@ public function check_login(){
4459
4560 View::json ($ json );
4661 }
62+
63+ public function forget_form_check (){
64+ $ json = array ();
65+
66+ $ this ->form_validation ->set_rules ('email ' , 'Email Address ' , 'required ' );
67+ $ data = $ this ->input ->post (NULL ,true );
68+ if ($ this ->form_validation ->run () == FALSE ){
69+ $ json ['errors ' ] = $ this ->form_validation ->error_array ();
70+ }
71+
72+ if (!isset ($ json ['errors ' ])) {
73+ $ user = User::where ("email " ,"like " ,$ data ['email ' ])->first ();
74+ if (!$ user ){
75+ $ json ['errors ' ]['email ' ] = 'Invalid Email Address or Password ' ;
76+ } else if (!$ user ->status ){
77+ $ json ['errors ' ]['email ' ] = 'User must be active for reset password ' ;
78+ } else {
79+ PasswordReset::where ('email ' ,'like ' ,$ this ->input ->post ('email ' ) )->delete ();
80+
81+ $ newToken = new PasswordReset ();
82+ $ newToken ->token = token (20 );
83+ $ newToken ->email = $ this ->input ->post ('email ' );
84+ $ newToken ->save ();
85+
86+ $ this ->load ->config ('email ' );
87+ $ this ->load ->library ('email ' );
88+
89+ $ from = $ this ->config ->item ('smtp_user ' );
90+ $ to = $ this ->input ->post ('email ' );
91+ $ subject = 'Reset Password Notification ' ;
92+ $ message = "<p>Hello!</p><br>
93+ <p>You are receiving this email because we received a password reset request for your account.</p>
94+
95+ <a href=' " . route ('admin.reset_password_form ' ,['token ' => $ newToken ->token ]) ."'>Reset Password</a>
96+ <p>This password reset link will expire in 60 minutes.</p>
97+
98+ <p>If you did not request a password reset, no further action is required.</p>
99+
100+ <br>
101+ <b>Thanks</b>
102+ " ;
103+
104+ $ this ->email ->set_newline ("\r\n" );
105+ $ this ->email ->from ($ from );
106+ $ this ->email ->to ($ to );
107+ $ this ->email ->subject ($ subject );
108+ $ this ->email ->message ($ message );
109+
110+
111+ if ($ this ->email ->send ()) {
112+ set_message ('success ' , 'An email has been sent to your email address. Please check its inbox to continue reseting password. ' );
113+ $ json ['redirect ' ] = route ('admin.forget_form ' );
114+ } else {
115+ show_error ($ this ->email ->print_debugger ());
116+ }
117+ }
118+ }
119+
120+ View::json ($ json );
121+ }
122+
123+ public function reset_password_check (){
124+ $ json = array ();
125+
126+ $ this ->form_validation ->set_rules ('password ' , 'Password ' , 'required ' );
127+ $ this ->form_validation ->set_rules ('token ' , 'token ' , 'required ' );
128+ $ this ->form_validation ->set_rules ('c_password ' , 'Confirm Password ' , 'required|matches[password] ' );
129+
130+ $ data = $ this ->input ->post (NULL ,true );
131+ if ($ this ->form_validation ->run () == FALSE ){
132+ $ json ['errors ' ] = $ this ->form_validation ->error_array ();
133+ }
134+
135+ if (!isset ($ json ['errors ' ])) {
136+ $ token = PasswordReset::where ('token ' ,$ data ['token ' ])->first ();
137+ if (!$ token ){
138+ $ json ['errors ' ]['password ' ] = 'Invalid token.. ' ;
139+ } else {
140+ $ user = User::where ('email ' ,$ token ->email )->first ();
141+ if (!$ user ){
142+ $ json ['errors ' ]['password ' ] = 'Invalid token.. ' ;
143+ } else {
144+ $ user ->password = bcrypt_hash ($ data ['password ' ]);
145+ $ user ->save ();
146+
147+ set_message ('success ' , 'Password reset successfully ' );
148+ PasswordReset::where ('email ' ,'like ' ,$ user ->email )->delete ();
149+ $ json ['redirect ' ] = route ('admin.login ' );
150+ }
151+ }
152+ }
153+
154+ View::json ($ json );
155+ }
47156}
0 commit comments