Code to log user in after registration

Hi,

Im trying to get a user to be logged into the Authentication componant automatically after they register. Ive tried looking for examples but cannot find any upto date.

im currently using CakePHP 4.x

if anyone can help that would be great.

Thank you for pointing me in the right direction.

Here is my code for anyone else that may have the same issue

	public function register()
	{
        $this->loadModel('Users');
		$user = $this->Users->newEmptyEntity();
        if ($this->request->is('post')) {
			
			$formdata = $this->request->getData();
			$formdata['role'] = 'user';
            
			$user = $this->Users->patchEntity($user, $formdata);
			
            if ($result = $this->Users->save($user)) {
				$this->Authentication->setIdentity($result);
				$this->Flash->success(__('The user has been saved.'));
                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The user could not be saved. Please, try again.'));
        }
        $this->set(compact('user'));
	}