Felhasználói eszközök

Eszközök a webhelyen


oktatas:web:back-end_framework:leaf:azonositas

< Leaf

Azonosítás

Függőség telepítése

leaf install auth

Vagy composer paranccsal:

composer require leafs/auth

Adatbázis

Szükségünk van egy users nevű táblára.

MariaDB

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');

SQLite

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.

Kapcsolódás adatbázishoz

MariaDB

$auth->connect('localhost', 'dbname', 'username', 'titok', 'mysql');

SQLite

$auth->connect('', 'database.db', '', '', 'sqlite');

Regisztráció

$auth = new Leaf\Auth;
 
$auth->register([
        'username' => 'dani',
        'email' => 'dani@zold.lan',
        'password' => 'titok'
    ]);

Útvonallal

$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);
});

Belépés

$auth->login([
        'email' => 'dani@zold.lan',
        'password' => 'titok'
    ]);

Útvonallal

$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);
});

Útvonalak védelme

$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);
    }
});

Egyszerűsítés köztes szoftverrel

$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);
}]);

Elnevezett middleware

$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);
}]);

Legjobb változat

$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);
}]);

Csoportos megadás

A hivatalos weboldalon hibás mintakód van fent. A group() metódusnak kell egy útvonal is. Ez nálam '/' lett.

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);
    });
 
}]);
oktatas/web/back-end_framework/leaf/azonositas.txt · Utolsó módosítás: 2024/03/16 09:12 szerkesztette: admin