2

I created an Ajax-based login form based on this gist: https://gist.github.com/keithmancuso/29e8446ec5fa66fb2b01.

After the user has logged in, how can I get some info about them over Ajax?

1
  • I do not want to refresh the page for such a trifle Sep 28 '15 at 11:25
3

For the redirect I agree, I could not get it too from ajax response, so I just saved it before the ajax call.
var returnUrl = $("input[name='redirect']").val();

Then I can make the redirection :

.success(function(data) {
    if (data.success) {
        window.location.href = returnUrl;
    }

For the user's data could you precise what you want to do?

1
  • I on the contrary do not want to redirect the user. I update question. Oct 4 '15 at 13:24
2

I found a solution using the Element API plugin, with the following endpoints config:

<?php
namespace Craft;

return [
    'endpoints' => [
        'user/info.json' => [
            'elementType' => 'User',
            'criteria' => [
                'id' => craft()->userSession->getId()
            ],
            'first' => true,
            'transformer' => function (UserModel $user) {
                return [
                    'email' => $user->email,
                    'image' => $user->getPhotoUrl('28')
                ];
            },
        ]
    ]
];

Now after saving a user I can just create an Ajax request to /user/info.json and get their info in response.

1
  • Hey Vitaliy, I just updated your question and answer to make them more clear for other people in the future. I also added 'first' => true to your endpoint config, which is going to slightly alter the response format, making it more optimized for a single user response. You can see how a single-user response differs from multi-user by looking at the Examples section in the Element API docs. Oct 6 '15 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.