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

Commit

Permalink
feat: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Mar 3, 2023
1 parent 0f7f25c commit 978712f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
5 changes: 4 additions & 1 deletion app/controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 11 additions & 13 deletions app/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@
* 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
{
// refer to base controller to find package initialization
// 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']);

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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()
Expand All @@ -166,6 +164,6 @@ public function edit()
'username', 'email'
]);

response()->json($user ?? response()->throwErr(auth()->errors()));
response()->json($user ?? response()->exit(auth()->errors()));
}
}
15 changes: 15 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 978712f

Please sign in to comment.