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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值