henryoladj's avatar

Getting 401 | Unauthorized after post update

I am getting a 401 Unauthorized page after trying to Edit the content of my website

Controller

public function update(Request $request, News $news)
    {
        if(auth()->user()->id !== $news->user_id){
            abort(401, "Please Login");
        }

        $this->validate($request,[
            'body' => 'required|min:20'
        ]);

        $news->update($request->all());

        return redirect()->route('news.show', $news->slug);
    }

Form

            <div class="news-form">
<form action="{{route('news.update', $news->slug)}}" id="postform" method="POST">
    {{csrf_field()}}
    {{method_field('put')}}
    <div class="news-subject">
    <b>Body:</b>
    </div>
        <textarea name="body" class="form-control my-editor">{{$news->body}}</textarea>
<script>
  var editor_config = {
    path_absolute : "/",
    selector: "textarea.my-editor",
    height : "300",
    plugins: [
      "advlist autolink lists link image charmap print preview hr anchor pagebreak",
      "searchreplace wordcount visualblocks visualchars code fullscreen",
      "insertdatetime media nonbreaking save table contextmenu directionality",
      "emoticons template paste textcolor colorpicker textpattern"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media",
    relative_urls: false,
    file_browser_callback : function(field_name, url, type, win) {
      var x = window.innerWidth || document.documentElement.clientWidth || document.getElementsByTagName('body')[0].clientWidth;
      var y = window.innerHeight|| document.documentElement.clientHeight|| document.getElementsByTagName('body')[0].clientHeight;

      var cmsURL = editor_config.path_absolute + 'laravel-filemanager?field_name=' + field_name;
      if (type == 'image') {
        cmsURL = cmsURL + "&type=Images";
      } else {
        cmsURL = cmsURL + "&type=Files";
      }

      tinyMCE.activeEditor.windowManager.open({
        file : cmsURL,
        title : 'Filemanager',
        width : x * 0.8,
        height : y * 0.8,
        resizable : "yes",
        close_previous : "no"
      });
    }
  };

  tinymce.init(editor_config);
</script>

Please help because it works fine on localhost

0 likes
14 replies
jlrdw's avatar

If it's working on local then check your case, Linux is case sensitive.

And maybe do some echo and dd to see what's happening.

henryoladj's avatar

How can i solve this? it is not even updating in the database on live server. I am hosting my Application on Nameserver

siangboon's avatar

is 401 thrown because of this?

if(auth()->user()->id !== $news->user_id){
            abort(401, "Please Login");
        }

have you check the value auth()->user()->id and $news->user_id?

henryoladj's avatar

Yes it is same user that made the post that wants to edit the post. How come it is throwing the error?

jlrdw's avatar

Are you sure all the data is on the live servers database.

Check that ID and make sure it's there.

Snapey's avatar

Most likely issue is that you are not getting the news item.

How is the $news->slug resolved to a specific instance of News?

Have you checked that you get the correct News item?

henryoladj's avatar

It works fine like this

    public function update(Request $request, News $news)
    {

        $this->validate($request,[
            'body' => 'required|min:20'
        ]);

        $news->update($request->all());
        return redirect()->route('news.show');
    }

But that means any user can edit any post that they did not make

Snapey's avatar

So users's id is not EXACTLY the same as user_id

Are they the same column type? (both unsigned int, or both unsigned Big Integer)

Snapey's avatar
Snapey
Best Answer
Level 122

so maybe drop the comparator from !== to != or change one of the columns to match the other

Snapey's avatar

Thats good, but check your discipline. You should not allow yourself to have different column types between primary key and foreign key.

siangboon's avatar

you should make the primary key and foreign key column type consistent.

Please or to participate in this conversation.