I don't Know why laravel doesn't regonize that the user that sending reqeust it logged in ?? Help

adev03

Ayoub

Posted on June 6, 2023

I don't Know why laravel doesn't regonize that the user that sending reqeust it logged in ?? Help

this is react code
const token = sessionStorage.getItem("token")
const response = await

axios.post("http://127.0.0.1:8000/api/cours", values, {
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          Authorization: `Bearer ${token}`
        }
Enter fullscreen mode Exit fullscreen mode

and this laravel block code

 public function store(CoursFormRequest $request)
    {
        if (!auth()->check()) {
            return $this->error("", "user is not loged in", 401);
        }
        $request->validated($request->all());
        $cours = new Course();
        $cours->title = $request->title;
        $cours->description = $request->description;
        $cours->Duration = $request->duration;
        $cours->language = $request->language;
        $cours->price = $request->price;
        $cours->category = $request->category;
        $cours->user_id = Auth::user()->id;
        $cours->image = "test";
        $cours->save();
        return new CoursResource($cours);
    }
Enter fullscreen mode Exit fullscreen mode

and this the route :
Route::group(["middleware" => ["auth:sanctum"]], function () {
Route::resource("/cours", CoursController::class);
});

the problem is with the Auth::user()->id
i get cannot read id on null
So helpp

💖 💪 🙅 🚩
adev03
Ayoub

Posted on June 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related