Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HasApiTokens tokens() method does not return token #25

Closed
popiciulo opened this issue Jan 10, 2020 · 3 comments
Closed

HasApiTokens tokens() method does not return token #25

popiciulo opened this issue Jan 10, 2020 · 3 comments
Assignees

Comments

@popiciulo
Copy link

popiciulo commented Jan 10, 2020

In my login controller I am using this code to log in the user

$credentials = $request->only('email', 'password');
        if (Auth::attempt($credentials)) {
            return Auth::user()->tokens()->first();
        }

and the result is like this without the token field:

{
    "id": 5,
    "user_id": 6,
    "name": "auth",
    "abilities": [
        "*"
    ],
    "last_used_at": "2020-01-10 18:21:19",
    "created_at": "2020-01-10 18:21:14",
    "updated_at": "2020-01-10 18:21:19"
}

Is there something that I missed or mistakenly used?

@popiciulo
Copy link
Author

popiciulo commented Jan 10, 2020

It seems I missed something and had to return it like this:
return Auth::user()->tokens()->first()->token;
BTW: I guess we could have duplicates like same user_id and name in the personal_access_token table. Would it be better to have unique('user_id', 'name') in order to prevent the creation of tokens with the same name for same user?

@amaelftah
Copy link
Contributor

@popica80 can't we just use the latest created tokens instead of using unique ?

@taylorotwell
Copy link
Member

taylorotwell commented Jan 15, 2020

The tokens are stored hashed in the database. You wouldn't return a token from your login route. I'm honestly not even sure what you are attempting to accomplish here. Are you authenticating your SPA, a mobile app? Have you read through the entire readme?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants