<p>Not sure what other user info you're looking for, but Craft's <a href="https://craftcms.com/classreference/controllers/UsersController" rel="nofollow">UsersController</a> will only return <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/UsersController.php#L1601-L1608" rel="nofollow">two things</a> in the JSON response of a successful login for an AJAX request.</p>

<p>A <code>success</code> key set to <code>true</code> and a <code>returnUrl</code> key set to where the login redirect should go to.</p>

CC BY-SA 3.0
2016-10-17T20:22:06.013Z
so no way to get user Id and email?
Rockwell Rice
CC BY-SA 3.0
2016-10-17T20:29:02.737Z
You'd want to do an additional controller call upon successful login to get info about the currently-logged in user, with your own controller to return that info, if that is what you need.
Patrick Harrington
CC BY-SA 3.0
2016-10-17T20:32:22.613Z
O.k., so I need to create my own controller for this?
Rockwell Rice
CC BY-SA 3.0
2016-12-17T00:08:48.983Z
You could also just ajax load in a Craft template with `currentUser.id` and `currentUser.email` in it, in your success handler - if you want to avoid a controller for whatever reason.  But a controller for this would be trivial really.
Jeremy Daalder
CC BY-SA 3.0
2016-10-17T20:21:35.893Z
2016-10-17T20:21:35.893Z
Brad Bell
<p>So if i understand your question correctly what you are trying to achieve is to get used data of a user that is trying to log in. </p>

<p>Craft's login controller will call the userSessionService->login() method which in turn will call the loginByUserId method. This method has two events attached to it - the onBeforeLogin and the onLogin. </p>

<p>-- onBeforeLogin is called before anything is really created.
-- onLogin is called if the login was succesfull. </p>

<p>Now if you listed to the onLogin method you get the param username. Craft will already have validated this username and logged it in - so you know it is good. You can then call</p>

<p><code>craft()-&gt;users-&gt;getUserByUsernameOrEmail($yourUsernameVar);</code></p>

<p>Which will return a populated UserModel. Hope it helps in some way</p>

CC BY-SA 3.0
2018-03-29T11:59:25.970Z
2018-03-29T11:59:25.970Z
Gįëł Tėttëłæår
<p>In your case, i think you can use <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">element-api</a>, this is a great plugin for extracting data through ajax.</p>

CC BY-SA 3.0
2017-08-31T05:30:17.863Z
This is really too general a response, and not an answer to the user's question as such.
Jeremy Daalder
CC BY-SA 3.0
2017-08-31T04:20:20.390Z
2017-08-31T04:20:20.390Z
D. Hung
<p>I did search for this but was unable to find the documentation for getting user's data on a return when logging them in from a front-end facing login form (I need to do the same for a registration as well).</p>

<p>I need to get a return of the user's Id and their email.</p>

<p>This is my code for the form. It works fine,  I just cannot seem to find the user's info in the data return so if anyone has an example or suggestion it would be greatly appreciated.</p>

<pre><code>$('#workshop-registration-user-login-form').submit(function (e) {
  e.preventDefault();
  var data = $('#workshop-registration-user-login-form').serialize();

  $.ajax({
    type: 'POST',
    url: '/users/login',
    data: data,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  })
  .success(function(data) {
    console.log(data.currentUserId);

    ...

    // get data populate the fields
  })
  .error(function(data) {
    console.log('error');
  });
</code></pre>

<ajax><craft-pro>
How to get user's information when registering or logging in via ajax form
# How to get user's information when registering or logging in via ajax form

(Asked by: username_0 on 2016-10-17)

I did search for this but was unable to find the documentation for getting user's data on a return when logging them in from a front-end facing login form (I need to do the same for a registration as well).
I need to get a return of the user's Id and their email.
This is my code for the form. It works fine,  I just cannot seem to find the user's info in the data return so if anyone has an example or suggestion it would be greatly appreciated.
$('#workshop-registration-user-login-form').submit(function (e) {
  e.preventDefault();
  var data = $('#workshop-registration-user-login-form').serialize();

  $.ajax({
    type: 'POST',
    url: '/users/login',
    data: data,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
  })
  .success(function(data) {
    console.log(data.currentUserId);

    ...

    // get data populate the fields
  })
  .error(function(data) {
    console.log('error');
  });

**Tags:** ajax, craft-pro


---

## 3 answers

### Answer by username_1 on 2016-10-17. Score: 1

Not sure what other user info you're looking for, but Craft's [UsersController](https://craftcms.com/classreference/controllers/UsersController) will only return [two things](https://github.com/pixelandtonic/Craft-Release/blob/master/app/controllers/UsersController.php#L1601-L1608) in the JSON response of a successful login for an AJAX request.

A `success` key set to `true` and a `returnUrl` key set to where the login redirect should go to.

> **Comments:**
> **username_0**: so no way to get user Id and email?  
> **username_2**: You'd want to do an additional controller call upon successful login to get info about the currently-logged in user, with your own controller to return that info, if that is what you need.  
> **username_0**: O.k., so I need to create my own controller for this?  
> **username_3**: You could also just ajax load in a Craft template with `currentUser.id` and `currentUser.email` in it, in your success handler - if you want to avoid a controller for whatever reason.  But a controller for this would be trivial really.  

---

### Answer by username_4 on 2018-03-29. Score: 1

So if i understand your question correctly what you are trying to achieve is to get used data of a user that is trying to log in.

Craft's login controller will call the userSessionService->login() method which in turn will call the loginByUserId method. This method has two events attached to it - the onBeforeLogin and the onLogin.

-- onBeforeLogin is called before anything is really created.
-- onLogin is called if the login was succesfull.

Now if you listed to the onLogin method you get the param username. Craft will already have validated this username and logged it in - so you know it is good. You can then call

`craft()->users->getUserByUsernameOrEmail($yourUsernameVar);`

Which will return a populated UserModel. Hope it helps in some way


---

### Answer by username_5 on 2017-08-31. Score: -2

In your case, i think you can use [element-api](https://github.com/craftcms/element-api), this is a great plugin for extracting data through ajax.

> **Comments:**
> **username_3**: This is really too general a response, and not an answer to the user's question as such.  

---

