You cannot rely on having an authenticated user at that point in the lifecycle, so you need a fallback. Usually, we fallback to IP address using the null-safe object operator ?-> to guard against that error you are experiencing, e.g.
reurn Limit::perHour(10)->by($request->user()?->id ?: $request->ip())
Whenever we have an authenticated user, that ID will be used, otherwise it will use their IP address for guests.