Set Default controller in usersystem [codeigniter] - codeigniter

I want to create a simple user system in codeigniter. I have created two controller called admin and login. When user visit my website i.e http://example.com it shows the login page if the user is not logged in. After that when user logged in it redirects to admin page.
But I want to know which controller I should make as a default controller admin or login?
N.B- I am new in codeigniter. I know how MVC works.

your default controller should be login.
In application/config/routes.php,
$route['default_controller'] = "login controller name";
In login controller's constructor, check if the user is already loggen in'
if(user alerady logged in){
redirect("admin controller's name");//redirect to admin controller
}

In application/config/routes.php
Replace
$route['default_controller'] = "welcome";
To
$route['default_controller'] = "login"; //or whatever you want

Related

Laravel's email verification redirecting to login page

I am having a problem with Laravel 5.7 Email verification.
After using Laravel's email verification it is forcing me to the login page if I'm not logged in.
Here is what we need:
We enable the code for the email verification of users. So when someone signup we want to verify the user email. We want the user to signup on the website, the user is asked to verify the email address and they can't do anything further until verified - which is ok for me.
Our trouble is, if a new user comes (Not registered) to our website, our website will force all that user to the login page as well .
A normal user who is not signed up is also getting to login page and force to verify or log in.
While we want the only user who signed up needs to verify. Which is working.
All the normal users who are not subscribed can use site easy. Where currently they are going to login page
What I've done so far
Added the following code
class User extends Authenticatable implements MustVerifyEmail
Auth::routes(['verify' => true]);
Route::get('profile', function () { })->middleware('verified');
After Verifying Emails
protected $redirectTo = '/dashboard';
It is working fine but,
What I need is that I don't want to force users to verify email because this is blocking the user from accessing the home page of my website.
The problem is you need to specify what pages will use auth middleware. To exclude your welcome view.
In your controller file.
public function __construct()
{
$this->middleware('auth')->except('welcome');
}
public function home(){
return view('welcome');
}
Im using 'welcome' view, because I believe that you do not change the code of default Laravel installation, you must be careful, since the view 'home' is the default page that laravel shows after you logged in. If you remove the authentication layer of that page, any user can access your system. You must change the code of this page or create another view.
In your web.php file
Route::get('/', 'HomeController#home');
Laravel Docs - Controller Middleware
try to add except method and check for url home
public function __construct()
{
$this->middleware(['auth', 'verified'])->except('home');
}

Redirect out of login page if session alive

in my laravel application, there is something strange happening, i had the idea when a user have a active session, some how the app didnt let the user go the the login page form. For example if im logged in to my app, and then go to login page, i expect that im redirect to the dashboard since i have a live session.
What is wrong?
My routes:
// Login and Dashboard route
Route::get('/', 'PagesController#getIndex');
Route::get('dashboard', 'MainController#getDashboard');
//Authentication Routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
My laravel version is 5.2
You can control the redirect after login in app/Http/Controller/Auth/AuthController.php by changing the $redirectTo string.
protected $redirectTo = 'dashboard';
You can control the redirect from /login when you have an active session in app/Http/Middleware/RedirectIfAuthenticated.php. If you want to show the login page with an active session you could comment return redirect('your/route');

Asp.net identity authentication - replace homepage with log in page & only logged in user can access

how to implement Asp.net identity authentication - replace homepage with log in page & only logged in user can access?
So if i new a mvc project. it has a homepage with home, about, contact & register link on top right. But i want a log in page to be the first page and only logged in user can access to the homepage mentioned above. and will have it done with asp.net identity. How to start? Thanks.
You need to set the default route such that it goes to login controller instead of Home controller. Please see the sample code belowl
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Login", action = "Your Action", id = UrlParameter.Optional},
);

5.2 Out of the box authentication customization

I'm new to laravel framework and I used the laravel-5.2 out of the box user authentication feature for creating a user authentication system. It is working fine. But when I want my home page to be displayed not the login page as the root. That is I want to access the login via the home page not the login page first. How can I customize my routes.
Just make sure that your home page route is not protected by the auth middleware. The default auth scaffolding provided by Laravel generates the following "home" route:
Route::get('/home', 'HomeController#index');
Inside the HomeController, in the constructor, is the following statement:
public function __construct()
{
$this->middleware('auth');
}
This means that any route handled by this controller is subject to the auth middleware: if you're not logged in, you'll be redirected to the login page. So, if you don't want your "home" route to be protected by auth, you need to either remove the auth middleware from the HomeController, or create a new controller to handle your "home" route, one which does not use the auth middleware.

Manage Login Redirection in Pyrocms

I need to manage login in such a way that it should redirect the control after successful login to the page which call login method in pyrocms.
By default it return control to Home Page. for example i want to go gallery page but it require user to be logged in so it will redirect control to the login page and now i want to redirect the control back to the gallery page once the user successful logged in.
Finally, i have come with the exact solution which is working correctly for me.
Whenever user try to view the gallery page(restricted page) which require user login, we have to only assign the URL where we want to redirect after successful login in $redirect_to in the controller method:
$this->session->set_userdata('redirect_to',$redirect_to);
Then it will automatically redirect the control to the desired page. Because in the users controller the login function is developed in such a way that:
$redirect_to = $this->input->post('redirect_to') ? $this->input->post('redirect_to') : $this->session->userdata('redirect_to');
Hopefully this will help you sometime

Resources