PHP Classes

File: examples/case-studies/auth/validated_login_flow/validated_login_flow-GR.md

Recommend this page to a friend!
  Packages of Christos Drogidis   Ascoos OS   examples/case-studies/auth/validated_login_flow/validated_login_flow-GR.md   Download  
File: examples/case-studies/auth/validated_login_flow/validated_login_flow-GR.md
Role: Auxiliary data
Content type: text/markdown
Description: User Credential Validation and Authentication with Event Logging
Class: Ascoos OS
A PHP Web 5.0 Kernel for decentralized web and IoT
Author: By
Last change:
Date: 8 months ago
Size: 5,607 bytes
 

Contents

Class file image Download

????????? ??? ??????????????? ?????? ?? ????????? ?????????

???? ? ?????? ?????????? ??????????? ??? ?? Ascoos OS ?????? ?? ?????????? ?? credentials ???? ?????? ???? ??? ???????????????, ????????????? ?? ?????????? ????????. ?? ??????? ????????? modular handlers ??? ?????????, ??????????????? ??? ?????????? ?????????.

??????

  • ????????? credentials ?????? ?? ???????
  • ??????????????? ?????? ????? ???????????? ?????????
  • ????????? ????????? ????????? ? ?????????

?????? ??????? ??? Ascoos OS

  • TValidationHandler ????????? ????????? ??????? ?? ???????????????? ???????
  • TAuthenticationHandler ??????????????? ?????? ??? ?????????? token
  • TEventHandler ????????? ??? ?????????? ?????????

???? ???????

? ????????? ????????? ?? ??? ?????? PHP: - validated_login_flow.php

???????????? ??? ?? ??????: ?????????, ??????????????? ??? ????????? ?????????.

??????????????

  1. PHP ? 8.2
  2. ????????????? ?? Ascoos OS ? ?? AWES 26

??? ?????????

  1. ????????? ????????? ??? ????????? ?? ?????? `validated_login.log`.
  2. ??????????????? ?? handlers ??????????, ???????????????? ??? ?????????.
  3. ?????????? ?? handlers ?? ??? ??????????? ?????????.
  4. ????????????? ????????: - `validation.failed`: ???????? ?????????? - `auth.success`: ???????? ??????????????? - `auth.failed`: ???????? ????????????????
  5. ????????? credentials ?????? ??? ??????? ??????????.
  6. ?? ? ????????? ????????, ???????????? ?? ?????? ??? ???????????? JSON ?? ?? ????.
  7. ?? ? ??????????????? ???????, ????????????? token ??? ???????????? ????????.
  8. ?? ????????, ???????????? ????????.
  9. ???????????? JSON ?? ?? ??????????.

?????????? ??????

$auth = new TAuthenticationHandler($properties);
$validator = new TValidationHandler($properties);
$events = new TEventHandler([], $properties);

$auth->setEventHandler($events);
$validator->setEventHandler($events);

$events->register('login', 'validation.failed', fn($errors) => $events->logger->log("Validation failed: " . json_encode($errors)));
$events->register('login', 'auth.success', fn($user) => $events->logger->log("Authentication successful for: $user"));
$events->register('login', 'auth.failed', fn($user) => $events->logger->log("Authentication failed for: $user"));

$credentials = ['username' => 'admin', 'password' => 'securePass123'];
$rules = ['username' => 'required|string|min:3', 'password' => 'required|string|min:8'];

if (!$validator->validate($credentials, $rules)) {
    $validator->emit('validation.failed', $validator->getErrors());
    echo json_encode(['status' => 'validation_error', 'errors' => $validator->getErrors()], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    return;
}

if ($auth->authenticate($credentials)) {
    $token = $auth->generateToken();
    $auth->emit('auth.success', $credentials['username']);
} else {
    $auth->emit('auth.failed', $credentials['username']);
    $token = null;
}

echo json_encode([
    'user' => $credentials['username'],
    'authenticated' => $token !== null,
    'token' => $token
], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);

??????????? ??????????

?? ? ????????? ????????:

{
  "status": "validation_error",
  "errors": {
    "password": "?? ????? password ?????? ?? ???? ??????????? 8 ??????????."
  }
}

?? ? ??????????????? ???????:

{
  "user": "admin",
  "authenticated": true,
  "token": "..."
}

?? ????????:

{
  "user": "admin",
  "authenticated": false,
  "token": null
}

?????

??????????

???????? ?? ??????????? ?? ?????? ???????????? ???????? ????????, ?????????? ??? ?????????? ??????? ? ????????????? ??????????? ????????????????. ????? ?? CONTRIBUTING.md ??? ???????.

????? ??????

???? ? ?????? ?????????? ??? ??? Ascoos General License (AGL). ????? ?? LICENSE.md.