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

Commit 978712f

Browse files
committed
feat: update docs
1 parent 0f7f25c commit 978712f

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

app/controllers/Controller.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ public function __construct()
1414
parent::__construct();
1515

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

1919
// autoConnect uses the .env variables to quickly connect to db
2020
// Leaf auth will automagically connect to this db instance
21+
// Note that you only need to enable this if you didn't
22+
// already connect to the db in your public/index.php file
23+
// If you did, you can delete this whole block
2124
db()->autoConnect();
2225

2326
// You can configure auth to get additional customizations

app/controllers/UsersController.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@
1717
* you can switch to those as you see fit.
1818
*
1919
* Although a demo, it's a real controller and works correctly as is.
20-
* You can continue your project like this or edit it to match your app.
20+
* You can always delete this controller or link it to a route if you wish
21+
* to use it as a reference.
2122
*/
2223
class UsersController extends Controller
2324
{
2425
// refer to base controller to find package initialization
2526
// and auth settings
2627
public function login()
2728
{
28-
// From v2, you can also use request()
29-
// $password = request()->get('password');
30-
3129
// You can also mass assign particular fields from the request
3230
$credentials = request()->get(['username', 'password']);
3331

@@ -43,7 +41,7 @@ public function login()
4341

4442
// This line catches any errors that MAY happen
4543
if (!$user) {
46-
response()->throwErr(auth()->errors());
44+
response()->exit(auth()->errors());
4745
}
4846

4947
// We can call json on the response global shortcut method
@@ -69,7 +67,7 @@ public function register()
6967
]);
7068

7169
// Throws an error if there's an issue in validation
72-
if (!$validation) response()->throwErr(Form::errors());
70+
if (!$validation) response()->exit(Form::errors());
7371

7472
// Direct registration with Leaf Auth. Registers and initiates a
7573
// login, so you don't have to call login again, unless you want
@@ -81,7 +79,7 @@ public function register()
8179

8280
// throw an auth error if there's an issue
8381
if (!$user) {
84-
response()->throwErr(auth()->errors());
82+
response()->exit(auth()->errors());
8583
}
8684

8785
response()->json($user);
@@ -93,7 +91,7 @@ public function recover_account()
9391
$user = User::where('email', $username)->first() ?? null;
9492

9593
if (!$user) {
96-
response()->throwErr(['email' => 'Email not found']);
94+
response()->exit(['email' => 'Email not found']);
9795
}
9896

9997
// Set a temporary random password and reset user password
@@ -121,14 +119,14 @@ public function reset_password()
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());
122+
$userId = auth()->id() ?? response()->exit(auth()->errors());
125123
$password = request()->get('password');
126124

127125
// Get the current id
128126
$user = User::find($userId);
129127

130128
if (!$user) {
131-
response()->throwErr(['user' => 'User not found! Check somewhere...']);
129+
response()->exit(['user' => 'User not found! Check somewhere...']);
132130
}
133131

134132
// Change the user password
@@ -139,7 +137,7 @@ public function reset_password()
139137
$user = auth()->login(['id' => $userId]);
140138

141139
if (!$user) {
142-
response()->throwErr(auth()->errors());
140+
response()->exit(auth()->errors());
143141
}
144142

145143
response()->json($user);
@@ -151,7 +149,7 @@ public function user() {
151149
// hide from the returned user
152150
$user = auth()->user(['id', 'remember_token', 'password']);
153151

154-
response()->json($user ?? response()->throwErr(auth()->errors()));
152+
response()->json($user ?? response()->exit(auth()->errors()));
155153
}
156154

157155
public function edit()
@@ -166,6 +164,6 @@ public function edit()
166164
'username', 'email'
167165
]);
168166

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

public/index.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@
8888
*/
8989
Leaf\Database::config(DatabaseConfig());
9090

91+
/*
92+
|--------------------------------------------------------------------------
93+
| Sync Leaf Db with ORM and connect
94+
|--------------------------------------------------------------------------
95+
|
96+
| Sync Leaf Db with ORM and connect to the database
97+
| This allows you to use Leaf Db without having to initialize it
98+
| in your controllers.
99+
|
100+
| This is optional, you can still use Leaf Db in your controllers. If you
101+
| want to opt into this, just uncomment the line below.
102+
|
103+
*/
104+
// Leaf\Database::syncLeafDb();
105+
91106
/*
92107
|--------------------------------------------------------------------------
93108
| Initialise Config

0 commit comments

Comments
 (0)