How to redirect back to previous page in laravel 8 after Login

1k Views Asked by At

I have a blog where users can only comment after being logged in.

The Problem

After login, the page is redirected to the home page.

blog_details.blade

'''

@if(!Auth::check())
    <h3 class="news-details__title">Leave a reply</h3>

    <a class="thm-btn sidebar__tags-btn" href="{{route('login')}}">
        You must be logged in to post a comment.</a>

@else
<div id="app">
<div class="comment-one">
    <h3 class="comment-one__title">Comments</h3>
    <div class="comment-one__single">
        <div class="comment-one__image">
            <img src="assets/images/blog/comment-1-2.png" alt="">
        </div>

            <get-comments :userid="{{Auth::user()->id}}" :blogid="{{$blog->id}}"></get-comments>


    </div>
</div>


<div class="comment-form">
    <h3 class="comment-form__title">Leave a Comment</h3>
    <form action="#" class="comment-one__form contact-form-validated"
        novalidate="novalidate">

            <add-comment :userid="{{Auth::user()->id}}" :blogid="{{$blog->id}}"></add-comment>



    </form>
</div>
</div>
    @endif

'''

Question

What should do to be redirected back to the blog details page?

** I have tried**

Login Controller

 use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
   // protected $redirectTo = RouteServiceProvider::HOME;
    protected function redirectTo()
    {
        return url()->previous();
    }

Thank you for the assist

EDIT Custom Login Controller Shows the custom login. I have included my custom login controller, the 'index' method returns the login form and the 'customLogin' checks user table and validates, How do I redirect back to the previous method?


<?php

class CustomAuthController extends Controller
{

    public function index()
    {
        return view('auth.login');
    }


    public function customLogin(Request $request)
    {
        $request->validate([
            'phone_number' => 'required',
            'password' => 'required',
        ]);

        $credentials = $request->only('phone_number', 'password');
        if (Auth::attempt($credentials)) {
            return redirect()->intended('/')
                        ->withSuccess('Signed in');
        }

        return redirect("login")->withSuccess('Login details are not valid');
    }


}

0

There are 0 best solutions below