Middleware redirect to if user have not permissions

I need solution @ndm . what should i need to change in code and how to user to redirect to back previous location with flash message if user have not permissions. I already built system role permission but now i just want to redirect user if not permission or authorized to access action. as you can seen above error throw identity is not authorized so my role permission working fine but i want to redirect user back user page instead you that error page, I want to do that all with middleware not from controller side… because my application is large so i do not want to write again and again same code in controller so better way to it handle via middleware. please helping me if you have any idea with code example