Laracasts mascots - robot, elephant, monster, computer.

This episode is for
subscribers only.

For the cost of a pizza, you'll gain access to this and hundreds of hours worth of content from top developers in the Laravel space!

13

Token-Based API Authentication

Episode
08
Published
Apr 16th, 2019
Run Time
13m 09s
Topic
Laravel
JeffreyWay's avatar
Your Instructor
thealberti1231's avatar
thealberti1231
6 years ago

How can a field be unique and nullable at the same time? Or did it change something.

SHMDesign's avatar
SHMDesign
6 years ago

Hello there,

How would you go about token-based API Authentication using a signing in or out mechanism?

So your SPA would have register/login/logout pages like a normal laravel app would provide out-of-the-box?

Loots's avatar
Loots
6 years ago

I would like to know this as well. But I suppose the best way is to generate the token when creating the account and then when loading the SPA, also pass the SPA the token when the user logs in. Or maybe it might be even better to regenerate a token every time a user logs in so others cannot copy your token.

In general, I'm also wondering if token based API authentication is safe enough...

QuentinRM's avatar
QuentinRM
6 years ago

Hi Jeffrey, thank you for this episode :)

I think it would be interesting to give a few indications how to automatically include the api_token when navigating through the different pages of the site.

In real life, a user wouldn't need to enter his token to get his achievements list. How to automatically include the token in the component when calling ?

Thank you !

Anaxarchos's avatar
Anaxarchos
6 years ago

@quentinrm A user would have to provide that token once, which then can be stored in a database. And each time she wants to access the achievement list, this token can be fetched from that database.

BoSkiv's avatar
BoSkiv
5 years ago

Is that api_token a magic variable in terms of Laravel ? if I create a field secure_api_token in migration table, will it still work ?

BoSkiv's avatar
BoSkiv
5 years ago

Ok, looks like that does not exists anymore in Laravel 7. :( And I shoud use Sanctum

Macclinson's avatar
Macclinson
5 years ago

BadMethodCallException with message 'Method Illuminate\Database\Eloquent\Collection::fill does not exist.'

Anyone why this is happening , i am following this lesson on Laravel 8 and i ran into this error playing around with tinker here is my code. after grabbing the particular user instance, $user->fill(['api_token' => Str::random(60)])->save()

JamLizzy101's avatar
JamLizzy101
5 years ago

I am attempting this using Laravel 8, and I am able to fetch the data at this point, but am stumbling on an error when outputting an incorrect token. I am not sure if I am missing something here...

Here is the script code in Achievements.vue below: -

export default {
    data() {
        return { achievements: [], token: '', message: '' };
    }, 
    methods: {
        fetchAchievements() {

            axios

            .get(
                `http://laracasts.test/api/achievements?api_token=${
                    this.token
                }`
            )
            .catch(error => {
                this.achievements = [];
                this.message = error.response.data.message;
            })
            .then(({ data }) => {
                this.achievements = data;
                this.message = null;
            });
        }
    }
        
}

Everything works, but I am still getting stuck with the error in the console: -

Uncaught (in promise) TypeError: Cannot read property 'data' of undefined.

Can anyone point me in the direction on where I am going wrong, or is this normal to still receive this error?

kroete's avatar
kroete
5 years ago

The curly braces around data?

borisoft82's avatar
borisoft82
4 years ago

instead of this.message = error.response.data.message; there must be this.message = error.response.data.error;

jawaid's avatar
jawaid
4 years ago

Token is used when user is logged in. How user would login in SPA wihtout a token?

Your avatar

You get a window into a seasoned developer's mind and workflow for the price of an Uber. I can't overstate the value of a Laracasts subscription.

Caleb Porzio — Creator of Livewire

Subscribe to unlock full access.