获取身份验证用户名列

huangapple go评论64阅读模式
英文:

Getting the Authentication username column

问题

public $username_column = config('auth.username_column');

或者

public function getUsernameColumn()
{
    return config('auth.username_column');
}
英文:

I need to get the auth username that is used by the app for authentication from a User object. The column used for authentication is changing based on system/environment (can be email or username or some other column)

Is it a good idea to put that as a public property in the User model (I will add username_column to auth.php):

class User extends Authenticatable 
{
    public $username_column = config('auth.username_column')
}

or as a getter in the User model:

class User extends Authenticatable 
{
    public function getUsernameColumn()
    {
        return config('auth.username_column');
    }
}

Or something else?

Edit: For more context, I get the User object in a Illuminate\Auth\Events\Login event's Listener:

public function handle(Object $event)
{
    // $user = $event->user
}

and I want to get the value of the column that is used by the app to authenticate the user (and it can change - sometimes username, sometimes email, and sometimes other custom column)

答案1

得分: 1

在我的观点中,你不应该将你的列名放在配置中。身份验证标识符不是我们经常更改的东西,所以不要将标识符放在公共属性中。Laravel Authenticatable 提供了一些方法来实现你的自定义列。

在处理身份验证或登录过程的控制器中,内部有一个名为 username() 的方法,用作 Auth::attempt() 过程中将要使用的列名。

public class AuthController extends Controller
{
    use AuthenticatesUsers;

    public function username()
    {
        return 'username_column';
    }
}

当用户已经经过身份验证时,有两种选项可供使用:

选项1
我们可以覆盖 Authenticatable 特性以获取标识符和值。

  • getAuthIdentifierName() 将返回用户名列的名称
  • getAuthIdentifier() 将返回用户名列的值
class User extends Authenticatable
{
    public function getAuthIdentifierName()
    {
        return 'username_column';
    }
}

/* 获取 Auth 用户名 */
Auth::user()->getAuthIdentifier();

选项2
您可以在不覆盖可验证方法的情况下使用 Auth 标识符进行调用。

User::find(Auth::id())->username_column;

更多信息:

英文:

In my opinion, you shouldn't put your column name in the configuration. An authentication identifier isn't something that we change regularly, so don't put the identifier in the public property. Laravel Authenticatable has provided some methods to implement your custom column.

On the controller that handles the authentication or login process, inside the AuthenticatesUsers trait, there is a method named username() as the column name that will be used for the Auth::attempt() process

public class AuthController extends Controller
{
    use AuthenticatesUsers;

    public function username()
    {
        return 'username_column'
    }
}

When the user has been authenticated, 2 options can be used:

Option 1:
We can override the Authenticatable trait to get the identifier and the value.

  • getAuthIdentifierName() will return the name of the username column
  • getAuthIdentifier() will return the value of the username column
class User extends Authenticatable
{
    public function getAuthIdentifierName()
    {
        return `username_column`
    }
}

/* Get the Auth username */
Auth::user()->getAuthIdentifier();

Option 2:
You can call using the Auth identifier without overriding the authenticable methods.

User::find(Auth::id())->username_column;

More information:

huangapple
  • 本文由 发表于 2023年7月6日 17:43:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定