1111 * This is a demo users controller put together to give
1212 * you an idea on basic features of leaf. Each block is commented
1313 * to help you understand exactly what's going on.
14- *
14+ *
1515 * Some blocks can be used as alternatives depending on your preference,
1616 * you can switch to those as you see fit.
17- *
17+ *
1818 * Although a demo, it's a real controller and works correctly as is.
1919 * You can continue your project like this or edit it to match your app.
2020 */
@@ -34,8 +34,8 @@ public function login()
3434 // If you want to, you can perform some operation on the request object
3535 // $password = request()->get("password");
3636
37- // You can also mass assign particular fields from the request
38- list ($ username , $ password ) = requestData (["username " , "password " ], true , true );
37+ // You can also mass assign particular fields from the request
38+ list ($ username , $ password ) = request ()-> get (["username " , "password " ], true , true );
3939
4040 // You can perform operations on your model like this
4141 $ user = User::where ("username " , $ username )->first ();
@@ -51,11 +51,11 @@ public function login()
5151 // password encoding has been configured in the base controller
5252
5353 // This line catches any errors that MAY happen
54- if (!$ user ) throwErr ($ this ->auth ->errors ());
54+ if (!$ user ) request ()-> throwErr ($ this ->auth ->errors ());
5555
5656 // json is another global shortcut method
5757 // it's shorter than $this->json()
58- json ($ user );
58+ response ($ user );
5959 }
6060
6161 public function register ()
@@ -65,7 +65,7 @@ public function register()
6565 // $password = requestData("password");
6666
6767 // You can also directly pick vars from the request object
68- $ credentials = requestData (["username " , "email " , "password " ]);
68+ $ credentials = request (["username " , "email " , "password " ]);
6969
7070 // You can validate your data with Leaf Form Validation
7171 $ validation = $ this ->form ->validate ([
@@ -75,7 +75,7 @@ public function register()
7575 ]);
7676
7777 // Throws an error if there's an issue in validation
78- if (!$ validation ) throwErr ($ this ->form ->errors ());
78+ if (!$ validation ) request ()-> throwErr ($ this ->form ->errors ());
7979
8080 // Direct registration with Leaf Auth. Registers and initiates a
8181 // login, so you don't have to call login again, unless you want
@@ -86,17 +86,17 @@ public function register()
8686 ]);
8787
8888 // throw an auth error if there's an issue
89- if (!$ user ) throwErr ($ this ->auth ->errors ());
89+ if (!$ user ) request ()-> throwErr ($ this ->auth ->errors ());
9090
91- json ($ user );
91+ response ($ user );
9292 }
9393
9494 public function recover_account ()
9595 {
9696 $ username = request ("email " );
9797
9898 $ user = User::where ("email " , $ username )->first () ?? null ;
99- if (!$ user ) throwErr (["email " => "Email not found " ]);
99+ if (!$ user ) response ()-> throwErr (["email " => "Email not found " ]);
100100
101101 // Set a temporary random password and reset user password
102102 $ newPassword = rand (00000000 , 99999999 );
@@ -115,30 +115,30 @@ public function recover_account()
115115 "sender_name " => "API Name " ,
116116 ]);
117117
118- json (["message " => "ok " ]);
118+ response ()-> json (["message " => "ok " ]);
119119 }
120120
121121 public function reset_password ()
122122 {
123123 // id retrieves the JWT from the headers, decodes it and returns
124124 // the user encoded into the token. If there's a problem with the token,
125125 // we can throw whatever error occurs. This means the user must be logged in.
126- $ userId = $ this ->auth ->id () ?? throwErr ($ this ->auth ->errors ());
126+ $ userId = $ this ->auth ->id () ?? response ()-> throwErr ($ this ->auth ->errors ());
127127 $ password = request ("password " );
128128
129- // Get the
129+ // Get the
130130 $ user = User::find ($ userId );
131- if (!$ user ) throwErr (["user " => "User not found! Check somewhere... " ]);
131+ if (!$ user ) response ()-> throwErr (["user " => "User not found! Check somewhere... " ]);
132132
133133 // Change the user password
134134 $ user ->password = md5 ($ password );
135135 $ user ->save ();
136136
137137 // login again to get new token
138138 $ user = $ this ->auth ->login ("users " , ["id " => $ userId ]);
139- if (!$ user ) throwErr ($ this ->auth ->errors ());
139+ if (!$ user ) response ()-> throwErr ($ this ->auth ->errors ());
140140
141- json ($ user );
141+ response ()-> json ($ user );
142142 }
143143
144144 public function user () {
@@ -149,13 +149,13 @@ public function user() {
149149 // $auth->user() is new in v2.4 of leaf
150150 $ user = $ this ->auth ->user ("users " , $ hidden );
151151
152- json ($ user ?? throwErr ($ this ->auth ->errors ()));
152+ response ()-> json ($ user ?? response ()-> throwErr ($ this ->auth ->errors ()));
153153 }
154154
155155 public function edit ()
156156 {
157157 // auth->id returns the user id encoded into jwt by default
158- $ userId = $ this ->auth ->id () ?? throwErr ($ this ->auth ->errors ());
158+ $ userId = $ this ->auth ->id () ?? response ()-> throwErr ($ this ->auth ->errors ());
159159
160160 // data to update
161161 $ data = request (["username " , "email " , "password " ]);
@@ -168,6 +168,6 @@ public function edit()
168168
169169 $ user = $ this ->auth ->update ("users " , $ data , $ where , $ uniques );
170170
171- json ($ user ?? throwErr ($ this ->auth ->errors ()));
171+ response ()-> json ($ user ?? response ()-> throwErr ($ this ->auth ->errors ()));
172172 }
173173}
0 commit comments