PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, April 19, 2022

[FIXED] How to get user by Token in Sanctum Laravel

 April 19, 2022     laravel, laravel-sanctum     No comments   

Issue

I'm passing to the server in the POST request body the Token of the user. I need to find out which user this token belongs to. In laravel/sanctum documentation I found out that it is possible to do it only by putting the Token as "Authorization": "Bearer ****" header. But it is not my case, I need to pass it in the POST body. Is there a way to do so?


Solution

I found a solution by making a few experiments and reading the source code of Sanctum. User's data is possible to get by token in POST data in that way:

$post_data = $request->all();
if (isset($post_data['user_token'])) {
    [$id, $user_token] = explode('|', $post_data['user_token'], 2);
    $token_data = DB::table('personal_access_tokens')->where('token', hash('sha256', $partner_token))->first();
    $user_id = $user_id->tokenable_id; // !!!THIS ID WE CAN USE TO GET DATA OF YOUR USER!!!
}


Answered By - Dmytro Huz
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing