trailsjs / sails-auth

Passport-based User Authentication system for sails.js applications. Designed to work well with the sails-permissions module.
https://www.npmjs.org/package/sails-auth
MIT License
265 stars 141 forks source link

Linking an existing account when authenticating with a different provider #105

Open dottodot opened 8 years ago

dottodot commented 8 years ago

At the moment all my users can login using the local method however I would like to add an option to login using Google.

However when I try and login with an account that already exists I get the error "A record with that username already exists"

Is it possible to have multiple login protocols and if so how to I get it to link the accounts that already exist?

dottodot commented 8 years ago

OK I've worked out what the issue is. If a user already has a local account the only way they can currently link their account with a 3rd party provider i.e Google, is if they login first and then connect.

The problem here is my login form has 2 options, the traditional local login and login with Google, but if they select Google without having connected their account previously it will check to see if the passport exists, which it won't, and because of this then go directly to creating a new user which is where it fails because the user already exists. I trying to avoid the need to have user connecting their accounts first.

For this scenario it should be also checking to see if the user already exists so they can be linked or changing create user to findOrCreate.

I can't see any downside of using findOrCreate as if they didn't have a local account it would log them in anyway, it just stops the duplicate account issue for those already with a local account who are now trying to login with Google.