mardi 28 mai 2019

Conditionally set a URL to redirect to after authentication

In my app, a post author can set an otherwise public post to private. If an unauthenticated user tries to visit that post, they will be prompted to login.

After they authenticate, I want to redirect them back to the original post URL, so they can read that private post.

This behavior is normally handled by Laravel's default auth middleware. However, because the posts are often public, I can't use that in this case.

Here's my current, non-functioning middleware:

    public function handle($request, Closure $next)
    {
        $post = $request->route('post');
        if ($post->isPrivate()) {
            $request->session()->setPreviousUrl($request->url());
            return redirect()->guest('login');
        }
        return $next($request);
    }

My hope is that I can set a custom URL to redirect to (/posts/{id}). However, when I try to login, I'm redirected to my default $redirectTo property (/dashboard).

Is this something that's feasible? Am I even thinking about this in the correct way?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire