DEV Community

Ayoub
Ayoub

Posted on

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

Top comments (1)

Collapse
 
timoye profile image
Timothy Soladoye

I can see you are using Sanctum for authentication. did you issue a valid token on your /auth or /login endpoint?