mercredi 7 mars 2018

How to redirect login plus previous URL in laravel

                                        Laravel 5.1

I'm trying to build this functionality for every method in my controller. And this is very unpractical to do and very difficult to maintain. How can I set this that is when I registered an auth middleware in a specific route then it will redirect into a login page together with the URL that trying to view/visit.

Example:
URL: public/users

If this URL will try to visit by an unauthenticated user, then the user will be redirected to a url like public/login?url=http://localhost/myproject/public/users

Then After the user loggedin successfully, the user then will be redirected into http://localhost/myproject/public/users

What I have now: (But I think not a good one to use)

public function getLoginURL(){
    if(!Input::get('location-login')){
        // URL is not set, redirect user to home.
        return redirect('/');
    }

    if(auth()->check()){
        // User is logged in. Go the specified URL (depends on the middleware of the url (route))
        return redirect()->to(Input::get('location-login'));
    }

    if(!auth()->check()){
        // Redirect user to a login with the URL provided
        return view('pages::auth.login.url');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire