Laravel Sanctum返回:调用null上的createToken()成员函数。

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

Laravel Sanctum return: Call to a member function createToken() on null

问题

I am following sanctum documentation from here. I did all same as described but still it is not working.

我正在按照此处的 sanctum 文档进行操作。我按照描述的步骤进行了操作,但仍然无法正常工作。

I am getting an error when I am generating new token "Call to a member function createToken() on null".

当我生成新令牌时,我遇到了一个错误 "Call to a member function createToken() on null"。

Here is my code on github https://github.com/farukhaleem/sanctum

这是我的 GitHub 代码链接:https://github.com/farukhaleem/sanctum

I am trying to use sanctum as it is described in the documentation. I am getting createToken() method for generating a token but I am unable to get anything in $request->user(). I am using Thunder Client in VSCode for getting the result. http://127.0.0.1:8000/api/tokens/create is the endpoint, method type is POST. FORM data has token_name with value 'data'.

我正尝试按照文档中的描述使用 sanctum。我正在使用 createToken() 方法来生成令牌,但是我无法在 $request->user() 中获取任何内容。我正在使用 VSCode 中的 Thunder Client 获取结果。http://127.0.0.1:8000/api/tokens/create 是端点,方法类型是 POST。FORM 数据中有一个名为 token_name 的字段,其值为 'data'。

英文:

I am following sanctum documentation from here. I did all same as described but still it is not working.
I am getting an error when I am generating new token "Call to a member function createToken() on null".

use Illuminate\Http\Request;

Route::post('/tokens/create', function (Request $request) {
    $token = $request->user()->createToken($request->token_name);
 
    return ['token' => $token->plainTextToken];
});

Here is my code on github https://github.com/farukhaleem/sanctum

I am trying to use sanctum as it is described in the documentation. I am getting createToken() method for generating a token but I am unable to get anyting in $request->user(). I am using thunder client in vscode for getting result. http://127.0.0.1:8000/api/tokens/create is the endpoint, method type is POST. FORM data has token_name with value 'data'

答案1

得分: 0

文档假设您有经过身份验证的用户,在您的情况下没有。这就是为什么 $request->user() 为 null。

所以,为了生成一个令牌,您可以使用以下方式:

Route::post('/tokens/create', function (Request $request) {

    $user = User::find("some user id");
    $token = $user->createToken($request->token_name);
 
    return ['token' => $token->plainTextToken];
});

由于您在学习阶段,将您的路由转换为带有表单的登录会更有意义,用户可以在其中输入他们的电子邮件和密码,然后在提交时生成令牌。

Route::post('/login', function (Request $request) {

    # 进行任何验证

    $user = User::where('email', $request->email)->first();
    $token = $user->createToken("The user agent here");
 
    return ['token' => $token->plainTextToken];
});

还请注意,在 createToken 上最好传递用户代理,这样您就知道使用该特定令牌的应用程序。

英文:

The documentation assumes you have an authenticated user in your case you don't. this is why $request->user() is null.

So in order to just generate a token you could use the following

Route::post('/tokens/create', function (Request $request) {

    $user = User::find("some user id");
    $token = $user->createToken($request->token_name);
 
    return ['token' => $token->plainTextToken];
});

Since you are learning, it would make more sense if you transform your route into a log in with a form where the users can enter their email and password so on submit you generate the token.

Route::post('/login', function (Request $request) {

    # do any validations here

    $user = User::where('email', $request->email)->first();
    $token = $user->createToken("The user agent here");
 
    return ['token' => $token->plainTextToken];
});

Also note that on createToken it would be better if you pass the user agent. so you know the app that uses that specific token.

huangapple
  • 本文由 发表于 2023年6月29日 18:00:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580004.html
匿名

发表评论

匿名网友

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

确定