Hi
I have a website where you can upload images using vue2-dropzone. I have it working so it posts to an image controller, however now that I'm wanting to deploy the website I want the image controllers post method to be guarded by Auth. At the moment this is not guarded.
I feel like I'm missing the obvious answer as the vue component I'm uploading from is contained within my website. I'm struggling to find a way to authenticate the post axios call with the current user without creating a new sign in route from vue?
Any advice would be appreciated, I'm self taught and probably weakest in js, I've tried looking at the docs but I can't seem to find a full example. I receive a "Unauthenticated" response.
Working unauthed route --
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('image', [ImageController::class, 'store']);
authenticated error route
Route::post('image', function () { })->middleware(['auth:sanctum']);
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Vue component called on mount, csrf stuff not needed as I binned trying to use spa authentication
getToken(){
axios.get('/sanctum/csrf-cookie').then(response => {
this.xsrf = response.config.headers['X-XSRF-TOKEN']
axios.post('/tokens/create',{token_name: 'imageToken'}).then(response => {
let $token;
$token = JSON.parse(response.request.response)
var headers = {
"Accept": "application/json",
"Cache-Control": "no-cache",
"Authorization": "Bearer " + $token.token,
"X-XSRF-TOKEN": this.xsrf
}
this.config.headers = headers;
});
});
}