add function creatPost and login wp
This commit is contained in:
@@ -182,4 +182,91 @@ function ajouter_opengraph_meta_api($data, $post, $context) {
|
||||
|
||||
return $data;
|
||||
}
|
||||
add_filter('rest_prepare_post', 'ajouter_opengraph_meta_api', 10, 3);
|
||||
add_filter('rest_prepare_post', 'ajouter_opengraph_meta_api', 10, 3);
|
||||
|
||||
// ✅ Active les erreurs PHP pour le debug (désactive après les tests)
|
||||
define('WP_DEBUG', true);
|
||||
define('WP_DEBUG_LOG', true);
|
||||
define('WP_DEBUG_DISPLAY', false);
|
||||
@ini_set('log_errors', 1);
|
||||
@ini_set('display_errors', 0);
|
||||
|
||||
// ✅ Charger la librairie JWT
|
||||
use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
|
||||
// ✅ Configurer le secret JWT dans `wp-config.php`
|
||||
define('JWT_AUTH_SECRET_KEY', 'change_this_secret_key'); // Change ce secret !
|
||||
|
||||
// ✅ Fonction pour générer un token JWT
|
||||
function custom_generate_jwt_token($user_id) {
|
||||
$issuedAt = time();
|
||||
$expiration = $issuedAt + (60 * 60 * 24); // Expire en 24h
|
||||
|
||||
$payload = [
|
||||
'iss' => get_bloginfo('url'),
|
||||
'iat' => $issuedAt,
|
||||
'exp' => $expiration,
|
||||
'user_id' => $user_id
|
||||
];
|
||||
|
||||
return JWT::encode($payload, JWT_AUTH_SECRET_KEY, 'HS256');
|
||||
}
|
||||
|
||||
// ✅ Fonction pour vérifier un token JWT
|
||||
function custom_authenticate_with_jwt($token) {
|
||||
try {
|
||||
$decoded = JWT::decode($token, new Key(JWT_AUTH_SECRET_KEY, 'HS256'));
|
||||
return get_user_by('ID', $decoded->user_id);
|
||||
} catch (Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ Route API REST pour la connexion
|
||||
function custom_authenticate_user(WP_REST_Request $request) {
|
||||
$parameters = $request->get_json_params();
|
||||
$username = sanitize_text_field($parameters['username']);
|
||||
$password = sanitize_text_field($parameters['password']);
|
||||
|
||||
$user = wp_authenticate($username, $password);
|
||||
|
||||
if (is_wp_error($user)) {
|
||||
return new WP_Error('authentication_failed', 'Identifiants incorrects.', ['status' => 401]);
|
||||
}
|
||||
|
||||
$token = custom_generate_jwt_token($user->ID);
|
||||
|
||||
return [
|
||||
'user_id' => $user->ID,
|
||||
'token' => $token,
|
||||
'email' => $user->user_email,
|
||||
'role' => $user->roles
|
||||
];
|
||||
}
|
||||
|
||||
// ✅ Enregistrement des routes API
|
||||
add_action('rest_api_init', function () {
|
||||
register_rest_route('custom/v1', '/login', [
|
||||
'methods' => 'POST',
|
||||
'callback' => 'custom_authenticate_user',
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
});
|
||||
|
||||
// ✅ Autoriser les uploads et publications via l'API REST
|
||||
function allow_admin_upload_and_post($allcaps, $cap, $args) {
|
||||
if (in_array($cap[0], ['upload_files', 'edit_posts', 'publish_posts'])) {
|
||||
$allcaps[$cap[0]] = true;
|
||||
}
|
||||
return $allcaps;
|
||||
}
|
||||
add_filter('map_meta_cap', 'allow_admin_upload_and_post', 10, 3);
|
||||
|
||||
// ✅ Autoriser CORS (utile pour React)
|
||||
function custom_cors_headers() {
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Authorization");
|
||||
}
|
||||
add_action('init', 'custom_cors_headers');
|
||||
Reference in New Issue
Block a user