1

I have this in my view

@if(auth.user.id == post.user_id)
            <div class="all-edit">
                <div class="post-body">
                    <a href="/posts/edit/{{ post.id }}">(Edit)</a>
                </div>
            </div>
            @endif

When user that made the post click it, then it takes them to the edit post page. But users that did not create post visit the url it shows the edit post page. i want them to be redirected to the home page instead,

Tobi Ferdinand
  • 137
  • 1
  • 12

1 Answers1

0

Answer on forum.adonisjs.com

You can create custom middleware.

Logic :

  1. Get post id with params object

  2. Get user from auth object

  3. Custom logic (if post owner)

    3.1. if false : redirect (with
    response.redirect())

    3.2. if true : call await next()


Get post :

const post = await Post.find(params.id)
crbast
  • 2,192
  • 1
  • 11
  • 21