Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit 9c709b8

Browse files
committed
💡 updated comments
1 parent b05f7c3 commit 9c709b8

File tree

5 files changed

+26
-34
lines changed

5 files changed

+26
-34
lines changed

App/Controllers/Auth/AccountController.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,12 @@ public function update()
2020
{
2121
$userId = Auth::id();
2222

23-
$data = request(["username", "email", "name"]);
24-
$dataKeys = array_keys($data);
25-
23+
// request::try attempts to retrieve the data passed into it
24+
// but unsets the data key if no value is found.
25+
$data = request()->try(["username", "email", "name"]);
2626
$where = ["id" => $userId];
27-
2827
$uniques = ["username", "email"];
2928

30-
// This part simply removes empty fields from request
31-
foreach ($dataKeys as $key) {
32-
if (!$data[$key]) {
33-
unset($data[$key]);
34-
continue;
35-
}
36-
37-
if (!strlen($data[$key])) {
38-
unset($data[$key]);
39-
}
40-
}
41-
4229
// This section removes all uniques not found in request
4330
foreach ($uniques as $key => $unique) {
4431
if (!isset($data[$unique])) {

App/Controllers/Auth/LoginController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ public function index()
1616
// your own form rules
1717
Form::rule("max", function ($field, $value, $params) {
1818
if (strlen($value) > $params) {
19+
// add error sets the error which will display when this rule fails
1920
Form::addError($field, "$field can't be more than $params characters");
21+
// a rule must return false when it's validation fails
2022
return false;
2123
}
2224
});
2325

2426
// You can also pass in custom parameters into your
2527
// form rules. The example below calls the max rule defined
26-
// above, and replaces the $params variable with 10.
28+
// above, and replaces the $params variable with 15.
2729
$validation = Form::validate([
2830
// To pass a param to a rule, just use :
2931
"username" => "max:15",

App/Controllers/Auth/RegisterController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class RegisterController extends Controller
99
{
10+
// read the login controller to understand what each feature does.
1011
public function store()
1112
{
1213
$credentials = request(["username", "email", "password"]);

App/Controllers/Controller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function __construct()
1717
parent::__construct();
1818

1919
// In this version, request isn't initialised for you. You can use
20-
// requestData() or request() to get request data or initialise it yourself
20+
// request() to get request data or initialise it yourself
2121

2222
// autoConnect uses the .env variables to quickly connect to db
2323
Auth::autoConnect();
@@ -28,6 +28,8 @@ public function __construct()
2828
// You can configure auth to get additional customizations
2929
// This can be done here with the Auth::config method or
3030
// simply in the Config/auth.php file
31+
32+
// AuthConfig loads up the configuration done in Config/auth.php
3133
Auth::config(AuthConfig());
3234

3335
// You can refer to https://leafphp.netlify.app/#/leaf/v/2.5/core/auth for auth docs

App/Controllers/UsersController.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// This is our model, we import it here to use it below
55
use App\Models\User;
6+
use Leaf\Auth;
67
use 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

Comments
 (0)