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

Commit 9d24414

Browse files
committed
✨ Added authentication example
1 parent 05496ca commit 9d24414

File tree

15 files changed

+522
-64
lines changed

15 files changed

+522
-64
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use Leaf\Auth;
6+
7+
class AccountController extends Controller
8+
{
9+
public function user()
10+
{
11+
// The second parameter holds items to hide from the user array.
12+
$user = Auth::user("users", ["password"]);
13+
14+
if (!$user) throwErr(Auth::errors());
15+
16+
json($user);
17+
}
18+
19+
public function update()
20+
{
21+
$userId = Auth::id();
22+
23+
$data = request(["username", "email", "name"]);
24+
$dataKeys = array_keys($data);
25+
26+
$where = ["id" => $userId];
27+
28+
$uniques = ["username", "email"];
29+
30+
// This part simply removes empty fields from request
31+
foreach ($dataKeys as $key) {
32+
if (!$data[$key]) {
33+
unset($data[$key]);
34+
continue;
35+
}
36+
37+
if (!strlen($data[$key])) {
38+
unset($data[$key]);
39+
}
40+
}
41+
42+
// This section removes all uniques not found in request
43+
foreach ($uniques as $key => $unique) {
44+
if (!isset($data[$unique])) {
45+
unset($uniques[$key]);
46+
}
47+
}
48+
49+
$user = Auth::update("users", $data, $where, $uniques);
50+
51+
if (!$user) throwErr(Auth::errors());
52+
53+
json($user);
54+
}
55+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
/**
6+
* This is a base controller for the auth namespace
7+
*/
8+
class Controller extends \App\Controllers\Controller
9+
{
10+
//
11+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use Leaf\Auth;
6+
use Leaf\Form;
7+
8+
class LoginController extends Controller
9+
{
10+
public function index()
11+
{
12+
list($username, $password) = requestData(["username", "password"], true, true);
13+
14+
// You can now call leaf form methods statically.
15+
// Leaf v2.4.2 includes a new rule method which allows you to create
16+
// your own form rules
17+
Form::rule("max", function($field, $value, $params) {
18+
if (strlen($value) > $params) {
19+
Form::addError($field, "$field can't be more than $params characters");
20+
return false;
21+
}
22+
});
23+
24+
// You can also pass in custom parameters into your
25+
// form rules. The example below calls the max rule defined
26+
// above, and replaces the $params variable with 10.
27+
$validation = Form::validate([
28+
// To pass a param to a rule, just use :
29+
"username" => "max:15",
30+
"password" => "min:8",
31+
]);
32+
33+
// if validation fails, throw the errors
34+
if (!$validation) throwErr(Form::errors());
35+
36+
// Simple logins with leaf auth. It takes in the table
37+
// to search for users in and the credentials to check
38+
$user = Auth::login("users", [
39+
"username" => $username,
40+
"password" => $password
41+
]);
42+
43+
// If user isn't found, show some errors
44+
if (!$user) throwErr(Auth::errors());
45+
46+
json($user);
47+
}
48+
49+
public function logout()
50+
{
51+
// If you use session with your tokens, you
52+
// might want to remove all the saved data here
53+
json("Logged out successfully!");
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Controllers\Auth;
4+
5+
use Leaf\Auth;
6+
use Leaf\Form;
7+
8+
class RegisterController extends Controller
9+
{
10+
public function store()
11+
{
12+
$credentials = request(["username", "email", "password"]);
13+
14+
$validation = Form::validate([
15+
"username" => ["username", "max:15"],
16+
"email" => "email",
17+
"password" => "min:8"
18+
]);
19+
20+
if (!$validation) throwErr(Form::errors());
21+
22+
$user = Auth::register("users", $credentials, [
23+
"username", "email"
24+
]);
25+
26+
if (!$user) throwErr(Auth::errors());
27+
28+
json($user);
29+
}
30+
}

App/Controllers/Controller.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,35 @@
11
<?php
2+
23
namespace App\Controllers;
34

45
// Leaf Auth is a package which makes user authentication simple
56
use Leaf\Auth;
6-
use Leaf\Helpers\Password;
77

88
/**
99
* This is the base controller for your Leaf API Project.
1010
* You can initialize packages or define methods here to use
1111
* them across all your other controllers which extend this one.
1212
*/
13-
class Controller extends \Leaf\ApiController {
14-
public $auth;
15-
16-
public function __construct() {
13+
class Controller extends \Leaf\ApiController
14+
{
15+
public function __construct()
16+
{
1717
parent::__construct();
1818

1919
// In this version, request isn't initialised for you. You can use
2020
// requestData() or request() to get request data or initialise it yourself
21-
$this->auth = new Auth;
2221

2322
// autoConnect uses the .env variables to quickly connect to db
24-
$this->auth->autoConnect();
25-
23+
Auth::autoConnect();
24+
2625
// set default token expiry time
27-
$this->auth->tokenLifetime(60 * 60 * 24 * 365);
26+
Auth::tokenLifetime(60 * 60 * 24 * 365);
2827

2928
// You can configure auth to get additional customizations
30-
$this->auth->config("LOGIN_PARAMS_ERROR", "Username not registered!");
31-
32-
// Password encode is run when leaf wants to encode passwords on register
33-
// This exact method is used by default in Leaf, so you can delete it if
34-
// you want to.
35-
$this->auth->config("PASSWORD_ENCODE", function ($password) {
36-
return Password::hash($password);
37-
});
38-
39-
// this function is run to verify the password. It's done by default,
40-
// so you can remove this line and the above line if you wish to.
41-
$this->auth->config("PASSWORD_VERIFY", function ($password, $hashedPassword) {
42-
// Inside the password_verify method, you have access to the password and the hashed password
43-
return Password::verify($password, $hashedPassword);
44-
});
45-
46-
// You can refer to https://leafphp.netlify.app/#/leaf/v/2.4/core/auth for auth docs
47-
}
29+
// This can be done here with the Auth::config method or
30+
// simply in the Config/auth.php file
31+
Auth::config(AuthConfig());
32+
33+
// You can refer to https://leafphp.netlify.app/#/leaf/v/2.5/core/auth for auth docs
34+
}
4835
}

App/Routes/_auth.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
$app->group("/auth", function () use ($app) {
4+
$app->post("/login", "Auth\LoginController@index");
5+
$app->post("/register", "Auth\RegisterController@store");
6+
$app->get("/logout", "Auth\LoginController@logout");
7+
// Reset and recover account will be added later
8+
});
9+
10+
$app->group("/user", function () use ($app) {
11+
$app->get("/", "Auth\AccountController@user");
12+
$app->post("/update", "Auth\AccountController@update");
13+
});

App/Routes/_users.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

App/Routes/index.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@
4040

4141
// You can define your routes here directly or
4242
// import an independent route file
43-
require "_users.php";
43+
44+
// Example authentication has been created for you to give you
45+
// an idea on working with this version of leaf. To get rid of all
46+
// the comments, simply run php leaf scaffold:auth --api
47+
require __DIR__ . "/_auth.php";

Config/App.php

100644100755
File mode changed.

Config/aloe.php

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
"commands_path" => "/App/Console",
1515
"routes_path" => "/App/Routes",
1616
"lib_path" => "/Lib",
17-
]
17+
],
1818
];

0 commit comments

Comments
 (0)