PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, February 27, 2022

[FIXED] CakePHP 3: How to automatically log a user in after registration

 February 27, 2022     cakephp, cakephp-3.0     No comments   

Issue

I am trying to log a user in using CakePHP 3 right after registration, but I have not been successful. This is what I am doing:

function register(){
    // ....
    if($result = $this->Users->save($user)){
        // Retrieves corresponding user that was just saved
        $authUser = $this->Users->get($result->id);

        // Log user in using Auth
        $this->Auth->setUser($authUser);

        // Redirect user
        $this->redirect('/users/account');
    }
}

Solution

I guess posting this question opened my eyes to a fix. This is what I did to get it to work... if there is better way, I would be glad to change it...

function register(){
    // .... Default CakePHP generated code

    if($result = $this->Users->save($user)){
        // Retrieve user from DB
        $authUser = $this->Users->get($result->id)->toArray();

        // Log user in using Auth
        $this->Auth->setUser($authUser);

        // Redirect user
        $this->redirect(['action' => 'account']);
    }
}


Answered By - AKKAweb
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing