Laravel sanctum custom model - laravel

I'm using Laravel sanctum to authenticate my API, and I wanted to customize personal access token model so I did the following:
I created new model named PersonalAccessToken in App namespace.
I override model used in sanctum to be my new model by adding this line to my AppServiceProvider boot() method.
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
but when I create a token it works fine and insert it into DB but this line throw exception
return new NewAccessToken($token, $token->id.'|'.$plainTextToken);
and that's because it's type hinted to be an instance of Laravel\Sanctum\PersonalAccessToken
how can I fix that

If you are not extending the default PersonalAccessToken that maybe your issue.
Instead of extending Model extend use Laravel\Sanctum\PersonalAccessToken
use Laravel\Sanctum\PersonalAccessToken as Model;
class CustomPersonalAccessToken extends Model
{
// Add you customisation here
}

Related

Customize Laravel auth Login

I need to customize the Login native code of the Laravel Authentication. I started by overrode some methods on LoginController such us credentials and validateLogin adding a field.
But I need to add some other checks, such us the possibility to join with an other table and other code, before to login the user but I didn't find solutions on internet.
I found some infos about the possibility to override the attemptLogin method or create a guard, but I didn't understand how do this.
In you LoginController you can just override the attemptLogin() any other method in AuthenticatesUsers trait depending on where your custom logic will make sense:
class LoginController extends Controller
{
use AuthenticatesUsers;
protected function attemptLogin(Request $request)
{
// add your logic here
}
}

Is there a way to add a phone authentication to the default laravel auth scaffolding

Please is there a way to add phone number authentication to the default laravel auth scaffolding?
In LoginController add this method
// for laravel v5.4+
public function username()
{
return 'phone_number'; // HERE WRITE YOUR FIELD NAME
}
// for older laravel versions
protected function getCredentials(Request $request)
{
return $request->only('field_email', 'field_passwd');
}
This will override your default trait's (AuthenticatesUsers) method for getting the custom username for Authentication.
Also don't forget to make that field in migration as "unique" like this:
$table->string('phone_number')->unique(); // HERE WRITE YOUR FIELD NAME
This will optimize your DB structure and speed up your auth system.

Laravel Passport custom hasher to create token

I have used a SHA1 hash for a password like this:
https://arjunphp.com/laravel-5-sha1-encryption-instead-of-bcrypt/
Now I am using passport API to create a token, but it is not allowing me to create a token as the hasher is changed now.
Symfony\Component\Debug\Exception\FatalThrowableError: Argument 1 passed to Laravel\Passport\Bridge\UserRepository::__construct() must be an instance of Illuminate\Hashing\HashManager, instance of App\Libraries\ShaHash\SHAHasher given in file C:\xampp1\htdocs\coursekartv2\vendor\laravel\passport\src\Bridge\UserRepository.php on line 26
How can I override UserRepository to use SHAHasher instead of HashManager? Or any other help to overcome this issue.
we came across the same problem as the one described here, I am working on a Laravel API that needs to handle Passport and also has it own custom Hasher (SHA1). like the one in here
Our fix for this was not only make our class ShaHasher extends HashManager
like so:
class ShaHasher extends HashManager implements Hasher { ..... }
you also need to make sure that your provider for this Hasher get and instance of the $app container in the constructor like so:
<?php
namespace App\Providers;
use Illuminate\Hashing\HashServiceProvider;
use Psytech\ShaHasher;
class ShaHashServiceProvider extends HashServiceProvider {
public function register()
{
$this->app->singleton('hash', function () {
return new ShaHasher($this->app);
});
}
}
Hope this helps someone!
Found the solution:
I was using my custom hasher (SHAHasher) instead of Passport hashManager, now extend HashManager of passport instead of complete new hasher (SHAHasher). So now even i am sending SHAHasher (custom) it is accepting as my SHAHasher extends hashManager.
Extend hashManager inside custom hasher library.
You can also create a custom hasher and extend the existing HashManager with your custom hashing using the extend method on the HashManager
In the AuthServiceProvider:
$this->app->get('hash')->extend('custom_hasher', function(){
return new CustomHasher();
});
In the hash.php config file you can then change the hash driver:
'driver' => 'custom_hasher',

Checking for Roles & Permissions in zizaco/entrust laravel package

I'm using zizaco/entrust package in my laravel project beside multi Auth package name Hesto/multi-auth
Our project on laravel 5.4,
i get below error when i want to get current logged in users' role and permissions with this method:
Entrust::hasRole('role-name'); OR Auth::user()->hasRole('role-name');
But I can access users' Role with this method for example :
$user = User::find($userid);
dd($user->hasRole('admin')); // Return true
i followed exactly installation instruction but i get below error :
`Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically`
How can i solve my problem,
Thanks in advance
In this error message you have answer to your problem:
Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically
You called this method hasRole() statically, but this method is non static. It means that you need to create object of this class, but you instead that used a class.
In the example that you gave:
$user = User::find($userid);
dd($user->hasRole('admin')); // Return true
you create an object of class User, and class User (I think) implements class Entrust:
$user = User::find($userid);
$user is an object and it`s not static, you can use hasRole().
In other words, to use method hasRole() (literally - is someone has a role?) you need this someone:
$user->hasRole('admin') // Is this user has role `admin`?
Hope, I explained it. Sorry about my English (I'm just studying).
In your User.php file add EntrustUserTrait like
<?php
namespace App;
//...
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
use Notifiable, EntrustUserTrait;
//...

Laravel 5.2, friendship model, how to add correctly in the User model?

I try to implement this solution to Laravel 5.2,
I did all installation steps, but I can't get it to work, and for example I want to use $user->getFriends();, I can use it directly from view or only from constructor?
And where I must setup a model, on User.php or I can do it in Friend.php?
You want to add the Friendable trait to your User model.
use Hootlex\Friendships\Traits\Friendable;
class User extends Model
{
use Friendable;
...
}
Then, if you've installed everything and migrated the database, you can access friendships like below:
$user = new User::find(1);
$recipient = new User::find(2);
// both `$user` and `$recipient` are instances of your `User` model
// no `Friend` model needed.
$user->befriend($recipient);
// now `$user` has just friended `$recipient`
// you should be able to access all the methods provided by the trait.

Resources