Skip to content

Instantly share code, notes, and snippets.

@hasinhayder
Last active November 30, 2023 05:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasinhayder/0b095766f7cc95b2d136b2ba6454df30 to your computer and use it in GitHub Desktop.
Save hasinhayder/0b095766f7cc95b2d136b2ba6454df30 to your computer and use it in GitHub Desktop.
mock login
<?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' => 'admin@doe.com',
'name' => 'Admin',
'password' => 'admin',
'role' => 'admin',
'token' => hash('sha256', 'admin')
],
[
'email' => 'john@doe.com',
'name' => 'John Doe',
'password' => 'john',
'role' => 'editor',
'token' => hash('sha256', 'john')
],
[
'email' => 'jane@doe.com',
'name' => 'Jane Doe',
'password' => 'jane',
'role' => 'editor',
'token' => hash('sha256', 'jane')
]
];
// 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;
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']
]);
exit;
}
}
}
echo json_encode([
'success' => 0,
'error' => 'Invalid credentials'
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment