'Laravel Passport - Not able to get token string in response?
i am getting result like below in registration using passport laravel
"success": {
"token": {
"name": "MyApp",
"abilities": [
"*"
],
"tokenable_id": 6,
"tokenable_type": "App\\Models\\User",
"updated_at": "2021-08-28T14:41:33.000000Z",
"created_at": "2021-08-28T14:41:33.000000Z",
"id": 5
},
"name": "Test"
I want to get the token string in response ...what to do now.
Below my api controller function :
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
class ApiController extends Controller
{
public function register(Request $request)
{
$successStatus = 200;
$validator = \Validator::make($request->all(), [
'name' => 'required',
'email' => 'required|email',
'password' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = \Hash::make($input['password']);
$user = User::create($input);
$success['token'] = $user->createToken('MyApp')->accessToken;
$success['name'] = $user->name;
return response()->json(['success'=>$success],200);
}
}
Solution 1:[1]
You shouldn't use accessToken
instead try this way :
$user->createToken('MyApp')->plainTextToken;
Solution 2:[2]
php artisan config:cache &&
php artisan passport:install &&
php artisan passport:client --personal
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Xiidref |
Solution 2 | Adrian Mole |