简体   繁体   中英

How can I validate a token?

I have a laravel passport api, on my front end that is in react I created a private route, in this private route I called a checkAuth function, to know the user is authenticated, but for now I did this function passing only true or false to To test, as I am a beginner, when I tried to check if the token is true or not, I ended up crashing, because I do not know how to do or where to start, I would need a practical example, if someone can help me, thank you!

Even checking username and password, generating the token and storing it in local storage is ok. I just need to check if this token is true or unchanged.

Just use auth:api middleware. This is just a simple routes file example.

Route::get('/validate-token', function () {
    return ['data' => 'Token is valid'];
})->middleware('auth:api');

Also you might want to checkout the Passport Documentation

You Can Add Middleware on the route directyly as mentioned in previous answer You can also use this condition

if (auth()->guard('api')->check()){
//do some thing
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM