I am using Laravel 5.2 with angular js. And also I have added a middleware auth for all my URL's.
I have a URL like this http://localhost/myproject/public/#/resources.
When I directly run the above URL it will redirect me to the login page and after login, it redirects only to the home page. Because there is no URL assigned after public/. But I need to redirect to hash URL.
Here is my Authenticate middleware code.
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
$url = parse_url($request->url());
print_r($url["fragment"]);
//return $next($request);
}
I've tried to print fragment part of the URL. But I am getting nothing. Is ther any way to redirect with hash URL.
Related
In my Laravel application I have a middleware for specific routes, in this middleware I validate to redirect to a url.
public function handle($request, Closure $next)
{
$isValidated= ....
if ($isValidated) {
session()->put('url.intended', URL::full());
return redirect()->route('routeone')
}
return $next($request);
}
in the store method of that table when you register I do a redirect in the following way, but it turns out that when I do it, it redirects me to domain/img/favicon.png and I did not do the previous route
public function store(Request $request)
{
....
return redirect()->intended(session('url.intended') ?? '/admin');
}
What is the problem here or how could I address this detail to redirect to the url before the middleware redirects. ?
Try to use this code :
$referrer = $this->request->headers->get('referer');
$url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();
or directly :
$url = request()->headers->get('referer');
and
session()->put('url.intended', $url);
Could you not just do this
return back();
From the docs https://laravel.com/docs/5.7/redirects#creating-redirects
I have a Login form in navbar (as dropdown menus). And when a user gives incorrect login, laravel should redirect back page.
I want to if user incorrect login , laravel redirect route("login") to main login form page.
I change RedirectIfAuthenticated.php but not working
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
return redirect()->route("login");
return $next($request);
}
As result I getting error:
To many redirects
How change error login after redirect login page ?
There is a function sendFailedLoginResponse() which is called from login() function of Illuminate\Foundation\Auth\AuthenticatesUsers file. This file is included in App\Http\Controllers\Auth\LoginController. So you can change the redirection by using these files.
You are awesome. Thank you.
I override method ;
public function sendFailedLoginResponse(Request $request)
{
return redirect()->route("login")->withErrors([
$this->username() => [trans('auth.failed')],
]);
}
I'm having some issues with protecting routes in Laraval.
So one of my routes:
Route::get('/purchase-quote', ['middleware' => 'auth', 'uses' => 'PurchaseController#purchaseQuote']);
Lets say for example, I am on page /about and I click a link that takes me to /purchase-quote. Well it's doing what I want there, it's forcing someone to login or register before accessing the page.
The issue is that after they register, instead of continuing on and taking them to the intended /purchase-quote page, it takes them directly to the home page at /
This used to work the way I am needing it to, and all of a sudden it does not. If I do login instead of register, it works the way it should.
Will also include auth code here:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('register');
}
}
return $next($request);
}
I'm new to laravel 5. I just want know how to protect routes from invalid URL input. An example of an invalid input URL is shown below:
http://localhost:90/csadcsvs
If someone enters invalid URL from my laravel project, I just want to redirect to login page.
Works by adding
return redirect()->guest('auth/login')
instead of the below in app/Exceptions/Handler.php
return parent::render($request, $e)
It is used as shown below:
public function render($request, Exception $e)
{
if ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
}
return redirect()->guest('auth/login');
}
Route::get('/{any}','Pagecontroller#index');
any url which is not declared in the route will be accepted by {any} and the will be controlled back to index.
Hey create middleware using this LINK this is my auth middleware
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
}
return $next($request);
}
in here if you use this middle ware like this without sign in you can't access your url csadcsvsyou will redirect to login page this is the way protect url in laravel
Route::get('csadcsvs',['middleware' => 'auth', function () {
}]);
hope this will help you :)
I am trying to use Laravel 5 User Authentication. In this regard my URL is http://localhost/lara_project/public/auth/login before login. I would like to redirect user to http://localhost/lara_project/public/home after successful login. But it is redirect to http://localhost/lara_project/public/. I tried with routes like below
Route::get('home',['middleware' => 'auth', 'uses' => 'WelcomeController#index']);
Route::get('/', 'HomeController#index');
Route::controllers(['auth' => 'Auth\AuthController','password' => 'Auth\PasswordController']);
which are resided in routes.php
My Problem is afrer successful login user redirected to http://localhost/lara_project/public/ and if I would like to browse http://localhost/lara_project/public/home after successfull login it is showing error
This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
I tried by editing RedirectIfAuthenticated.php,compiled.php and AuthenticatesAndRegistersUsers files. But could not get any satisfactory result.
Could anyone help me in this regard ??
Go to Auth\AuthController and add a variable $redirectTo = 'home';
This with tell laravel where to redirect to after login.
Modify RedirectIfAuthenticated.php as below
public function handle($request, Closure $next)
{
if ($this->auth->check() && !$request->is('/home'))
{
return redirect('/home');
}
return $next($request);
}
Please also have a look at here. I am using some kind of code as below.
public function handle($request, Closure $next)
{
if (!$request->is('authenticate') && !$request->is('authenticate/login') && !Auth::check()) {
return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
}
if(Auth::check()) {
if( ! $request->user()->isAdmin() ) {
return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
}
return $next($request);
}
return $next($request);
}
Go to /app/Http/Controllers/Auth/LoginController.php
There you can change protected $redirectTo = RouteServiceProvider::HOME; that to what you want.