Parse-server: Google OAuth Configuration

Created on 20 May 2016  Â·  9Comments  Â·  Source: parse-community/parse-server

What would the configuration look like for using Google OAuth to sign in users? The documentation mentions this:

{ oauth: { twitter: { consumer_key: "", // REQUIRED consumer_secret: "" // REQUIRED }, facebook: { appIds: "FACEBOOK APP ID" } } }

but not what it should look like for Google. Also for the client iOS SDK, I know I would use the Google Sign In SDK, but how do I link that to Parse? Thanks.

Most helpful comment

@rsweny Thanks for this blob of code. It helped me.
In case, anyone wants to avoid duplicate email ids, this is how I am doing it.

                Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
                t.continueWith(new Continuation<ParseUser, Void>() {
                    @Override
                    public Void then(Task<ParseUser> task) throws Exception {
                        if (task.isCancelled()) {
                            Log.w("Login", "Task cancelled");
                        } else if (task.isFaulted()) {
                            Log.w("Login", "Save FAIL" + task.getError());
                        } else {
                            // the object was saved successfully.
                            /**
                             * current user is not null at this point
                             * if a user exists with the authData already, then that user was logged in.
                             * else, a new user was created.
                             * Now set the email id for this user and try to save the user. If a user already exists with this email id
                             * an error will be thrown stating an account already exists for this email id.
                             * If you get this error delete the new empty user that was created and also log them out.                             *
                             */
                            final ParseUser pUser = (ParseUser) task.getResult();
                            pUser.setEmail(googleSignInAccount.getEmail());
                            pUser.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if (e == null) {
                                        Log.w("Login", "Success " + pUser.getObjectId() + " " + pUser.getUsername() + " " + pUser.getEmail() + " " + pUser.getSessionToken());
                                        /* Handle successful login */
                                    } else {
                                        Log.e("Login", e.getMessage());
                                        pUser.deleteInBackground();
                                        ParseUser.logOutInBackground();
                                    }
                                }
                            });

                        }
                        return null;
                    }
                });

All 9 comments

+1 looking for this

You should not need anything on the server, just set the access_token property of the authData object when singingUp / linking from any client.

@flovilmart I have tried that and got the error described here: https://github.com/ParsePlatform/parse-server/issues/1961

Did you try with just putting {oauth: {google: true}}

In the server configuration? Like this? I am still getting the same issue with this.

oauth: {
    google: true
  }

@flovilmart any gist available for Android SDK oauth similar to your https://gist.github.com/flovilmart/68a6c538496953408bb1 for iOS?

Nope sorry. That should be pretty similar though

[F v:lmart]

Le 13 juin 2016 à 23:45, benitech [email protected] a écrit :

@flovilmart any gist available for Android SDK oauth similar to your https://gist.github.com/flovilmart/68a6c538496953408bb1 for iOS?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

I found this hard to figure out too.. but in case it helps others oauth: { google: true } is not needed on the server. Android code looks like this and is called from Activity.onActivityResult:

    private void handleSignInResult(GoogleSignInResult result) {
        Log.w(TAG, "-------- handleSignInResult Google: " + result.isSuccess());
        ParseUser curUser = ParseUser.getCurrentUser();
        if (curUser != null)
        {
            Log.w(TAG, "Skip Google Auth - Found cur user: " + curUser.getObjectId());
            return;
        }

        if (result.isSuccess()) {
            GoogleSignInAccount acct = result.getSignInAccount();

            if (acct != null) {
                Log.w(TAG, "SUCCESS: " + acct.getDisplayName());
                Log.w(TAG, acct.getIdToken() + " - " + acct.getId() + " - " + acct.getEmail());

                final String[] names = acct.getDisplayName().split(" ");
                final String personPhotoUrl = acct.getPhotoUrl().toString();
                final String googleEmail = acct.getEmail();
                //Log.w(TAG, personPhotoUrl);

                Map<String,String> authData = new HashMap<String,String>();
                authData.put("id_token", acct.getIdToken());
                authData.put("id", acct.getId());
                Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
                t.continueWith(new Continuation<ParseUser, Void>() {
                    public Void then(Task task) throws Exception {
                        if (task.isCancelled()) {
                            Log.w(TAG, "Task cancelled");
                        } else if (task.isFaulted()) {
                            Log.w(TAG, "Save FAIL" + task.getError());
                            Utilities.showToast(getResources().getString(R.string.errorLogin) + task.getError(), MainActivity.this);
                        } else {
                            // the object was saved successfully.
                            ParseUser user = (ParseUser)task.getResult();
                            Log.w(TAG, "Success " + user.getObjectId() + " " + user.getUsername() + " " + user.getEmail() + " " + user.getSessionToken());

It will create a new account even if a Facebook user exists with that email already so best not to fill in the User.email field or you will have problems because duplicates aren't allowed. Setting a new field like "User.appEmail" might work better.

@rsweny Thanks for this blob of code. It helped me.
In case, anyone wants to avoid duplicate email ids, this is how I am doing it.

                Task<ParseUser> t = ParseUser.logInWithInBackground("google", authData);
                t.continueWith(new Continuation<ParseUser, Void>() {
                    @Override
                    public Void then(Task<ParseUser> task) throws Exception {
                        if (task.isCancelled()) {
                            Log.w("Login", "Task cancelled");
                        } else if (task.isFaulted()) {
                            Log.w("Login", "Save FAIL" + task.getError());
                        } else {
                            // the object was saved successfully.
                            /**
                             * current user is not null at this point
                             * if a user exists with the authData already, then that user was logged in.
                             * else, a new user was created.
                             * Now set the email id for this user and try to save the user. If a user already exists with this email id
                             * an error will be thrown stating an account already exists for this email id.
                             * If you get this error delete the new empty user that was created and also log them out.                             *
                             */
                            final ParseUser pUser = (ParseUser) task.getResult();
                            pUser.setEmail(googleSignInAccount.getEmail());
                            pUser.saveInBackground(new SaveCallback() {
                                @Override
                                public void done(ParseException e) {
                                    if (e == null) {
                                        Log.w("Login", "Success " + pUser.getObjectId() + " " + pUser.getUsername() + " " + pUser.getEmail() + " " + pUser.getSessionToken());
                                        /* Handle successful login */
                                    } else {
                                        Log.e("Login", e.getMessage());
                                        pUser.deleteInBackground();
                                        ParseUser.logOutInBackground();
                                    }
                                }
                            });

                        }
                        return null;
                    }
                });
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jiawenzhang picture jiawenzhang  Â·  4Comments

mohmagdy picture mohmagdy  Â·  3Comments

ViolentCrumble picture ViolentCrumble  Â·  3Comments

carjo422 picture carjo422  Â·  3Comments

lorki picture lorki  Â·  3Comments