E
E
Egor Merkushev2015-02-25 14:29:24
iOS
Egor Merkushev, 2015-02-25 14:29:24

How to implement "Sign in with VK" on Parse.com?

Pars recently announced Third-Party Authentication support for web apps blog.parse.com/2014/01/14/adding-third-party-authe...
They even have an example for GitHub https://github.com/ParsePlatform /CloudCodeOAuthGit...
Also, a new method for PFUser appeared in the Client SDK:


[PFUser becomeInBackground:@"session-token-here" block:^(PFUser *user, NSError *error) {
if (error) {
// The token could not be validated.
} else {
// The current user is now set to user.
}
}];

But, as I understand it, it needs "support" from Cloud Code.
Is it possible to use this feature to implement login from VK.com, similar to that implemented in Parse SDK Twitter and Facebook?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Ivanov, 2015-04-10
@egormerkushev

The code for parse cloud looks for a user with a given vkId and returns a token

Parse.Cloud.define("getTokenByVkId", function(request, response) {
                   
                   Parse.Cloud.useMasterKey();
                   var vkId = request.params.vkId;
                   if(!vkId) {
                   response.error("wrong parameters");
                   return ;
                   }
                   var query = new Parse.Query(Parse.User);
                   query.equalTo("vkId", vkId);
                   query.first({
                              success: function(user) {
                                  response.success(user.getSessionToken());
                              },
                              error: function(error) {
                                  response.error(error.description);
                              }
                              });
                   
                   });

We send the vkId of the authorized user of our function to Parse Cloud and in response we receive a token by which we authorize in Parse
iOS itself objective-c example
//call parse cloud function
[PFCloud callFunctionInBackground:@"getTokenByVkId"
                                withParameters:@{@"vkId": vkUserId}
                                                 block:^(NSString* token, NSError *error) {
                    if (!error){
                       [PFUser becomeInBackground:token block:^(PFUser *user, NSError *error) {
                            <#code#>
                        }];
                    }else{
                        //TODO: handle error
                    }

Personally, when registering a new user in Parse, I save the generated password in the keychain and when I log in again, I first check for its presence and if it is not there (most likely the application is launched from another device), then I log in using the ParseCloud code provided.

A
Alexander Shcherbakov, 2015-03-15
@mkll

You yourself answer your own question, giving an example implementation for Github. :) Take this code and edit even under VK, even under Google+.
There is a link on the github to a detailed manual:
https://parse.com/tutorials/adding-third-party-aut...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question