implement find_or_create_user
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
{
|
||||
"name": "dabestaniha/authenticate-bridge",
|
||||
"require": {
|
||||
"php": "^8.2"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Dabestaniha\\AuthenticateBridge\\": "src/"
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Dabestaniha Authenticate Bridge
|
||||
* Description: Authenticate WordPress user via json web token from Laravel Application.
|
||||
* Description: Authenticate WordPress user via json web token from Mahakiha Application.
|
||||
* Version: 1.0
|
||||
* Author: Dabestaniha
|
||||
*
|
||||
* Example: https://wordpress.test/dabestaniha/login/?token=123&name=asd&email=test@wordpress.test
|
||||
*/
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
|
||||
use Dabestaniha\AuthenticateBridge\Requests\DabestanihaAuthenticateRequest;
|
||||
use Dabestaniha\AuthenticateBridge\App\Http\Requests\DabestanihaAuthenticateRequest;
|
||||
|
||||
add_action('init', function () {
|
||||
$request = DabestanihaAuthenticateRequest::make();
|
||||
@@ -19,34 +21,54 @@ add_action('init', function () {
|
||||
|
||||
$request->validate();
|
||||
$token = $request->string('token');
|
||||
var_dump($token);
|
||||
die();
|
||||
|
||||
$name = $request->string('name');
|
||||
$email = $request->string('email');
|
||||
|
||||
// اینجا توکن رو اعتبارسنجی میکنیم
|
||||
$user_id = verify_laravel_token_and_get_user_id($token); // این تابع رو بعداً مینویسیم
|
||||
$user_id = find_or_create_user($email, $name);
|
||||
|
||||
if ($user_id) {
|
||||
wp_set_auth_cookie($user_id);
|
||||
wp_set_current_user($user_id);
|
||||
do_action('wp_login', get_userdata($user_id)->user_login, get_userdata($user_id));
|
||||
|
||||
// انتقال به پنل
|
||||
login_user($user_id);
|
||||
wp_redirect(home_url('/dashboard'));
|
||||
exit;
|
||||
} else {
|
||||
wp_die('توکن نامعتبر است.');
|
||||
}
|
||||
exit();
|
||||
});
|
||||
|
||||
function verify_laravel_token_and_get_user_id($token): bool|int
|
||||
{
|
||||
// مثلا: رمزگشایی JWT، یا بررسی دیتابیس توکنها
|
||||
// برای تست میتونیم یک توکن ثابت بذاریم
|
||||
|
||||
if ($token === 'sample_token_123') {
|
||||
return 2; // ID یک کاربر موجود
|
||||
return 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
function find_or_create_user(string $email, string $name): int
|
||||
{
|
||||
$user = get_user_by('email', $email);
|
||||
|
||||
if ($user) {
|
||||
return $user->ID;
|
||||
}
|
||||
$random_password = wp_generate_password(12, true);
|
||||
$username = sanitize_user(strstr($email, '@', true));
|
||||
|
||||
if (username_exists($username)) {
|
||||
$username .= '_'.wp_generate_password(4, false);
|
||||
}
|
||||
|
||||
$user_id = wp_create_user($username, $random_password, $email);
|
||||
|
||||
if (is_wp_error($user_id)) {
|
||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
||||
}
|
||||
|
||||
wp_update_user([
|
||||
'ID' => $user_id,
|
||||
'display_name' => $name,
|
||||
]);
|
||||
|
||||
return $user_id;
|
||||
}
|
||||
|
||||
function login_user(int $user_id): bool
|
||||
{
|
||||
wp_set_auth_cookie($user_id);
|
||||
wp_set_current_user($user_id);
|
||||
do_action('wp_login', get_userdata($user_id)->user_login, get_userdata($user_id));
|
||||
return true;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Dabestaniha\AuthenticateBridge\Requests;
|
||||
namespace Dabestaniha\AuthenticateBridge\App\Http\Requests;
|
||||
|
||||
use Dabestaniha\AuthenticateBridge\Support\Collection;
|
||||
|
||||
@@ -54,6 +54,7 @@ abstract class BaseFormRequest
|
||||
{
|
||||
return match ($rule) {
|
||||
'required' => !empty($value),
|
||||
'nullable' => true,
|
||||
default => false,
|
||||
};
|
||||
}
|
||||
+5
-2
@@ -1,19 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Dabestaniha\AuthenticateBridge\Requests;
|
||||
namespace Dabestaniha\AuthenticateBridge\App\Http\Requests;
|
||||
|
||||
class DabestanihaAuthenticateRequest extends BaseFormRequest
|
||||
{
|
||||
|
||||
public function shouldContinue(): bool
|
||||
{
|
||||
return $this->getRoutePath() === '/dabestaniha/login';
|
||||
$path = $this->getRoutePath();
|
||||
return trim($path, '/') === 'dabestaniha/login';
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'token' => ['required'],
|
||||
'name' => ['nullable'],
|
||||
'email' => ['nullable'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -7,3 +7,12 @@ if (!function_exists('view')) {
|
||||
return file_get_contents("{$basedir}resources/views/$view.php");
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('dd')) {
|
||||
function dd(): never
|
||||
{
|
||||
echo '<pre>';
|
||||
array_map(function($arg) {var_dump($arg);}, func_get_args());
|
||||
die;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
<!-- views/invalid-token.php -->
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>خطا سامانه</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>اوه...</h1>
|
||||
<p>مشکلی پیش آمده، لطفا تا دقایق دیگر دوباره امتحان کنید.</p>
|
||||
<a href="<?= home_url() ?>">بازگشت به صفحه اصلی</a>
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+2
@@ -22,6 +22,8 @@ class ComposerAutoloaderInit1e84672d5963a78cf9ba3e25dc66cf6f
|
||||
return self::$loader;
|
||||
}
|
||||
|
||||
require __DIR__ . '/platform_check.php';
|
||||
|
||||
spl_autoload_register(array('ComposerAutoloaderInit1e84672d5963a78cf9ba3e25dc66cf6f', 'loadClassLoader'), true, true);
|
||||
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
|
||||
spl_autoload_unregister(array('ComposerAutoloaderInit1e84672d5963a78cf9ba3e25dc66cf6f', 'loadClassLoader'));
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
// platform_check.php @generated by Composer
|
||||
|
||||
$issues = array();
|
||||
|
||||
if (!(PHP_VERSION_ID >= 80200)) {
|
||||
$issues[] = 'Your Composer dependencies require a PHP version ">= 8.2.0". You are running ' . PHP_VERSION . '.';
|
||||
}
|
||||
|
||||
if ($issues) {
|
||||
if (!headers_sent()) {
|
||||
header('HTTP/1.1 500 Internal Server Error');
|
||||
}
|
||||
if (!ini_get('display_errors')) {
|
||||
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
|
||||
fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL);
|
||||
} elseif (!headers_sent()) {
|
||||
echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL;
|
||||
}
|
||||
}
|
||||
throw new \RuntimeException(
|
||||
'Composer detected issues in your platform: ' . implode(' ', $issues)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user