Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Does laravel-mongodb has laravel sanctum support? #2242

Answered by kudjieRaymond
rhasani asked this question in Q&A
Discussion options

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?

You must be logged in to vote

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

Comment options

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';

You must be logged in to vote
1 reply
Comment options

you should never replace code in vendors

Comment options

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);
}

}`

You must be logged in to vote
8 replies
Comment options

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

Comment options

Working fine, just forgot to add namespace App\Models\Sanctum;

Comment options

this works perfectly!

Comment options

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.

Comment options

I've created PHPORM-88 to track this internally. Please follow that ticket to get updates when we start implementing it.

Answer selected by divine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #2233 on April 30, 2021 09:58.

AltStyle によって変換されたページ (->オリジナル) /