Mahaveer's avatar

Get user ID By Access Token ?

Without using auth middleware, how to get user id by token?

//Get access token
        $access_token = $request->header('Authorization');

        // break up the string to get just the token
        $auth_header = explode(' ', $access_token);
        
        $token = $auth_header[1];
        
        // break up the token into its three parts
        $token_parts = explode('.', $token);
        
        $token_header = $token_parts[0];

        // base64 decode to get a json string
        $token_header_json = base64_decode($token_header);
        
        // then convert the json to an array
        $token_header_array = json_decode($token_header_json, true);
        
        $user_token = $token_header_array['jti'];
        
        // find the user ID from the oauth access token table
        // based on the token we just got
        $user_id = DB::table('oauth_access_tokens')->where('id', $user_token)->first();

        

        // then retrieve the user from it's primary key
        $user = User::find($user_id->id);

        echo $user->id ?? '';
        exit();

0 likes
6 replies
sauravs012's avatar

$user_id = DB::table('oauth_access_tokens')->where('id', $user_token)->first(); echo $user_id->id;

Mahaveer's avatar

@thoasty How to get user ID from header bear access token in passport;

Authorization:Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiIyIiwianRpIjoiZDYxODY4YWQ1YzEyZWY3YjFkMjE0NjBmNWU4N2ZiNTVlYzlkYjI3MWFkMWVjYzgzOTcwNTg3MTA4MTExZTJmMGE1MDQxNmY0NzkwYTIyMzgiLCJpYXQiOjE1ODI1NDYzMzAsIm5iZiI6MTU4MjU0NjMzMCwiZXhwIjoxNjE0MTY4NzMwLCJzdWIiOiI3Iiwic2NvcGVzIjpbXX0.kTxBqUczYyrNoV0OLrQQoODLtLJ6Zm9xtZxPfvdROznZ1Mo10WA4xrW9t0mhzL6jxc1HMdIngqjo4YIf7m4FaKeJnpW9qt2JMuI_uIgKnrYQe7z__d35mph0B3ykHKhsdjlyKm6iql4LniSuQwNDT2QXmNBVn8MYZ_lA-ioZH4qYQoAQbn-AY7UXwmS6t6Agn3gePJgajjRmIxCYLuseHSbRDt_2X9DUwLrSm6sFKKCqsJQyZPto0Hmxgv_a4A-NHIJ6ZXEE3ql8Bx6PkebK2aKX97l1SVYx4NjwFvQn8PsvZRm1HU05gnwzSPALfBhcWoF7rr6u8RuZwdLGwOUnfgOz1P0EzsJt1qgnCg0hAJ1MkZT4Lkcr_2MxPYVhW2bJXZqf89jHf9NBLjOv0j8q_rvVhKmG5DazeVF-SBHBURIDJZtUX75dve_LBYawPox0KcsYKmU6e55epjL1WXwaZhxF7O81nb3Shu7TJx6kSNK5Nm65MuEnLFRH81eLocLpInPD26Z7noPxuEgWnN4hNl9b6N_QneYTE73t0g07NP3Fg-ckFsZcWlkVJe8uslwr3nlv30UBj9TTdXjtZB80MoPjuP23Ys8QdbwGfNJBb9bLn1kB1_N14p06IWIc8Kvq0I2llq_1jmmJpksWbYreXlyxEemDHDaL6GKd9cxjfPc

evecimar's avatar

You need:

  • Replace a token_parts[0] to token_parts[1], because jti is in body token;

  • You can use Laravel\Passport\Token\Token::find($jti) to get a token in data base;

  • Token:find return object tath contain user_id.

  • You user $token->user_id to get a user model

      	$access_token = $request->header('Authorization');
          $auth_header = explode(' ', $access_token);
          $token = $auth_header[1];
          $token_parts = explode('.', $token);
          $token_header = $token_parts[1];
          $token_header_json = base64_decode($token_header);
          $token_header_array = json_decode($token_header_json, true);
          $token_id = $token_header_array['jti'];
    
      	$user = Token::find($token_id)->user
    
4 likes
oleevier's avatar

Am I right that if there was created token so user passed thru AUTH , so we can use :

$user = auth('api')->user();

like i use in my code ($request contains only Bearer token and getData is a method behind API AUTH) :

public function getData(Request $request) {
        $user_id = auth('api')->user()->id;
						// rest code
    }
2 likes

Please or to participate in this conversation.