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

Commit 9c2fd05

Browse files
committed
👽 updated auth code
1 parent 13de5ed commit 9c2fd05

1 file changed

Lines changed: 58 additions & 60 deletions

File tree

app/controllers/UsersController.php

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

44
// This is our model, we import it here to use it below
55
use App\Models\User;
6-
use Leaf\Auth;
76
use Leaf\Form;
87
use Leaf\Helpers\Password;
98

@@ -27,49 +26,44 @@ class UsersController extends Controller
2726
public function login()
2827
{
2928
// From v2, you can also use request()
30-
// You can directly get parameters like this:
31-
// $password = request("password");
32-
// If you want to, you can perform some operation on the request object
33-
// $password = request()->get("password");
29+
// $password = request()->get('password');
3430

3531
// You can also mass assign particular fields from the request
36-
list($username, $password) = request()->get(["username", "password"], true, true);
32+
$credentials = request()->get(['username', 'password']);
3733

3834
// You can perform operations on your model like this
39-
$user = User::where("username", $username)->first();
35+
$user = User::where('username', $credentials['username'])->first();
4036

4137
// auth is initialised in the base controller
4238
// login allows us to sign a user in, and also generates
4339
// a jwt automatically
44-
$user = Auth::login("users", [
45-
"username" => $username,
46-
"password" => $password
47-
]);
40+
$user = auth()->login($credentials);
4841

4942
// password encoding has been configured in the base controller
5043

5144
// This line catches any errors that MAY happen
52-
if (!$user) response()->throwErr(Auth::errors());
45+
if (!$user) {
46+
response()->throwErr(auth()->errors());
47+
}
5348

54-
// json is another global shortcut method
55-
// it's shorter than $this->json()
56-
response($user);
49+
// We can call json on the response global shortcut method
50+
response()->json($user);
5751
}
5852

5953
public function register()
6054
{
61-
// $username = request("username");
62-
// $email = request("email");
63-
// $password = request("password");
55+
// $username = request()->get('username');
56+
// $email = request()->get('email');
57+
// $password = request()->get('password');
6458

6559
// You can also directly pick vars from the request object
66-
$credentials = request(["username", "email", "password"]);
60+
$credentials = request()->get(['username', 'email', 'password']);
6761

6862
// You can validate your data with Leaf Form Validation
6963
$validation = Form::validate([
70-
"username" => "validUsername",
71-
"email" => "email",
72-
"password" => "required"
64+
'username' => 'validUsername',
65+
'email' => 'email',
66+
'password' => 'required'
7367
]);
7468

7569
// Throws an error if there's an issue in validation
@@ -79,22 +73,26 @@ public function register()
7973
// login, so you don't have to call login again, unless you want
8074
// to. The 3rd parameter makes sure that the same username
8175
// and email can't be registered multiple times
82-
$user = Auth::register("users", $credentials, [
83-
"username", "email"
76+
$user = auth()->register($credentials, [
77+
'username', 'email'
8478
]);
8579

8680
// throw an auth error if there's an issue
87-
if (!$user) response()->throwErr(Auth::errors());
81+
if (!$user) {
82+
response()->throwErr(auth()->errors());
83+
}
8884

89-
response($user);
85+
response()->json($user);
9086
}
9187

9288
public function recover_account()
9389
{
94-
$username = request("email");
90+
$username = request()->get('email');
91+
$user = User::where('email', $username)->first() ?? null;
9592

96-
$user = User::where("email", $username)->first() ?? null;
97-
if (!$user) response()->throwErr(["email" => "Email not found"]);
93+
if (!$user) {
94+
response()->throwErr(['email' => 'Email not found']);
95+
}
9896

9997
// Set a temporary random password and reset user password
10098
$newPassword = rand(00000000, 99999999);
@@ -104,68 +102,68 @@ public function recover_account()
104102
$user->save();
105103

106104
// Send an email to user with the new temporary password
107-
// email() is a global method that allows you to send a
108-
// quick email. Don't forget to configure your .env variables
105+
// You can use any email service of your choice.
106+
109107
// email([
110-
// "subject" => "Your Password has been reset",
111-
// "body" => "This is your new password: $newPassword",
112-
// "recepient_email" => $user->email,
113-
// "sender_name" => "API Name",
108+
// 'subject' => 'Your Password has been reset',
109+
// 'body' => 'This is your new password: $newPassword',
110+
// 'recepient_email' => $user->email,
111+
// 'sender_name' => 'API Name',
114112
// ]);
115113

116-
response()->json(["message" => "ok"]);
114+
response()->json(['message' => 'ok']);
117115
}
118116

119117
public function reset_password()
120118
{
121119
// id retrieves the JWT from the headers, decodes it and returns
122120
// the user encoded into the token. If there's a problem with the token,
123121
// we can throw whatever error occurs. This means the user must be logged in.
124-
$userId = Auth::id() ?? response()->throwErr(Auth::errors());
125-
$password = request("password");
122+
$userId = auth()->id() ?? response()->throwErr(auth()->errors());
123+
$password = request('password');
126124

127-
// Get the
125+
// Get the current id
128126
$user = User::find($userId);
129-
if (!$user) response()->throwErr(["user" => "User not found! Check somewhere..."]);
127+
128+
if (!$user) {
129+
response()->throwErr(['user' => 'User not found! Check somewhere...']);
130+
}
130131

131132
// Change the user password
132133
$user->password = md5($password);
133134
$user->save();
134135

135136
// login again to get new token
136-
$user = Auth::login("users", ["id" => $userId]);
137-
if (!$user) response()->throwErr(Auth::errors());
137+
$user = auth()->login(['id' => $userId]);
138+
139+
if (!$user) {
140+
response()->throwErr(auth()->errors());
141+
}
138142

139143
response()->json($user);
140144
}
141145

142146
public function user() {
143-
// fields to hide from user list
144-
$hidden = ["id", "remember_token", "password"];
145-
146147
// Make sure user is logged in
147-
// $auth->user() is new in v2.4 of leaf
148-
$user = Auth::user("users", $hidden);
148+
// You can pass in an array of items to
149+
// hide from the returned user
150+
$user = auth()->user(['id', 'remember_token', 'password']);
149151

150-
response()->json($user ?? response()->throwErr(Auth::errors()));
152+
response()->json($user ?? response()->throwErr(auth()->errors()));
151153
}
152154

153155
public function edit()
154156
{
155-
// auth->id returns the user id encoded into jwt by default
156-
$userId = Auth::id() ?? response()->throwErr(Auth::errors());
157-
158157
// data to update
159-
$data = request(["username", "email", "password"]);
160-
161-
// data to find user by
162-
$where = ["id" => $userId];
158+
$data = request()->get(['username', 'email', 'password']);
163159

164-
// params which shouldn't already exist in db
165-
$uniques = ["username", "email"];
166-
167-
$user = Auth::update("users", $data, $where, $uniques);
160+
// update in auth v2 gets the currently authenticated
161+
// user from the request or session, hence, there's no
162+
// longer the need to mnually validate the user
163+
$user = auth()->update($data, [
164+
'username', 'email'
165+
]);
168166

169-
response()->json($user ?? response()->throwErr(Auth::errors()));
167+
response()->json($user ?? response()->throwErr(auth()->errors()));
170168
}
171169
}

0 commit comments

Comments
 (0)