# Cakephp 3.x updating user data

**URL:** <https://discourse.cakephp.org/t/cakephp-3-x-updating-user-data/1815>\
**Category:** Uncategorized\
**Created:** [January 4, 2017, 2:06pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-updating-user-data/1815 "2017-01-04T14:06:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chris1](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@Chris1](https://discourse.cakephp.org/u/Chris1)\
**Post date:** [January 4, 2017, 2:06pm UTC](https://discourse.cakephp.org/t/cakephp-3-x-updating-user-data/1815/1 "2017-01-04T14:06:49Z")

</div>

I have written a basic login script and now need to update the data stored in the auth component and then save it to the database, this is what i have so far;

> public function login()  
> {  
> if ($this-\>request-\>is(‘post’)) {  
> $user = $this-\>Auth-\>identify();  
> if ($user) {  
> $this-\>Auth-\>setUser($user);  
> $this-\>Auth-\>user()-\>last\_activity = date(“Y-m-d”);  
> $this-\>Users-\>save($this-\>Auth-\>user());  
> return $this-\>redirect($this-\>Auth-\>redirectUrl());  
> }  
> $this-\>Flash-\>error(\_\_(‘Email or password is incorrect, please try again.’));  
> }  
> }  
> I’ve tried a few different variations but can’t get any to work. Any ideas?

---

<div class="post-metadata">

**Author:** ![justrohu](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/justrohu/32/192_2.png) [@justrohu](https://discourse.cakephp.org/u/justrohu)\
**Post date:** [January 5, 2017, 6:34am UTC](https://discourse.cakephp.org/t/cakephp-3-x-updating-user-data/1815/2 "2017-01-05T06:34:39Z")

</div>

I think this is what you are looking for …

```
public function login()
{
	if ($this->request->is('post')) {
		$user = $this->Auth->identify();
		if ($user) { 
			$this->Auth->setUser($user);
			$this->request->session()->write('Auth.User.last_activity', date("Y-m-d"));
			$updateData = ['last_activity' => date("Y-m-d")]; 
			$this->Users->query()->update()->set($updateData)->where(['id' => $user['id']])->execute();
			return $this->redirect($this->Auth->redirectUrl());
		}   
		$this->Flash->error(__('Email or password is incorrect, please try again.'));
	}
}
```
