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

Commit 8aa34db

Browse files
committed
🎨 Tidied up base controller
1 parent b207b71 commit 8aa34db

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

App/Controllers/UsersController.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33

44
// This is our model, we import it here to use it below
55
use App\Models\User;
6+
use Leaf\Helpers\Password;
67

78
/**
89
* UsersController (Demo)
910
* ---------------
1011
* This is a demo users controller put together to give
11-
* you an idea on basic features of leaf
12+
* you an idea on basic features of leaf. Each block is commented
13+
* to help you understand exactly what's going on.
14+
*
15+
* Some blocks can be used as alternatives depending on your preference,
16+
* you can switch to those as you see fit.
17+
*
18+
* Although a demo, it's a real controller and works correctly as is.
19+
* You can continue your project like this or edit it to match your app.
1220
*/
1321
class UsersController extends Controller
1422
{
@@ -19,7 +27,12 @@ public function login()
1927
// requestData is a shortcut method which allows
2028
// you get data passed into a request by key name
2129
// $username = requestData("username");
22-
// $password = requestData("password");
30+
31+
// From v2, you can also use request()
32+
// You can directly get parameters like this:
33+
// $password = request("password");
34+
// If you want to, you can perform some operation on the request object
35+
// $password = request()->get("password");
2336

2437
// You can also mass assign particular fields from the request
2538
list($username, $password) = requestData(["username", "password"], true, true);
@@ -80,14 +93,16 @@ public function register()
8093

8194
public function recover_account()
8295
{
83-
$username = requestData("email");
96+
$username = request("email");
8497

8598
$user = User::where("email", $username)->first() ?? null;
8699
if (!$user) throwErr(["email" => "Email not found"]);
87100

88101
// Set a temporary random password and reset user password
89102
$newPassword = rand(00000000, 99999999);
90-
$user->password = md5($newPassword);
103+
104+
// hash new password (uses leaf password helper)
105+
$user->password = Password::hash($newPassword);
91106
$user->save();
92107

93108
// Send an email to user with the new temporary password
@@ -109,7 +124,7 @@ public function reset_password()
109124
// the user encoded into the token. If there's a problem with the token,
110125
// we can throw whatever error occurs. This means the user must be logged in.
111126
$userId = $this->auth->id() ?? throwErr($this->auth->errors());
112-
$password = requestData("password");
127+
$password = request("password");
113128

114129
// Get the
115130
$user = User::find($userId);
@@ -143,13 +158,13 @@ public function edit()
143158
$userId = $this->auth->id() ?? throwErr($this->auth->errors());
144159

145160
// data to update
146-
$data = requestData(["username", "email", "password"]);
161+
$data = request(["username", "email", "password"]);
147162

148163
// data to find user by
149164
$where = ["id" => $userId];
150165

151166
// params which shouldn't already exist in db
152-
$uniques = ["username"];
167+
$uniques = ["username", "email"];
153168

154169
$user = $this->auth->update("users", $data, $where, $uniques);
155170

0 commit comments

Comments
 (0)