Felhasználói eszközök

Eszközök a webhelyen


oktatas:web:back-end_framework:leaf:azonositas

Különbségek

A kiválasztott változat és az aktuális verzió közötti különbségek a következők.

Összehasonlító nézet linkje

Előző változat mindkét oldalonElőző változat
oktatas:web:back-end_framework:leaf:azonositas [2024/03/16 09:12] – [Csoportos megadás] adminoktatas:web:back-end_framework:leaf:azonositas [2025/08/01 10:46] (aktuális) – eltávolítva admin
Sor 1: Sor 1:
-[[oktatas:web:back-end_framework:leaf|< Leaf]] 
- 
-====== Azonosítás ====== 
- 
-  * **Szerző:** Sallai András 
-  * Copyright (c) 2024, Sallai András 
-  * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] 
-  * Web: https://szit.hu 
- 
-===== 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: 
- 
-<code mysql> 
-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 
-); 
-</code> 
- 
- 
-Lehetséges változtatás: 
-<code mysql> 
-created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, 
-updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 
-</code> 
- 
-A Leaf program használata során meg kell adnunk a formátumot.  
- 
-<code php> 
-$auth->config('TIMESTAMP_FORMAT', 'YYYY-MM-DD HH:mm:ss'); 
-</code> 
- 
-==== SQLite ==== 
- 
-<code mysql> 
-create table users( 
-    id integer not null primary key autoincrement, 
-    username text, 
-    email text, 
-    password text, 
-    enabled integer, 
-    created_at text, 
-    updated_at text 
-); 
-</code> 
- 
-SQLite esetén nem szükséges a dátumformátum beállítása. 
- 
-===== Kapcsolódás adatbázishoz ===== 
-==== MariaDB ==== 
- 
-<code php> 
-$auth->connect('localhost', 'dbname', 'username', 'titok', 'mysql'); 
-</code> 
- 
-==== SQLite ==== 
- 
-<code php> 
-$auth->connect('', 'database.db', '', '', 'sqlite'); 
-</code> 
- 
-===== Regisztráció ===== 
- 
-<code php> 
-$auth = new Leaf\Auth; 
- 
-$auth->register([ 
-        'username' => 'dani', 
-        'email' => 'dani@zold.lan', 
-        'password' => 'titok' 
-    ]); 
- 
-</code> 
- 
-==== Útvonallal ==== 
- 
-<code php> 
-$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); 
-}); 
-</code> 
- 
-===== Belépés ===== 
- 
-<code php> 
-$auth->login([ 
-        'email' => 'dani@zold.lan', 
-        'password' => 'titok' 
-    ]); 
-</code> 
- 
-===== Útvonallal ===== 
- 
-<code php> 
-$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); 
-}); 
-</code> 
- 
-===== Útvonalak védelme ===== 
- 
-<code php> 
-$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']); 
-    } 
-}); 
-</code> 
- 
- 
-Bővebb információval, visszatérési érték megadásával: 
- 
-<code php> 
-$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); 
-    } 
-}); 
-</code> 
- 
-===== Egyszerűsítés köztes szoftverrel ===== 
- 
-<code php> 
-$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); 
-}]); 
- 
-</code> 
- 
-==== Elnevezett middleware ==== 
- 
-<code php> 
-$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); 
-}]); 
-</code> 
- 
-==== Legjobb változat ==== 
- 
-<code php> 
-$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); 
-}]); 
-</code> 
- 
-==== Csoportos megadás ==== 
- 
-<note important> 
-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 
-</note> 
- 
-<code php> 
-$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); 
-    }); 
- 
-}]); 
-</code> 
  
oktatas/web/back-end_framework/leaf/azonositas.1710576721.txt.gz · Utolsó módosítás: 2024/03/16 09:12 szerkesztette: admin