leaf install auth
Vagy composer paranccsal:
composer require leafs/auth
Szükségünk van egy users nevű táblára.
MariaDB esetén:
create table users( id int not null primary key auto_increment, username varchar(50), email varchar(50), password varchar(250), created_at timestamp, updated_at timestamp );
Lehetséges változtatás:
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
A Leaf program használata során meg kell adnunk a formátumot.
$auth->config('TIMESTAMP_FORMAT', 'YYYY-MM-DD HH:mm:ss');
create table users( id integer not null primary key autoincrement, username text, email text, password text, enabled integer, created_at text, updated_at text );
SQLite esetén nem szükséges a dátumformátum beállítása.
$auth->connect('localhost', 'dbname', 'username', 'titok', 'mysql');
$auth->connect('', 'database.db', '', '', 'sqlite');
$auth = new Leaf\Auth; $auth->register([ 'username' => 'dani', 'email' => 'dani@zold.lan', 'password' => 'titok' ]);
$app = new Leaf\App; $auth = new Leaf\Auth; $auth->connect('localhost', 'pad', 'pad', 'titok', 'mysql'); $auth->config('TIMESTAMP_FORMAT', 'YYYY-MM-DD HH:mm:ss'); $app->post('/register', function() use($app, $auth) { $username = request()->get('username'); $email = request()->get('email'); $password = request()->get('password'); $data = $auth->register([ 'username' => $username, 'email' => $email, 'password' => $password ]); if($data) { $msg = $data; }else { $msg = $auth->errors(); } $app->response()->json($msg); });
$auth->login([ 'email' => 'dani@zold.lan', 'password' => 'titok' ]);
$app->post('/login', function() use($app, $auth) { $email = request()->get('email'); $password = request()->get('password'); $data = $auth->login([ 'email' => $email, 'password' => $password ]); if($data) { $msg = $data; }else { $msg = $auth->errors(); } $app->response()->json($msg); });
$app->get('/employees', function() use($app, $db, $auth) { $user = $auth->user(); if($user) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }else { $app->response()->json(['Hiba!' => 'Nem vagy bejelentkezve']); } });
Bővebb információval, visszatérési érték megadásával:
$app->get('/employees', function() use($app, $db, $auth) { $user = $auth->user(); if($user) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }else { $app->response()->json([ "error" => "Unauthorized", "data" => $auth->errors() ], 401); } });
$prot = function() use($app, $auth) { $user = $auth->user(); if(!$user) { $app->response()->json([ "error" => "Unauthorized", "data" => $auth->errors() ], 401); exit(); } }; $app->get('/employees', ['middleware' => $prot, function() use($app, $db, $auth) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }]);
$app->registerMiddleware('prot', function() use($app, $auth) { $user = $auth->user(); if(!$user) { $app->response()->json([ "error" => "Unauthorized", "data" => $auth->errors() ], 401); exit(); } }); $app->get('/employees', ['middleware' => 'prot', function() use($app, $db) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }]);
$app->registerMiddleware('auth', function() use($app, $auth) { $user = $auth->user(); if(!$user) { $app->response()->exit([ "error" => "Unauthorized", "data" => $auth->errors() ], 401); } }); $app->get('/employees', ['middleware' => 'auth', function() use($app, $db) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }]);
https://leafphp.dev/modules/auth/protecting-your-routes.html#using-middleware
$app->registerMiddleware('auth', function() use($app, $auth) { $user = $auth->user(); if(!$user) { $app->response()->exit([ "error" => "Unauthorized", "data" => $auth->errors() ], 401); } }); $app->group('/', ['middleware' => 'auth', function () use($app, $db, $auth){ $app->get('/employees', function() use($app, $db) { $emps = $db->query('select * from employees')->all(); $app->response()->json($emps); }); }]);