I have laravel 5.2 app in which user is restricted to access /login
if user is already logged in. Now we have implemented functionality of "login in our app from other app (Not laravel)". Now problem is, when requesting app makes ajax call to get login, laravel 5.2 returns perfect response if user is not logged in before request. But after login, if requesting app request again for login, laravel app returns view of home page in response. I want to customize it and make user logout before processing request for login. I have spend too much hours over internet and debugging. But everywhere people asking to restrict login page. But my requirement is to allow request /login
with post method and customize response. If anyone knows answer, it will be appreciated.
Here is code of requesting app
$.ajax({
type: 'POST',
url: 'http://192.168.1.78/my-app/login',
data: {
st_email: 'xyz@xyz.com',
st_password: 'myPass'
}
}).success(function (response) {
// getting view in response if user is already restrict
}).error(function () {
console.log('error');
});
In your app/Http/Middleware/RedirectIfAuthenticated you have this function:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/');
}
return $next($request);
}
If you remove this if your app stop redirect you from /login. You can also just edit this function like that:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check() and ! $request->isMethod('post')) {
return redirect('/');
}
return $next($request);
}
this function redirect you only when you request isn't post method