Parse server twitter authentication: Twitter auth integrated but unable to create session to use on client side

强颜欢笑 提交于 2019-12-24 08:25:18

问题


Parse Cloud code:

Parse.Cloud.define("twitter", function(req, res) {
    /*
        |--------------------------------------------------------------------------
        | Login with Twitter
        | Note: Make sure "Request email addresses from users" is enabled
        | under Permissions tab in your Twitter app. (https://apps.twitter.com)
        |--------------------------------------------------------------------------
        */
    var requestTokenUrl = 'htt****/oauth/request_token';
    var accessTokenUrl = 'http***itter.com/oauth/access_token';
    var profileUrl = 'https://api.twitter.com/1.1/account/verify_credentials.json';

    // Part 1 of 2: Initial request from Satellizer.
    if (!req.params.oauth_token || !req.params.oauth_verifier) {
        var requestTokenOauth = {
            consumer_key: 'EVJCRJfgcKSyNUQgOhr02aPC2',
            consumer_secret: 'UsunEtBnEaQRMiq5yi4ijnjijnjijnijnjEjkjYzHNaaaSbQCe',
            oauth_callback: req.params.redirectUri
        };

        // Step 1. Obtain request token for the authorization popup.
        request.post({
            url: requestTokenUrl,
            oauth: requestTokenOauth
        }, function(err, response, body) {
            var oauthToken = qs.parse(body);
            // console.log(body);
            // Step 2. Send OAuth token back to open the authorization screen.
            console.log(oauthToken);
            res.success(oauthToken);
        });
    } else {
        // Part 2 of 2: Second request after Authorize app is clicked.
        var accessTokenOauth = {
            consumer_key: 'EVJCRJfgcKSyNUQgOhr02aPC2',
            consumer_secret: 'UsunEtBnEaQRMiq5yi4ijnjijnjijnijnjEjkjYzHNaaaSbQCe',
            token: req.params.oauth_token,
            verifier: req.params.oauth_verifier
        };
        // Step 3. Exchange oauth token and oauth verifier for access token.
        request.post({
            url: accessTokenUrl,
            oauth: accessTokenOauth
        }, function(err, response, accessToken) {

            accessToken = qs.parse(accessToken);

            var profileOauth = {
                consumer_key: 'EVJCRJfgcKSyNUQgOhr02aPC2',
                consumer_secret: 'UsunEtBnEaQRMiq5yi4ijnjijnjijnijnjEjkjYzHNaaaSbQCe',
                token: accessToken.oauth_token,
                token_secret: accessToken.oauth_token_secret,
            };
            console.log(profileOauth);

            // Step 4. Retrieve user's profile information and email address.
            request.get({
                url: profileUrl,
                qs: {
                    include_email: true
                },
                oauth: profileOauth,
                json: true
            }, function(err, response, profile, USER) {

                console.log(profile);
                //console.log(response.email);

                Parse.Cloud.useMasterKey();
                var UserPrivateInfo = Parse.Object.extend("UserPrivateInfo");
                var query = new Parse.Query(UserPrivateInfo);
                query.equalTo("email", profile.email);
                query.first({
                    success: function(privateInfo) {
                        if (privateInfo) {
                            res.success(privateInfo.get('user'));
                        } else {
                            response.success();
                        }
                    },
                    error: function(error) {
                        response.error("Error : " + error.code + " : " + error.message);
                    }
                });
            });
        });
    }

});

For client side using Sendgrid twitter authentication:

loginCtrl.twitterLogin = function() {
    $auth.authenticate("twitter").then(function(response) {
        console.log(response.data.result);
        var user = response.data.result;
        if (!user.existed()) {
var promise = authFactory.saveUserStreamDetails(user,                            response.email);
            promise.then(function(response) {
                signInSuccess(response);
            }, function(error) {
                console.log("error while saving user details.");
            });
        } else {
            signInSuccess(user);
        }


    }).catch(function(error) {
        console.log(error);
    });;
};

Issue:

Step 1: Called cloud function Parse.Cloud.define("twitter", function(req, res) using loginCtrl.twitterLogin

Step 2: Twitter popup opens and user logs in to twitter

Step 3: Got verification keys and again cloud function
Parse.Cloud.define("twitter", function(req, res) is called and user is verified

Step 4: Got the user email using the twitter API.

Step 5: I can get the existing Parse User Object using the email or can signUp using that email.

Step 6: Returns the parse user object to client but there is no session attached to it so **How can I create user session?

Without parse session we can not log in to parse application. Every clound code api/ function call will fail as no session is attached to them. So how can I create and manage a session using twitter authentication.

来源:https://stackoverflow.com/questions/42500612/parse-server-twitter-authentication-twitter-auth-integrated-but-unable-to-creat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!