Skip to content

Commit d5902cd

Browse files
authored
Merge pull request #925 from amatsuda/legacy_to_identity_on_login
Migrate from legacy provider/uid to Identity record on lookup
2 parents 068af4a + 78980f0 commit d5902cd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

app/models/user.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ def self.from_omniauth(auth, invitation_email = nil)
4141
return identity.user if identity
4242

4343
# Fall back to legacy lookup in users table
44-
user = find_by(provider: auth.provider, uid: auth.uid)
45-
return user if user
44+
if (user = find_by(provider: auth.provider, uid: auth.uid))
45+
# Migrate legacy user to identities table
46+
transaction do
47+
user.identities.create!(provider: auth.provider, uid: auth.uid)
48+
user.update_columns(provider: nil, uid: nil)
49+
end
50+
return user
51+
end
4652

4753
# Create new user with identity
4854
create_from_omniauth!(auth, invitation_email)

spec/models/user_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@
1111
let(:email) { '[email protected]' }
1212
let(:account_name) { 'testuser' }
1313

14-
context "User already exists" do
15-
let!(:user) { create(:user, email: email, name: OmniAuth.config.mock_auth[:github].info.name, uid: OmniAuth.config.mock_auth[:github].uid, provider: "github") }
14+
context 'User already exists with legacy provider/uid' do
15+
let!(:user) { create(:user, email: email, name: OmniAuth.config.mock_auth[:github].info.name, uid: OmniAuth.config.mock_auth[:github].uid, provider: 'github') }
1616

17-
it "doesn't create a new user from github auth hash when user exists" do
17+
it 'does not create a new user' do
1818
expect {
1919
User.from_omniauth(github_auth_hash)
2020
}.to_not change { User.count }
2121
end
2222

23+
it 'migrates legacy user to identities table' do
24+
expect {
25+
User.from_omniauth(github_auth_hash)
26+
}.to change { Identity.count }.by(1)
27+
28+
user.reload
29+
expect(user.identities.find_by(provider: 'github', uid: github_auth_hash.uid)).to be_present
30+
expect(user.provider).to be_nil
31+
expect(user.uid).to be_nil
32+
end
2333
end
2434

2535
context "User doesn't yet exist" do

0 commit comments

Comments
 (0)