简体   繁体   English

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

[英]Using the Parse.com Javascript API with users authenticated via the 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? 如果我的站点使用Parse.com PHP API对用户进行身份验证,我是否可以将Javascript API用于ajax请求,以便通过PHP登录的用户可以通过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. 我有一个网站使用Parse.com PHP API来处理所有后端任务,例如存储和检索数据以及对用户进行身份验证。

I have several pages that use ajax to refresh page content. 我有几个使用ajax刷新页面内容的页面。

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. 我已经编写了自己的Javascript类,用于通过ajax将JSON数据发送到服务器,在该处处理信息,并根据需要插入PHP代码以创建,更新或删除Parse对象。

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. Parse.com Javascript API很棒,我对此非常熟悉,它已经很好地处理了文件。 It would be awesome If I could use it for the ajax. 如果我可以将其用于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 使用PHP API登录,然后尝试直接使用Javascript SDK来获取当前用户,例如

    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. 这没有用。

It turns out that you can do this by passing the Parse PHP session token to the Javascript API like this: 事实证明,您可以通过将Parse PHP会话令牌传递给Javascript API来做到这一点,如下所示:

   <?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);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM