install firebase jwt and use token for authenticate
This commit is contained in:
@@ -5,11 +5,12 @@
|
||||
* Version: 1.0
|
||||
* Author: Dabestaniha
|
||||
*
|
||||
* Example: https://wordpress.test/dabestaniha/login/?token=123&name=asd&email=test@wordpress.test
|
||||
* Example: https://wordpress.test/dabestaniha/login/?token=eyJ0...
|
||||
*/
|
||||
|
||||
require_once __DIR__.'/vendor/autoload.php';
|
||||
|
||||
use Firebase\JWT;
|
||||
use Dabestaniha\AuthenticateBridge\App\Http\Requests\DabestanihaAuthenticateRequest;
|
||||
|
||||
add_action('init', function () {
|
||||
@@ -22,10 +23,13 @@ add_action('init', function () {
|
||||
$request->validate();
|
||||
$token = $request->string('token');
|
||||
|
||||
$name = $request->string('name');
|
||||
$email = $request->string('email');
|
||||
$payload = decrypt_jwt_token($token);
|
||||
|
||||
$user_id = find_or_create_user($email, $name);
|
||||
$identifier_name = get_configured_option('user-identifier');
|
||||
$identifier_value = $payload[$identifier_name] ?? null;
|
||||
|
||||
$name = $payload['name'] ?? config('da.translates.new-user');
|
||||
$user_id = find_or_create_user($identifier_name, $identifier_value, $name);
|
||||
|
||||
login_user($user_id);
|
||||
wp_redirect(home_url('/dashboard'));
|
||||
@@ -34,26 +38,34 @@ add_action('init', function () {
|
||||
|
||||
require_once 'settings-page.php';
|
||||
|
||||
function verify_laravel_token_and_get_user_id($token): bool|int
|
||||
// $jwt_secret = get_configured_option('jwt-secret');
|
||||
function decrypt_jwt_token(string $token): array
|
||||
{
|
||||
return 0;
|
||||
$jwt_secret = get_configured_option('jwt-secret');
|
||||
|
||||
try {
|
||||
$decoded = (array) JWT\JWT::decode($token, new JWT\Key($jwt_secret, 'HS256'));
|
||||
return (array) $decoded['data'];
|
||||
} catch (\Exception $e) {
|
||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
||||
}
|
||||
}
|
||||
|
||||
function find_or_create_user(string $email, string $name): int
|
||||
function find_or_create_user(string $identifier_name, string $identifier_value, string $name): int
|
||||
{
|
||||
$user = get_user_by('email', $email);
|
||||
$user = get_user_by($identifier_name, $identifier_value);
|
||||
|
||||
if ($user) {
|
||||
return $user->ID;
|
||||
}
|
||||
$random_password = wp_generate_password(12, true);
|
||||
$username = sanitize_user(strstr($email, '@', true));
|
||||
$username = $identifier_value;
|
||||
|
||||
if (username_exists($username)) {
|
||||
$username .= '_'.wp_generate_password(4, false);
|
||||
}
|
||||
|
||||
$user_id = wp_create_user($username, $random_password, $email);
|
||||
$user_id = wp_create_user($username, $random_password);
|
||||
|
||||
if (is_wp_error($user_id)) {
|
||||
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 500]);
|
||||
|
||||
Reference in New Issue
Block a user