I'm using Laravel's Sanctum library to authenticate my API, but it just doesn't work and everything seems on purpose.
I'm creating a token after someone authenticates, and it goes intopersonal_access_token
table in database. And then I insert the token into metatag. Then I retrieve the content of the metatag with default javascript (it works) and set it as global authorization header to Axios inbootstrap.js
file.
When using the decrypted bearer token, this approach was working. But when I started to use the default token column onpersonal_access_token
, it doesn't work.
The way I define my token
protected function authenticated(Request $request, $user)
{
$user->access_token = $user->createToken('api-token')->plainTextToken;
return $request->wantsJson() ? response()->json($user, 200) : redirect()->intended('/students');
}
That works good. It inserts new token after someone authenticates.
The way I set token as content of metatag
<meta name="access-token" content="{{auth()->user()->tokens()->first()->token}}" />
I can see the token when I inspect, it is like this:fd34a554cc5a6f14004f8728735375f980d81053b97f719be5bd504647d786cf
The way I set the token as global header to Axios
let metatag = document.querySelector('meta[name="access-token"]').content;
window.axios = require('axios');
axios.defaults.headers.common['Authorization'] = `Bearer ${metatag}`;
There were no problem with that, until I switched topersonal_access_table
. That approach was working when I used a token like6|yqbI...
.
That is returning a 401 error right now, when I click to that I can see my token likeAuthorization: Bearer fd34a554cc5a6f14004f8728735375f980d81053b97f719be5bd504647d786cf
in Request Headers
When I use the hashed token as global header I got error.
But I've added this line to .env fileSANCTUM_STATEFUL_DOMAINS=localhost:8000
Since my localhost has a port, I needed to define it manually in .env file
That solution worked for me.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Vue is an open source JavaScript framework for building a creative interface. Integration with Vue in projects using other JavaScript libraries is simplified because it is designed to be adaptable. Over 36,000 websites currently use Vue. Companies like Stackoverflow, PlayStation, etc. rely on Vue for their UI sites.
https://vuejs.org/
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.