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

Commit 10024f8

Browse files
committed
👽 updated auth config
1 parent 5645f07 commit 10024f8

File tree

2 files changed

+49
-20
lines changed

2 files changed

+49
-20
lines changed

app/controllers/UsersController.php

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

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

810
/**
@@ -39,15 +41,15 @@ public function login()
3941
// auth is initialised in the base controller
4042
// login allows us to sign a user in, and also generates
4143
// a jwt automatically
42-
$user = $this->auth->login("users", [
44+
$user = Auth::login("users", [
4345
"username" => $username,
4446
"password" => $password
4547
]);
4648

4749
// password encoding has been configured in the base controller
4850

4951
// This line catches any errors that MAY happen
50-
if (!$user) response()->throwErr($this->auth->errors());
52+
if (!$user) response()->throwErr(Auth::errors());
5153

5254
// json is another global shortcut method
5355
// it's shorter than $this->json()
@@ -64,25 +66,25 @@ public function register()
6466
$credentials = request(["username", "email", "password"]);
6567

6668
// You can validate your data with Leaf Form Validation
67-
$validation = $this->form->validate([
69+
$validation = Form::validate([
6870
"username" => "validUsername",
6971
"email" => "email",
7072
"password" => "required"
7173
]);
7274

7375
// Throws an error if there's an issue in validation
74-
if (!$validation) response()->throwErr($this->form->errors());
76+
if (!$validation) response()->throwErr(Form::errors());
7577

7678
// Direct registration with Leaf Auth. Registers and initiates a
7779
// login, so you don't have to call login again, unless you want
7880
// to. The 3rd parameter makes sure that the same username
7981
// and email can't be registered multiple times
80-
$user = $this->auth->register("users", $credentials, [
82+
$user = Auth::register("users", $credentials, [
8183
"username", "email"
8284
]);
8385

8486
// throw an auth error if there's an issue
85-
if (!$user) response()->throwErr($this->auth->errors());
87+
if (!$user) response()->throwErr(Auth::errors());
8688

8789
response($user);
8890
}
@@ -104,12 +106,12 @@ public function recover_account()
104106
// Send an email to user with the new temporary password
105107
// email() is a global method that allows you to send a
106108
// quick email. Don't forget to configure your .env variables
107-
email([
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",
112-
]);
109+
// 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",
114+
// ]);
113115

114116
response()->json(["message" => "ok"]);
115117
}
@@ -119,7 +121,7 @@ public function reset_password()
119121
// id retrieves the JWT from the headers, decodes it and returns
120122
// the user encoded into the token. If there's a problem with the token,
121123
// we can throw whatever error occurs. This means the user must be logged in.
122-
$userId = $this->auth->id() ?? response()->throwErr($this->auth->errors());
124+
$userId = Auth::id() ?? response()->throwErr(Auth::errors());
123125
$password = request("password");
124126

125127
// Get the
@@ -131,8 +133,8 @@ public function reset_password()
131133
$user->save();
132134

133135
// login again to get new token
134-
$user = $this->auth->login("users", ["id" => $userId]);
135-
if (!$user) response()->throwErr($this->auth->errors());
136+
$user = Auth::login("users", ["id" => $userId]);
137+
if (!$user) response()->throwErr(Auth::errors());
136138

137139
response()->json($user);
138140
}
@@ -143,15 +145,15 @@ public function user() {
143145

144146
// Make sure user is logged in
145147
// $auth->user() is new in v2.4 of leaf
146-
$user = $this->auth->user("users", $hidden);
148+
$user = Auth::user("users", $hidden);
147149

148-
response()->json($user ?? response()->throwErr($this->auth->errors()));
150+
response()->json($user ?? response()->throwErr(Auth::errors()));
149151
}
150152

151153
public function edit()
152154
{
153155
// auth->id returns the user id encoded into jwt by default
154-
$userId = $this->auth->id() ?? response()->throwErr($this->auth->errors());
156+
$userId = Auth::id() ?? response()->throwErr(Auth::errors());
155157

156158
// data to update
157159
$data = request(["username", "email", "password"]);
@@ -162,8 +164,8 @@ public function edit()
162164
// params which shouldn't already exist in db
163165
$uniques = ["username", "email"];
164166

165-
$user = $this->auth->update("users", $data, $where, $uniques);
167+
$user = Auth::update("users", $data, $where, $uniques);
166168

167-
response()->json($user ?? response()->throwErr($this->auth->errors()));
169+
response()->json($user ?? response()->throwErr(Auth::errors()));
168170
}
169171
}

config/auth.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@
135135
*/
136136
"GUARD_REGISTER" => "/auth/register",
137137

138+
/*
139+
|--------------------------------------------------------------------------
140+
| Logout route
141+
|--------------------------------------------------------------------------
142+
*/
143+
"GUARD_HOME" => "/home",
144+
138145
/*
139146
|--------------------------------------------------------------------------
140147
| Logout route
@@ -158,4 +165,24 @@
158165
|
159166
*/
160167
"SAVE_SESSION_JWT" => false,
168+
169+
/*
170+
|--------------------------------------------------------------------------
171+
| JWT Token Secret
172+
|--------------------------------------------------------------------------
173+
|
174+
| Secret string to encode JWT
175+
|
176+
*/
177+
"TOKEN_SECRET" => "@_leaf$0Secret!",
178+
179+
/*
180+
|--------------------------------------------------------------------------
181+
| JWT Lifetime
182+
|--------------------------------------------------------------------------
183+
|
184+
| How long should JWT be valid for?
185+
|
186+
*/
187+
"TOKEN_LIFETIME" => 60 * 60 * 24 * 365
161188
];

0 commit comments

Comments
 (0)