-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Does laravel-mongodb has laravel sanctum support? #2242
-
Hello, I am using laravel sanctum with mongodb.
but when I am runnung:
$authToken = $user->createToken('auth-token')->plainTextToken();
I am getting the following error:
"message": "Call to a member function prepare() on null", "exception": "Error", "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php", "line": 465, "trace": [ { "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php", "line": 671, "function": "Illuminate\\Database\\{closure}", "class": "Illuminate\\Database\\Connection", "type": "->" },
and I think this is because of the lack of pre-built columns of personal_access_token table due to using mongodb.
is there any solution make it work with laravel sanctum?
Beta Was this translation helpful? Give feedback.
All reactions
Create a custom PersonalAccessToken Model
`use Laravel\Sanctum\Contracts\HasAbilities;
use Jenssegers\Mongodb\Eloquent\Model;
class PersonalAccessToken extends Model implements HasAbilities
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'abilities' => 'json',
'last_used_at' => 'datetime',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'token',
'abilities',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'token',
];
/**
* Get the tokenable model that the access token belongs to.
*
* @retur...
Replies: 2 comments 9 replies
-
in File vendor/laravel/sanctum/src/PersonalAccessToken.php Replace
use Illuminate\Database\Eloquent\Model; to
use Jenssegers\Mongodb\Eloquent\Model;
and write protected $connection = 'mongodb' ;
and in User Model don't forget to replace use Illuminate\Foundation\Auth\User as Authenticatable;
to use Jenssegers\Mongodb\Auth\User as Authenticatable;
and write
protected $connection = 'mongodb' ;
protected $collection = 'users';
Beta Was this translation helpful? Give feedback.
All reactions
-
👀 1
-
you should never replace code in vendors
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 4
-
Create a custom PersonalAccessToken Model
`use Laravel\Sanctum\Contracts\HasAbilities;
use Jenssegers\Mongodb\Eloquent\Model;
class PersonalAccessToken extends Model implements HasAbilities
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'abilities' => 'json',
'last_used_at' => 'datetime',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'token',
'abilities',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'token',
];
/**
* Get the tokenable model that the access token belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function tokenable()
{
return $this->morphTo('tokenable');
}
/**
* Find the token instance matching the given token.
*
* @param string $token
* @return static|null
*/
public static function findToken($token)
{
if (strpos($token, '|') === false) {
return static::where('token', hash('sha256', $token))->first();
}
[$id, $token] = explode('|', $token, 2);
if ($instance = static::find($id)) {
return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null;
}
}
/**
* Determine if the token has a given ability.
*
* @param string $ability
* @return bool
*/
public function can($ability)
{
return in_array('*', $this->abilities) ||
array_key_exists($ability, array_flip($this->abilities));
}
/**
* Determine if the token is missing a given ability.
*
* @param string $ability
* @return bool
*/
public function cant($ability)
{
return !$this->can($ability);
}
}`
then use it in your appService provider
`use Illuminate\Support\ServiceProvider;
use App\Models\Sanctum\PersonalAccessToken;
use Illuminate\Foundation\AliasLoader;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Loader Alias
$loader = AliasLoader::getInstance();
// SANCTUM CUSTOM PERSONAL-ACCESS-TOKEN
$loader->alias(\Laravel\Sanctum\PersonalAccessToken::class, \App\Models\Sanctum\PersonalAccessToken::class);
}
}`
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 5 -
🎉 6 -
❤️ 2
-
For me it gives:
ErrorException: Class "App\Models\Sanctum\PersonalAccessToken" not found in file E:\xampp8\htdocs\WIMB\vendor\laravel\framework\src\Illuminate\Foundation\AliasLoader.php on line 80
Beta Was this translation helpful? Give feedback.
All reactions
-
Working fine, just forgot to add namespace App\Models\Sanctum;
Beta Was this translation helpful? Give feedback.
All reactions
-
this works perfectly!
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm surprised this hasn't been fully integrated into the core functionality yet. Writing to +1 @kudjieRaymond's solution. It will resolve your issue using Sanctum with MongoDB.
Beta Was this translation helpful? Give feedback.
All reactions
-
I've created PHPORM-88 to track this internally. Please follow that ticket to get updates when we start implementing it.
Beta Was this translation helpful? Give feedback.
All reactions
-
❤️ 2