dtbiszu7724 2015-11-08 04:39
浏览 40
已采纳

使用Parse.com Javascript API与通过PHP API进行身份验证的用户

If my site uses the Parse.com PHP API to authenticate users, can I use the Javascript API for ajax requests in such a way that the user, having been logged in via PHP, can access data via the Javascript API?

What would I need to set up differently?


Some background

I have a site that uses the Parse.com PHP API to handle all backend tasks like storing and retrieving data and authenticating users.

I have several pages that use ajax to refresh page content.

I have written my own Javascript class for sending JSON data to my server via ajax where the info is processed and my PHP code steps in to create, update, or delete Parse objects as needed.

This all works very well with the exception of dealing with files. I see several approaches to dealing with files but I feel like I'm re-inventing the wheel.

The Parse.com Javascript API is great, I'm pretty familiar with it and it already deals with files really well. It would be awesome If I could use it for the ajax.


I have tried...

Logging in with the PHP API and then attempting to use the Javascript SDK directly to get the current user like

    var currentUser = Parse.User.current();
    if (currentUser) {
        // do stuff with the user
        alert(currentUser);
    } else {
        // show the signup or login page
        alert('that didnt work');
    }

This did not work.

  • 写回答

1条回答 默认 最新

  • dongxing7530 2015-11-09 06:43
    关注

    It turns out that you can do this by passing the Parse PHP session token to the Javascript API like this:

       <?php
            $sessionToken = ParseUser::getCurrentUser()->getSessionToken();
            echo 'var token="'.$sessionToken.'";';
        ?>
    
    
        Parse.User.become(token).then(function (user) {
            // The current user is now set to user.
            alert('User has access to javascript API');
        }, function (error) {
            // The token could not be validated.
            alert('Error: ' + error.code + ' ' + error.message);
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器