From 978712f7fdaffe801b0cf36718a45e690ca12155 Mon Sep 17 00:00:00 2001 From: mychidarko Date: Fri, 3 Mar 2023 23:50:03 +0000 Subject: [PATCH] feat: update docs --- app/controllers/Controller.php | 5 ++++- app/controllers/UsersController.php | 24 +++++++++++------------- public/index.php | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/app/controllers/Controller.php b/app/controllers/Controller.php index 60a1191..e9ce9ad 100644 --- a/app/controllers/Controller.php +++ b/app/controllers/Controller.php @@ -14,10 +14,13 @@ public function __construct() parent::__construct(); // In this version, request isn't initialised for you. You can use - // requestData() or request() to get request data or initialise it yourself + // request() to get request data or initialise it yourself // autoConnect uses the .env variables to quickly connect to db // Leaf auth will automagically connect to this db instance + // Note that you only need to enable this if you didn't + // already connect to the db in your public/index.php file + // If you did, you can delete this whole block db()->autoConnect(); // You can configure auth to get additional customizations diff --git a/app/controllers/UsersController.php b/app/controllers/UsersController.php index 430245c..ce71b37 100644 --- a/app/controllers/UsersController.php +++ b/app/controllers/UsersController.php @@ -17,7 +17,8 @@ * you can switch to those as you see fit. * * Although a demo, it's a real controller and works correctly as is. - * You can continue your project like this or edit it to match your app. + * You can always delete this controller or link it to a route if you wish + * to use it as a reference. */ class UsersController extends Controller { @@ -25,9 +26,6 @@ class UsersController extends Controller // and auth settings public function login() { - // From v2, you can also use request() - // $password = request()->get('password'); - // You can also mass assign particular fields from the request $credentials = request()->get(['username', 'password']); @@ -43,7 +41,7 @@ public function login() // This line catches any errors that MAY happen if (!$user) { - response()->throwErr(auth()->errors()); + response()->exit(auth()->errors()); } // We can call json on the response global shortcut method @@ -69,7 +67,7 @@ public function register() ]); // Throws an error if there's an issue in validation - if (!$validation) response()->throwErr(Form::errors()); + if (!$validation) response()->exit(Form::errors()); // Direct registration with Leaf Auth. Registers and initiates a // login, so you don't have to call login again, unless you want @@ -81,7 +79,7 @@ public function register() // throw an auth error if there's an issue if (!$user) { - response()->throwErr(auth()->errors()); + response()->exit(auth()->errors()); } response()->json($user); @@ -93,7 +91,7 @@ public function recover_account() $user = User::where('email', $username)->first() ?? null; if (!$user) { - response()->throwErr(['email' => 'Email not found']); + response()->exit(['email' => 'Email not found']); } // Set a temporary random password and reset user password @@ -121,14 +119,14 @@ public function reset_password() // id retrieves the JWT from the headers, decodes it and returns // the user encoded into the token. If there's a problem with the token, // we can throw whatever error occurs. This means the user must be logged in. - $userId = auth()->id() ?? response()->throwErr(auth()->errors()); + $userId = auth()->id() ?? response()->exit(auth()->errors()); $password = request()->get('password'); // Get the current id $user = User::find($userId); if (!$user) { - response()->throwErr(['user' => 'User not found! Check somewhere...']); + response()->exit(['user' => 'User not found! Check somewhere...']); } // Change the user password @@ -139,7 +137,7 @@ public function reset_password() $user = auth()->login(['id' => $userId]); if (!$user) { - response()->throwErr(auth()->errors()); + response()->exit(auth()->errors()); } response()->json($user); @@ -151,7 +149,7 @@ public function user() { // hide from the returned user $user = auth()->user(['id', 'remember_token', 'password']); - response()->json($user ?? response()->throwErr(auth()->errors())); + response()->json($user ?? response()->exit(auth()->errors())); } public function edit() @@ -166,6 +164,6 @@ public function edit() 'username', 'email' ]); - response()->json($user ?? response()->throwErr(auth()->errors())); + response()->json($user ?? response()->exit(auth()->errors())); } } diff --git a/public/index.php b/public/index.php index fd1c062..d05ad6c 100644 --- a/public/index.php +++ b/public/index.php @@ -88,6 +88,21 @@ */ Leaf\Database::config(DatabaseConfig()); +/* +|-------------------------------------------------------------------------- +| Sync Leaf Db with ORM and connect +|-------------------------------------------------------------------------- +| +| Sync Leaf Db with ORM and connect to the database +| This allows you to use Leaf Db without having to initialize it +| in your controllers. +| +| This is optional, you can still use Leaf Db in your controllers. If you +| want to opt into this, just uncomment the line below. +| +*/ +// Leaf\Database::syncLeafDb(); + /* |-------------------------------------------------------------------------- | Initialise Config