Redirection Issue using Auth Component - php

I have my home screen with Login Form and Registration button at the following location -
http://localhost/myproject/user/login
When user clicks on Register button, the page is re-directed to -
http://localhost/myproject/user/register
where I have Login Form on the top of the page and Registration Form at the bottom. Now when I fill the login form and submit, if the login fails, user is redirected to
http://localhost/myproject/user/login
instead, I want the user to be redirected to
http://localhost/myproject/user/register
How can I achieve this behavior?

I would do something in your Users_Controllers like so...
NOTE: I have not tried it and I am not sure it works... I can repost later after trying this...
First you need to turn Auto Redirect to False
class UsersController extends AppController{
//...
function beforeFilter(){
parent::beforeFilter();
$this->Auth->autoRedirect = FALSE;
}
function login(){
if( !(empty($this->data)) && $this->Auth->user() ){
//...
}else{
//If useR didnt/cant login redirect to register
$this->redirect(array('controller' => 'users', 'action' => 'register'));
// Or if you have routes setup for /register
$this->redirect('/register');
}
}
Hopefully this helps you out. If not I will try some things later and repost
}

If you add code examples, we might be able to better help you.
It sounds like you have one of the two issues:
You have forms or submit buttons with the same ID's or names.
Your forms are posting to the same place.

When the login fails, are you really redirected to /user/login ? Or is the login form action url always pointing to the /user/login page, meaning that if the login fails, you just stay on this page because you are already on it when you check the credential ?
If I'm right, you could obtain what you want by checking the referer in the /user/login action when the login fails and in this case redirect to /user/register if the referer is /user/register.

Turn of autoRedirect in beforeFilter and redirect manually
function beforeFilter()
{
parent::beforeFilter();
$this->Auth->autoRedirect = false;
}
function login()
{
if ($this->RequestHandler->isPost()
{
if ($this->Auth->user()
{
$this->redirect($this->Auth->redirect());
}
else
{
$this->redirect('/users/register');
}
}
// ...
}

Related

Laravel session handling to redirect without login

hi i have a login form and when user logins only it should open the page i.e if the user directly enters url it should redirect back to login page.
In laravel i read there is Auth::check() but i am using auth.php for different login and that is working fine. but i created new login page since i cant use auth.php how can i proceed to see if session is set or not , if not redirect to login page in laravel;
<?php
class TelecallController extends BaseController {
public function __construct() {
$this->beforeFilter(function () {
if(!Session::has('telecall_id'))
{
return Redirect::to('/8032/telecalls/login');
}
}, array('except' => array('')));
}
}
But i am getting the following error
ERR_TOO_MANY_REDIRECTS
The error you get is quite simple, look:
if(!Session::has('telecall_id'))
{
return Redirect::to('/8032/telecalls/login');
}
Result of this !Session::has('telecall_id') is TRUE at the beginning.
When you go to return Redirect::to('/8032/telecalls/login'); you made no changes in session value of telecall_id that is why you are redirected again and again.
After redirection you need to be sure that result of if(!Session::has('telecall_id')) is FALSE. So that - you need to set session value properly.
Here you have how to prepare session properly: https://laravel.com/docs/5.6/session

Redirect user to specific section/div of page after login/registration

Suppose a user is looking into some section of page and he is not logged in. He than logs in through the link given in the page. How can I redirect the user to specific section that he was reading.
Note: I have already redirected the user to initial page but still can't redirect to the specific section.
Use the intended() method:
return redirect()->intended();
This method reads the url.intended value from the session and if it exists, the method redirects a user to this URL. If not, by default it redirects a user to /
To make it work with a section, use JS to get full URL:
window.location.href
Then you could make an AJAX call to save current URL to the session manually with:
session(['url.intended' => url()->full()])
Or you could put it into a hidden input and then in a LoginController get it from a request and save it to the session.
The way i did it:
In App\Http\Middleware\Authenticate.php i update the handle function like this:
public function handle($request, Closure $next)
{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect('login')->with(['lastUrl' => $request->url()]);
}
}
return $next($request);
}
The important part is return redirect('login')->with(['lastUrl' => $request->url()]);. That way i got the intended url the user tried to access before login so when i login i just redirect the user to the url he tried to access.
In case the user didn't try to access any page he is just redirected to the default welcome page.

Force login after ajax request

im using cake php 2.4 my problem is sometimes after session idle, or logout in other tab the ajax request doesn't work because the user is already logged out.
my question is how i can verify that to redirect the user to the login page.
my function that im using to filter requests is used on my userscontroller class
//class UsersController
public function beforefilter(){
parent::beforeFilter();
// Allow users to logout.
$this->Auth->allow('logout');
}
thank you
if (!$this->Session->read('Auth.User')) {
// redirect here
}

Codeigniter failed login direct access to controller

i have login form, and then i try to access the controller directly, it works ! how do i prevent this access ?
i got some class
class C_home extends CI_Controller{
public function __construct() {
parent::__construct();
$this->session->set_userdata('islogin'); //to set session islogin
}
function index()
{
if ($this->session->userdata('islogin') != TRUE)
{
redirect('c_home','refresh'); //caused infinite refresh
}
redirect('c_login', 'refresh');
}
}
then i try to direct access controller, the page show infinite refresh, i want the page to show the login form
how do i resolve this ?
A couple of comments:
On the $this->session->set_userdata('islogin'); line, you should pass a 2nd argument which is the value to be assigned (presumably, TRUE is what you meant to put)
I think your redirect lines are the wrong way around. If the user isn't logged in, then you want to redirect to login. Now what your code does is redirect to home if the user isn't logged in, hence the endless loop (since this code is in the home page!
The $this->session->set_userdata('islogin', TRUE); line should obviously be in your login controller, but I'm guessing you've put it here just for testing purposes?
I'd rather do this like so
class C_home extends CI_Controller {
public function __construct()
{
parent::__construct();
}
function index()
{
if ($this->session->userdata('islogin') != TRUE)
{
redirect('c_home/login','refresh'); // go for login
}
// do something for loged in users here
}
function login()
{
if ($this->session->userdata('islogin') == TRUE)
{
redirect('c_home','refresh'); // get back home
}
// perform some login logic here
// then, if successful
{
$this->session->set_userdata('islogin',TRUE);
redirect('c_home','refresh'); // get back home
}
// or else
// display login form here
}
Of course is always better to use third party login library like this one https://github.com/DaBourz/SimpleLoginSecure
You're supposed to access the controller, that is the point of them to control things. If you have specific functions you don't want accessed via URL then prefix the function name with an _ like _notForPublicFunction. As to the infinite refresh...
if(!$this->session->userdata('isLogin'))
{
redirect('c_login');
} else {
redirect('c_home');
}
What you need to do is set up a base controller that will look after the session for you and split your logged in controllers from your logged out ones via inheritance.
It is a common question on here how best to manage logged-in and logged-out states. Please refer to this answer for detailed explanation on how to do it.

cakePHP stuck on logging page with Auth

I have a problem here. I created a cakePHP application using the Bake feature of cakePHP. I baked my Model, my Controller and my views (with default index, add, edit, and view actions). I created a small table called users in my database which contains only three fields (id int auto_increment primary key, username varchar(15), password charr(40)). The problem that I'm having is that when I use the Auth component, I get stuck on the logging page forever (until I take it out). I have tried almost EVERYTHING in my functions login() and beforeFilter() with no success. Any idea please?
I use the Auth component like so in my Users Controller:
var $components = array('Auth');
I have tried this in my function beforeFilter(), but it does not work:
function beforeFilter() {
$this->Auth->autoRedirect = false;
parent::beforeFilter();
}
I have even tried to redirect right in my function login() like so:
function login() {
$this->redirect($this->Auth->redirect());
}
But when I do this I get error 310: TOO MANY REDIRECTS.
I cannot go to my index, or add, or view page. Help please?
In the code you provided, you don't seem to do anything to make the 'login' action reachable if you are not logged in yet.
function beforeFilter()
{
parent :: beforeFilter();
$this->Auth->allow('login');
}
If you don't do that, the login page is protected, making you redirected to... the login page, making you redirected to... the login page, making you redirected to... the login page, making you redirected to... the login page ;-)
This piece of code has you caught in an endless loop, because it keeps redirecting you back to the login page:
function login() {
$this->redirect($this->Auth->redirect());
}
Read up here on list of Auth component variables. Specifically the loginRedirect part. You need to put that in into your beforeFilter function:
function beforeFilter() {
parent::beforeFilter();
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'index');
}

Categories