Laravel Passport Not Returning Token
I've created an authentication controller using laravel passport, but the result is not what it usually is. variable $token = $user->createToken('TestApp')->accessToken;
does not generate tokens in the oauth_access_tokens
table but instead creating token in the personal_access_token
table
My registration controller is
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'username' => 'required|unique:users,username',
'email' => 'required|email|unique:users,email',
'password' => 'required|confirmed',
'phone' => 'required',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->errors()], 401);
}
$input = $request->all();
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
$verifyUser = UserVerfication::create([
'user_id' => $user->id,
'token' => sha1(time())
]);
\Mail::to($user->email)->send(new VerifyMail($user));
$token = $user->createToken('TestApp')->accessToken;
return response()->json([
'success' => true,
'message' => 'Registration Success',
'data' => array('APItoken' => $token, 'user' => $user)
], 200);
}
And The result :
"APItoken": {
"name": "LinwokApp",
"abilities": [
"*"
],
"tokenable_id": 13,
"tokenable_type": "App\\Models\\User",
"updated_at": "2021-09-22T08:55:12.000000Z",
"created_at": "2021-09-22T08:55:12.000000Z",
"id": 11},
I do not know if the question is still valid, but I had the same problem and found a solution.
Check out from what namespace you have HasApiTokens.
I used:
use Laravel\Sanctum\HasApiTokens;
instead of
use Laravel\Passport\HasApiTokens;
this was causing the problem