Jump to content

Failed login for 'guest' - Guest user may not login


blacksrv
 Share

Recommended Posts

Hi, I'm having problems with a custom login.

I test almost everything I found in the forum:

$this->users->setCurrentUser($user);
$this->session->forceLogin($user);
$this->session->login( $name, $pass );
$this->session->_user_id = $user->id;
Nothing works, I always get a failed login for guest.
 
The $user exists, is created I can track the id and everything.
 
Help needed!!!!
 
Thanks
Edited by LostKobrakai
Added Codeblock
Link to comment
Share on other sites

The "guest" user can't login - simple as that really :) That account is just to determine what access all non logged in users have to the site.

 

Is not a guest user, is a brand new user (or an existing one):

$name = $user->name;
$user->of( false );
$user->pass = $pass;
$user->save();
$this->session->login( $name, $pass );
Link to comment
Share on other sites

You can't get the password like that and use it to log in - it is encrypted. What you need is:

$session->forceLogin($user)

Read more about it here:

https://processwire.com/blog/posts/processwire-2.6.8-brings-new-version-of-reno-admin-theme-and-more/#new-session-gt-forcelogin-user-method-to-login-user-without-a-password

PS Sorry, I see that you already tried the forceLogin option. 

Link to comment
Share on other sites

I think you need to give some role to the user you are trying to log-in, otherwise they will just get the guest role (and they can't be logged in). See http://processwire.com/api/variables/user/#examples

Also beware of using $user as your own variable, as you risk overwriting the built-in $user object.

Actually I just tested and I can successfully use forceLogin to login a user with just a guest role. The only time I can replicate that error is if I try to login the user named "guest".

Keep in mind that:

$this->session->forceLogin($user);

will try to login the current user in the $user object, which will be the "guest" user.

You need to specify the name of the user you are trying to login:

$this->session->forceLogin('testuser');

or get the user object first, like:

$u = $users->get('testuser');
$this->session->forceLogin($u);

Remember it's never a good idea to overwrite $user.

  • Like 1
Link to comment
Share on other sites

@Adrian & @DaveP, thank you for the time, I'll try to clarify a few things:

The error comes from the Social Login plugin that I modify myself, I didn't change that but I already encountered that error in another page that I made and has nothing to do with social login.

I'll put the complete code to explain that I already have the $user:
 

$social_id = $provider_name . '-' . $user_profile->identifier;
$display_name = $user_profile->displayName;

$user = $this->users->get( "social_id=$social_id" );

// Create a random pass to use in login
$pass = self::passwordGen();

if ( $user->id ) {
$name = $user->name;
// User has logged in earlier, change its password
$user->of( false );
$user->pass = $pass;
$user->save();
} else {
if ( property_exists( $user_profile, 'emailVerified' ) ) {
$name = $this->sanitizer->pageName( $user_profile->emailVerified );
} else {
$name = self::passwordGen();
}
// Create the user
$user = new User;
$user->name = $name;
$user->pass = $pass;
if ( property_exists( $user_profile, 'email' ) ) {
$user->email = $user_profile->email;
}
$user->social_id = $social_id;
$user->oauth = serialize( $user_profile );
$user->addRole( self::socialLoginPagePath );
$user->roles->add( $this->roles->get( "guest" ) );
$user->roles->add( $this->roles->get( "members" ) );
$user->activation_key = "0";
$user->save();
}
//$this->session->forceLogin($user);
$this->session->login( $name, $pass );
//$this->users->setCurrentUser($user);
Link to comment
Share on other sites

Well, I don't know what passwordGen() returns so it's a bit hard to tell what's going on. Have you checked the values of $name and $pass right before you try to log them in?

Regardless, as I mentioned above, you shouldn't be overwriting $user - try $u or similar.

Also, please consider indenting your code here - it's hard to follow like that :)

  • Like 1
Link to comment
Share on other sites

passwordGen is just a pass generator, is working. 

In my code I delete a commented line with an echo with the name and pass.

Was indented in my paste, I'll check next time.

Edit:

I have the same problem in the Forgot Password Module, according to the log: 

2016-03-01 21:05:39 guest social-login/?provider=facebook Error: Failed login for 'guest' - Guest user may not login

Link to comment
Share on other sites

If it's just a password generator, then why are you assigning $name to its results?

$name = self::passwordGen();

I suggest you start by taking the output of $name and $pass (from that echo line) and in a template file try the login command there to make sure it is working.

I would also like to know that the new user is definitely being created and the roles you want are being successfully added.

I still keep coming back to that initial error - it suggests to me that the user you are trying to login is "guest" which suggests there is something wrong with $name or $user depending on which you try. It also might be related to overwriting $user - have you tried using $u ?

  • Like 1
Link to comment
Share on other sites

If it's just a password generator, then why are you assigning $name to its results?

$name = self::passwordGen();
I suggest you start by taking the output of $name and $pass (from that echo line) and in a template file try the login command there to make sure it is working.

I would also like to know that the new user is definitely being created and the roles you want are being successfully added.

I still keep coming back to that initial error - it suggests to me that the user you are trying to login is "guest" which suggests there is something wrong with $name or $user depending on which you try. It also might be related to overwriting $user - have you tried using $u ?

First of all, thank your the help. 

Now I'm using $u as the var, but nothing changes.

What you are saying about $name = self::passwordGen(), is when facebook doesn't provide the email. I tested with a blank user and an existing one but it's the same.

I'll try to do a re write and see where I made the mistake.

Edit: I have no idea why this code is no working:

$u = $users->get( "4930" );
if ( $u->id ) {
  $name = $u->name;
  $pass = 'Demo123';
  $login = $session->login($name, $pass);
  $session->forceLogin($u);
  $users->setCurrentUser($u);
}
I got 2 errors: guest http://localhost/ Error: Failed login for 'guest' - Guest user may not login (of course id 4930 exists), so it's trying 2 times.
Link to comment
Share on other sites

Firstly, this should be sufficient - you don't want to try logging in more then once.

$u = $users->get(4930);
if ( $u->id ) {
  $session->forceLogin($u);
}

The first part of That error message is coming from: https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L520

which shows that it is definitely receiving the $name populated as "guest". I realize that isn't what you are passing to it - so I am definitely confused :)

The second part is coming from:

https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L488

which checks this:

$user->id == $this->wire('config')->guestUserPageID

I'd love if you could echo out the contents of $name and $user in that login function (starting here: https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L435) to see what it is receiving and see if you can figure out at what point in that fuinction it is failing.

  • Like 1
Link to comment
Share on other sites

Firstly, this should be sufficient - you don't want to try logging in more then once.

$u = $users->get(4930);
if ( $u->id ) {
  $session->forceLogin($u);
}
The first part of That error message is coming from: https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L520

which shows that it is definitely receiving the $name populated as "guest". I realize that isn't what you are passing to it - so I am definitely confused :)

I'm getting crazy with this, after 4 hours with the same I really don't have a clue.

Let see:

$u = $users->get(4930);
if ( $u->id ) {
    echo '$name: '.$u->name.' - id:'.$u->id;
    $session->forceLogin($u);
}
It returns:

$name: demo - id:4930

The second part is coming from:

https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L488

which checks this:

$user->id == $this->wire('config')->guestUserPageID
I'd love if you could echo out the contents of $name and $user in that login function (starting here: https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L435) to see what it is receiving and see if you can figure out at what point in that fuinction it is failing.

Ok, when I add on: https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L435

echo $name;
it return a "guest", so no $name is getting into that function.

Edit: Updated to 2.7.3 (was using 2.7.2) still the same.  :'(

Edit 2: echoed on 506 session.php returns correct id, but echo on 436 return 'guest'.

Edited by blacksrv
Link to comment
Share on other sites

I am not surprised that $user is undefined on 436 - it isn't passed to this function. Try echoing $user->id at 453. 

What about in here: line 505?

public function forceLogin($user) {
    echo $user->id;
    return $this->login($user, '', true);
}
Link to comment
Share on other sites

I am not surprised that $user is undefined on 436 - it isn't passed to this function. Try echoing $user->id it at 453. 

What about in here: line 505?

public function forceLogin($user) {
    echo $user->id;
    return $this->login($user, '', true);
}

Yes, I notice my error and correct it. It returns '4930'.

Link to comment
Share on other sites

Ok, what about getting $user->id and $name after this line:

https://github.com/ryancramerdesign/ProcessWire/blob/980ce4f0be2054dfbad4a7b334d35bdca42da7da/wire/core/Session.php#L440

We know that a $user object is making it into the login function so we need to figure out where it is getting messed up. Maybe it's not passing the:

if(is_object($name) && $name instanceof User) {

condition for some reason?

I hope I am not taking you on a wild goose chase here - this is pretty bizarre - I feel like I am missing something obvious.

Link to comment
Share on other sites

There is something weird happening, I'll start uninstalling plugins to see if something is messing up with that.

Thanks adrian for your help.

Yeah - it sounds like maybe something is hooking into Session::login()

I'd grep your site/modules directory for that hook.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...