Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Last active November 30, 2023 05:15
Show Gist options
  • Save hasinhayder/0b095766f7cc95b2d136b2ba6454df30 to your computer and use it in GitHub Desktop.
Save hasinhayder/0b095766f7cc95b2d136b2ba6454df30 to your computer and use it in GitHub Desktop.

Revisions

  1. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 32 additions and 28 deletions.
    60 changes: 32 additions & 28 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -6,47 +6,51 @@

    $users = [
    [
    'email'=>'[email protected]',
    'name'=>'Admin',
    'password'=>'admin',
    'role'=>'admin',
    'token'=>hash('sha256', 'admin')
    'email' => '[email protected]',
    'name' => 'Admin',
    'password' => 'admin',
    'role' => 'admin',
    'token' => hash('sha256', 'admin')
    ],
    [
    'email'=>'[email protected]',
    'name'=>'John Doe',
    'password'=>'john',
    'role'=>'editor',
    'token'=>hash('sha256', 'john')
    'email' => '[email protected]',
    'name' => 'John Doe',
    'password' => 'john',
    'role' => 'editor',
    'token' => hash('sha256', 'john')
    ],
    [
    'email'=>'[email protected]',
    'name'=>'Jane Doe',
    'password'=>'jane',
    'role'=>'editor',
    'token'=>hash('sha256', 'jane')
    'email' => '[email protected]',
    'name' => 'Jane Doe',
    'password' => 'jane',
    'role' => 'editor',
    'token' => hash('sha256', 'jane')
    ]
    ];

    if(isset($_POST['email']) && isset($_POST['password'])){
    $email = $_POST['email'];
    $password = $_POST['password'];
    // Check if data is sent as JSON payload or form data
    $jsonData = file_get_contents("php://input");
    $data = !empty($jsonData) ? json_decode($jsonData, true) : $_POST;

    foreach($users as $user){
    if($user['email'] === $email && $user['password'] === $password){
    if (isset($data['email']) && isset($data['password'])) {
    $email = $data['email'];
    $password = $data['password'];

    foreach ($users as $user) {
    if ($user['email'] === $email && $user['password'] === $password) {
    echo json_encode([
    'success'=>1,
    'name'=>$user['name'],
    'username'=>$user['email'],
    'role'=>$user['role'],
    'token'=>$user['token']
    'success' => 1,
    'name' => $user['name'],
    'username' => $user['email'],
    'role' => $user['role'],
    'token' => $user['token']
    ]);
    exit;
    }
    }
    }

    echo json_encode([
    'success'=>0,
    'error'=>'Invalid credentials'
    ]);
    'success' => 0,
    'error' => 'Invalid credentials'
    ]);
  2. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    <?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: POST, GET');
    header('Access-Control-Allow-Headers: Content-Type, Authorization');

    $users = [
    [
    'email'=>'[email protected]',
  3. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions index.php
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    <?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: *');
    $users = [
    [
    'email'=>'[email protected]',
  4. hasinhayder renamed this gist Nov 30, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -40,11 +40,6 @@
    exit;
    }
    }

    echo json_encode([
    'success'=>0,
    'error'=>'Invalid credentials'
    ]);
    }

    echo json_encode([
  6. hasinhayder revised this gist Nov 30, 2023. No changes.
  7. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -5,19 +5,22 @@
    'email'=>'[email protected]',
    'name'=>'Admin',
    'password'=>'admin',
    'role'=>'admin'
    'role'=>'admin',
    'token'=>hash('sha256', 'admin')
    ],
    [
    'email'=>'[email protected]',
    'name'=>'John Doe',
    'password'=>'john',
    'role'=>'editor'
    'role'=>'editor',
    'token'=>hash('sha256', 'john')
    ],
    [
    'email'=>'[email protected]',
    'name'=>'Jane Doe',
    'password'=>'jane',
    'role'=>'editor'
    'role'=>'editor',
    'token'=>hash('sha256', 'jane')
    ]
    ];

    @@ -31,7 +34,8 @@
    'success'=>1,
    'name'=>$user['name'],
    'username'=>$user['email'],
    'role'=>$user['role']
    'role'=>$user['role'],
    'token'=>$user['token']
    ]);
    exit;
    }
  8. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    <?php
    //simple api endpoint for login with roles

    header('Content-Type: application/json');
    $users = [
    [
    'email'=>'[email protected]',
  9. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion login.php
    Original file line number Diff line number Diff line change
    @@ -42,5 +42,9 @@
    'success'=>0,
    'error'=>'Invalid credentials'
    ]);
    exit;
    }

    echo json_encode([
    'success'=>0,
    'error'=>'Invalid credentials'
    ]);
  10. hasinhayder revised this gist Nov 30, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,7 @@
    if($user['email'] === $email && $user['password'] === $password){
    echo json_encode([
    'success'=>1,
    'name'=>$user['name'],
    'username'=>$user['email'],
    'role'=>$user['role']
    ]);
  11. hasinhayder created this gist Nov 30, 2023.
    45 changes: 45 additions & 0 deletions login.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    <?php
    //simple api endpoint for login with roles

    $users = [
    [
    'email'=>'[email protected]',
    'name'=>'Admin',
    'password'=>'admin',
    'role'=>'admin'
    ],
    [
    'email'=>'[email protected]',
    'name'=>'John Doe',
    'password'=>'john',
    'role'=>'editor'
    ],
    [
    'email'=>'[email protected]',
    'name'=>'Jane Doe',
    'password'=>'jane',
    'role'=>'editor'
    ]
    ];

    if(isset($_POST['email']) && isset($_POST['password'])){
    $email = $_POST['email'];
    $password = $_POST['password'];

    foreach($users as $user){
    if($user['email'] === $email && $user['password'] === $password){
    echo json_encode([
    'success'=>1,
    'username'=>$user['email'],
    'role'=>$user['role']
    ]);
    exit;
    }
    }

    echo json_encode([
    'success'=>0,
    'error'=>'Invalid credentials'
    ]);
    exit;
    }