Without generating Laravel default auth controllers, I create my own login and registration controller.
My routes look like these
For Login
GET /login ->> SessionController@create -->name = login.create
POST /login ->> SessionController@store -->name = login.store
For Registration
GET /registration ->> RegistrationController@create -->name = register.create
POST /registration ->> RegistrationController@store -->name = register.store
Everything is fine. Now I need one thing. Whenever I put auth middleware, a non-authenticated user will be redirected to my custom login page route to login.create.
You can extend Illuminate\Auth\Middleware\Authenticate middleware or just add a new middleware:
class AuthMiddleware
{
public function handle($request, Closure $next)
{
if (auth()->guest()) {
return redirect()->route('login.create');
}
return $next($request);
}
}
If you want to use the built in auth middleware, all you need to do is create a route named login
when you use auth middleware and the user is not logged in then an exception is thrown. This is caught in the ExceptionsHandler and in the case of Unauthenticated, the route named login is called or a json response is returned if applicable