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();
}
}