Prefer to use primary email for new user creation over other available emails

This commit is contained in:
Vinoth Kannan 2018-03-19 17:10:35 +05:30
parent 19a93b0e95
commit c5d26992d4
2 changed files with 28 additions and 0 deletions

View File

@ -47,6 +47,8 @@ class Auth::GithubAuthenticator < Auth::Authenticator
# Potentially use *any* of the emails from GitHub to find a match or
# register a new user, with preference given to the primary email.
all_emails = Array.new(auth_token[:extra][:all_emails])
primary = all_emails.detect { |email| email[:primary] && email[:verified] }
all_emails.unshift(primary) if primary.present?
# Only consider verified emails to match an existing user. We don't want
# someone to be able to create a GitHub account with an unverified email

View File

@ -57,6 +57,32 @@ describe Auth::GithubAuthenticator do
expect(result.email_valid).to eq(true)
end
it 'should use primary email for new user creation over other available emails' do
hash = {
extra: {
all_emails: [{
email: "bob@example.com",
primary: false,
verified: true,
}, {
email: "john@example.com",
primary: true,
verified: true,
}]
},
info: {
email: "john@example.com",
nickname: "john",
name: "John Bob",
},
uid: "100"
}
result = authenticator.after_authenticate(hash)
expect(result.email).to eq("john@example.com")
end
it 'will not authenticate for already existing users with an unverified email' do
hash = {
extra: {