0

I made a route like :

Route::get('free-download/{product}' , 'ProductController@freeDownload')->name('product.free')->middleware('auth');

This route check if the user is logged before calling freeDownload method. if not, the login form appear.

Then the user need to sign in and the login controller return back the home and the user need to click again on the button route ('product.free') in order to acces the route name "product.free".

There is a way to call ProductController@freeDownload method just after logged if the user clicked the button before ?

Hope i was more or less clear.

Here my login controller:

    class LoginController extends Controller
{
    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

Finally here my freeDownload method

public function freeDownload(Product $product){

    $user_download = UserDownload::where('user_id' , auth()->user()->id)->where('product_id' , $product->id)->exists();

    if(!$user_download && $product->file){

        $user_download = new UserDownload();
        $user_download->user_id = auth()->user()->id;
        $user_download->product_id = $product->id;
        $user_download->save();

        $product->downloads = $product->downloads + 1;
        $product->save();

        return Storage::disk('s3')->download($product->file);

    }else{

        Alert::error('Download error', 'file not found');
        return back();
    }
}
Mathieu Mourareau
  • 1,140
  • 2
  • 23
  • 47

1 Answers1

1

in the login controller use the intended method.

public function login(Request $request)
{
    if ($this->guard()->attempt(...)) {
        return redirect()->intended(route('home'));
    } else {...}
}

it will try to redirect to the intended page before being caught by the middleware. if it can't find the intended page, it will redirect to the home page.

N69S
  • 16,110
  • 3
  • 22
  • 36
  • thanks for your reply, this method will only redirect me to the route 'product.free' only if i request the route just before login ? what should i put on attempt ? – Mathieu Mourareau Jan 18 '19 at 08:30
  • yes, and the `attempt` is where you put the credentials to login the user. check your current AuthController – N69S Jan 18 '19 at 10:00
  • it's not working it bring my back to home but not to route('product.free'). the button is in my home view by the way. i didn't mention this. – Mathieu Mourareau Jan 18 '19 at 11:32