33
44// This is our model, we import it here to use it below
55use App \Models \User ;
6+ use Leaf \Auth ;
67use Leaf \Helpers \Password ;
78
89/**
@@ -36,21 +37,20 @@ public function login()
3637 // You can perform operations on your model like this
3738 $ user = User::where ("username " , $ username )->first ();
3839
39- // auth is initialised in the base controller
40+ // auth settings are passed in the base controller
4041 // login allows us to sign a user in, and also generates
4142 // a jwt automatically
42- $ user = $ this -> auth -> login ("users " , [
43+ $ user = Auth:: login ("users " , [
4344 "username " => $ username ,
4445 "password " => $ password
4546 ]);
4647
4748 // password encoding has been configured in the base controller
4849
4950 // This line catches any errors that MAY happen
50- if (!$ user ) response ()->throwErr ($ this -> auth -> errors ());
51+ if (!$ user ) response ()->throwErr (Auth:: errors ());
5152
52- // json is another global shortcut method
53- // it's shorter than $this->json()
53+ // output json data or return the response object
5454 response ($ user );
5555 }
5656
@@ -65,7 +65,7 @@ public function register()
6565
6666 // You can validate your data with Leaf Form Validation
6767 $ validation = $ this ->form ->validate ([
68- "username " => "validUsername " ,
68+ "username " => [ "validUsername " , " max:10 " ] ,
6969 "email " => "email " ,
7070 "password " => "required "
7171 ]);
@@ -77,12 +77,12 @@ public function register()
7777 // login, so you don't have to call login again, unless you want
7878 // to. The 3rd parameter makes sure that the same username
7979 // and email can't be registered multiple times
80- $ user = $ this -> auth -> register ("users " , $ credentials , [
80+ $ user = Auth:: register ("users " , $ credentials , [
8181 "username " , "email "
8282 ]);
8383
8484 // throw an auth error if there's an issue
85- if (!$ user ) response ()->throwErr ($ this -> auth -> errors ());
85+ if (!$ user ) response ()->throwErr (Auth:: errors ());
8686
8787 response ($ user );
8888 }
@@ -119,7 +119,7 @@ public function reset_password()
119119 // id retrieves the JWT from the headers, decodes it and returns
120120 // the user encoded into the token. If there's a problem with the token,
121121 // we can throw whatever error occurs. This means the user must be logged in.
122- $ userId = $ this -> auth -> id () ?? response ()->throwErr ($ this -> auth -> errors ());
122+ $ userId = Auth:: id () ?? response ()->throwErr (Auth:: errors ());
123123 $ password = request ("password " );
124124
125125 // Get the
@@ -131,8 +131,8 @@ public function reset_password()
131131 $ user ->save ();
132132
133133 // login again to get new token
134- $ user = $ this -> auth -> login ("users " , ["id " => $ userId ]);
135- if (!$ user ) response ()->throwErr ($ this -> auth -> errors ());
134+ $ user = Auth:: login ("users " , ["id " => $ userId ]);
135+ if (!$ user ) response ()->throwErr (Auth:: errors ());
136136
137137 response ()->json ($ user );
138138 }
@@ -143,15 +143,15 @@ public function user() {
143143
144144 // Make sure user is logged in
145145 // $auth->user() is new in v2.4 of leaf
146- $ user = $ this -> auth -> user ("users " , $ hidden );
146+ $ user = Auth:: user ("users " , $ hidden );
147147
148- response ()->json ($ user ?? response ()->throwErr ($ this -> auth -> errors ()));
148+ response ()->json ($ user ?? response ()->throwErr (Auth:: errors ()));
149149 }
150150
151151 public function edit ()
152152 {
153153 // auth->id returns the user id encoded into jwt by default
154- $ userId = $ this -> auth -> id () ?? response ()->throwErr ($ this -> auth -> errors ());
154+ $ userId = Auth:: id () ?? response ()->throwErr (Auth:: errors ());
155155
156156 // data to update
157157 $ data = request (["username " , "email " , "password " ]);
@@ -162,8 +162,8 @@ public function edit()
162162 // params which shouldn't already exist in db
163163 $ uniques = ["username " , "email " ];
164164
165- $ user = $ this -> auth -> update ("users " , $ data , $ where , $ uniques );
165+ $ user = Auth:: update ("users " , $ data , $ where , $ uniques );
166166
167- response ()->json ($ user ?? response ()->throwErr ($ this -> auth -> errors ()));
167+ response ()->json ($ user ?? response ()->throwErr (Auth:: errors ()));
168168 }
169169}
0 commit comments