next-drupal icon indicating copy to clipboard operation
next-drupal copied to clipboard

How do I get the authenticated user details?

Open dakala opened this issue 2 years ago • 6 comments

I've implemented the Demo Umami example and everything seems to be working. However, when I log in, I don't get the authenticated user details (I was expecting at least the uid, username and email) in the response. What am I doing wrong? How do I fix it? Thanks.

Screenshot 2023-02-09 at 09 30 35

dakala avatar Feb 09 '23 09:02 dakala

I think you need to grant proper permission in Drupal backend to user role that access token belongs to. Be carefull this might have security implications when used wrong.

Screenshot from 2023-02-09 13-10-38

p0zi avatar Feb 09 '23 12:02 p0zi

Thanks @p0zi . I've added a Next.js role with perms as suggested in the screenshot and assigned my user (drupal admin(1) user) to the Next.js role. I cleared Drupal cache and restarted the Next.js frontend site. No change. Could it be something missing in my Consumer settings?

Screenshot 2023-02-09 at 13 14 18

dakala avatar Feb 09 '23 13:02 dakala

@see: https://github.com/chapter-three/next-drupal/issues/337

p0zi avatar Mar 09 '23 17:03 p0zi

Thanks @p0zi. I'll go back to this soon and give that hook in #337 a try.

dakala avatar Mar 10 '23 13:03 dakala

I made a try yesterday and all works great as @shadcn wrote, here's the full code in custom_module.module:

<?php
use Drupal\simple_oauth\Entities\AccessTokenEntity;
use Drupal\user\Entity\User;

/**
 * Implements hook_simple_oauth_private_claims_alter().
 * 
 * Alter the private claims to prepare convert to JWT token.
 *
 * @param $private_claims
 *   The private claims array to be altered.
 * @param \Drupal\simple_oauth\Entities\AccessTokenEntity $access_token_entity
 *
 * @see \Drupal\simple_oauth\Entities\AccessTokenEntity::convertToJWT()
 */
function custom_module_simple_oauth_private_claims_alter(&$private_claims, AccessTokenEntity $access_token_entity) {
    $user_id = $access_token_entity->getUserIdentifier();
    /** @var \Drupal\user\UserInterface $user */
    $user = User::load($user_id);

    $private_claims = [
        'id' => $user->uuid(),
        'email' => $user->getEmail(),
        'name' => $user->getAccountName(),
    ];
}

p0zi avatar Mar 10 '23 14:03 p0zi

nothing is working really frusted. Poor documentations, this issue is still not solved

Did you implement Drupal side code as custom module?

p0zi avatar Feb 04 '24 21:02 p0zi