Skip to content

The third-party login lacks a bit of flexibility #4694

@oallouch

Description

@oallouch

My usernames are emails.
I'd like Facebook or Google logins to automatically link with a potential existing account.

What I did was:
On the client side:
. I use hello.js to retrieve an access-token
. I send the token to the server. I don't call _linkWith on the client, otherwise a User might be created with a random username.
. The server uses the access_token to retrieve the email using an OAuth service. I also check that the e-mail is verified if necessary.
. I search for the User using its email (it's the username). If not found, I create it. I can now create the user. The username is the email and my beforeSave hook will work (it wants e-mails). If the user is created, it has a random password.
. Then I do a server _linkWith. Looking at the js sdk source, I saw it's only a save with some specific data. My code for this is:

//---- linkWith (the server version) ----//
		await user.save({
			authData: {
				[provider]: authData
			}
		}, {useMasterKey: true}); // see ParseUser's UserController's linkWith

. Then I return a Parse sessionToken, taken from the User returned the signup call or from a server login. For this server login, I had to dig deep (for me) inside the Parse codebase. The code I used is:


/**
 * . creates a session using the masterKey
 * . code taken from Parse.UsersRouter's handleLogIn
 * @param userId
 * @param can be 'password' or 'facebook' (or 'google'...)
 */
function serverLogin({ userId, authProvider, installationId }) {
	// if we put Auth on top of this file, it's too early and throws an exception
	const Auth = require('../node_modules/parse-server/lib/Auth');
	const Config = require('../node_modules/parse-server/lib/Config');

	const config = Config.get(Parse.applicationId);
	const { sessionData, createSession } = Auth.createSession(config, {
		userId: userId,
		createdWith: {
			action: 'login',
			authProvider
		},
		installationId
	});

	createSession();
	return sessionData.sessionToken;
}

I think you should be able to login on the server like we can signup Using the sessionToken with 'become' is nice and elegant.

What do you think of it ?

Olivier from Paris

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions