-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: Refactor authentication and email models
- Introduced `UserDatabaseAuthentication` model with secure password management. - Created `UserEmail` model to manage email addresses independently. - Updated login, signup, and set password mutations to use the new models. - Modified invitation mailer to work with `UserEmail`. - Adjusted tests to reflect the separation of user authentication and email logic. - Removed `pgcrypto` extension as UUIDs are now handled without it.
- Loading branch information
1 parent
537e2eb
commit 1118c43
Showing
24 changed files
with
149 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class InvitationMailer < ApplicationMailer | ||
def invite(user_id) | ||
@user = User.find(user_id) | ||
@signed_id = @user.signed_id(purpose: :invite, expires_in: 1.hour) | ||
mail to: @user.email_address | ||
def invite(user_email_id) | ||
@user_email = UserEmail.find(user_email_id) | ||
@signed_id = @user_email.signed_id(purpose: :invite, expires_in: 1.hour) | ||
mail to: @user_email.email | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
class User < ApplicationRecord | ||
has_secure_password | ||
has_many :sessions, dependent: :destroy | ||
has_one :database_authentication, dependent: :destroy, class_name: :UserDatabaseAuthentication, inverse_of: :user | ||
has_one :email, dependent: :destroy, class_name: :UserEmail, inverse_of: :user | ||
|
||
normalizes :email_address, with: -> e { e.strip.downcase } | ||
delegate :email_address, to: :email | ||
|
||
validates :email_address, uniqueness: true | ||
|
||
enum :onboarding_status, %w[ | ||
before_verify_email_address | ||
before_set_own_password | ||
onboarded | ||
].index_by(&:itself), suffix: 'status' | ||
class << self | ||
def create_with!(email_address:) | ||
create! | ||
.tap { _1.create_email!(email_address:) } | ||
.tap { _1.create_database_authentication!(password: SecureRandom.uuid) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class UserDatabaseAuthentication < ApplicationRecord | ||
has_secure_password | ||
|
||
belongs_to :user, inverse_of: :database_authentication | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class UserEmail < ApplicationRecord | ||
belongs_to :user, inverse_of: :email | ||
alias_attribute :email, :email_address | ||
|
||
normalizes :email_address, with: -> e { e.strip.downcase } | ||
validates :email_address, uniqueness: true | ||
scope :verified, -> { where.not(confirmed_at: nil) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class CreateUsers < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :users, id: :uuid do |t| | ||
t.timestamps | ||
end | ||
end | ||
end |
5 changes: 0 additions & 5 deletions
5
backend/db/migrate/00000000000001_enable_pgcrypto_extension.rb
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
backend/db/migrate/00000000000002_create_user_database_authentications.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateUserDatabaseAuthentications < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :user_database_authentications, id: :uuid do |t| | ||
t.references :user, null: false, foreign_key: true, type: :uuid, index: { unique: true } | ||
t.string :password_digest, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateUserEmails < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :user_emails, id: :uuid do |t| | ||
t.references :user, null: false, foreign_key: true, type: :uuid, index: { unique: true } | ||
t.string :email_address, null: false | ||
t.datetime :confirmed_at | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :user_emails, :email_address, unique: true | ||
end | ||
end |
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :user_database_authentication do | ||
association(:user) | ||
password { SecureRandom.alphanumeric } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FactoryBot.define do | ||
factory :user_email do | ||
association(:user) | ||
email_address { "#{SecureRandom.alphanumeric}@example.com" } | ||
confirmed_at { Time.current } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
FactoryBot.define do | ||
factory :user do | ||
email_address { "#{SecureRandom.alphanumeric}@example.com" } | ||
password { SecureRandom.alphanumeric } | ||
onboarding_status { :onboarded } | ||
trait :onboarded do | ||
after(:create) do |instance| | ||
create(:user_database_authentication, user: instance) | ||
create(:user_email, user: instance) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,11 @@ | |
end | ||
|
||
context "when password is correct" do | ||
before { User.create!(email_address: "[email protected]", password: "password", onboarding_status: :before_verify_email_address) } | ||
before do | ||
user = create(:user) | ||
create(:user_email, user:, email: "[email protected]") | ||
create(:user_database_authentication, user:, password: "password") | ||
end | ||
it "no errors" do | ||
expect { subject }.to change(Session, :count).by(1) | ||
expect(subject_response_to_hash).to match( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,39 +65,24 @@ | |
let!(:email_address) { '[email protected]' } | ||
let!(:context) { {} } | ||
|
||
context 'when onboarding_status: :before_verify_email_address' do | ||
before { create(:user, email_address:, onboarding_status: :before_verify_email_address) } | ||
it 'sends an invitation email' do | ||
expect { subject } | ||
.to have_enqueued_job(ActionMailer::MailDeliveryJob) | ||
.with('InvitationMailer', 'invite', 'deliver_now', { args: [be_a(String)] }) | ||
expect(subject_response_to_hash).to match( | ||
data: { | ||
signup: { | ||
success: true, | ||
errors: [], | ||
}, | ||
}, | ||
) | ||
end | ||
before do | ||
user = create(:user) | ||
create(:user_email, user:, email: "[email protected]") | ||
end | ||
|
||
context 'when onboarding_status: :before_set_own_password' do | ||
before { create(:user, email_address:, onboarding_status: :before_set_own_password) } | ||
it 'does not send invitation email' do | ||
expect { subject } | ||
.not_to have_enqueued_job(ActionMailer::MailDeliveryJob) | ||
expect(subject_response_to_hash).to match( | ||
data: { | ||
signup: { | ||
success: false, | ||
errors: [ | ||
{ __typename: "Taken" }, | ||
], | ||
}, | ||
it 'does not send invitation email' do | ||
expect { subject } | ||
.not_to have_enqueued_job(ActionMailer::MailDeliveryJob) | ||
expect(subject_response_to_hash).to match( | ||
data: { | ||
signup: { | ||
success: false, | ||
errors: [ | ||
{ __typename: "Taken" }, | ||
], | ||
}, | ||
) | ||
end | ||
}, | ||
) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,10 @@ | |
} | ||
GRAPHQL | ||
end | ||
let!(:current_user) { User.new(email_address: "[email protected]") } | ||
let!(:user) { create(:user) } | ||
let!(:user_email) { create(:user_email, user:, email: '[email protected]') } | ||
let!(:variables) { {} } | ||
let!(:context) { { current_user: } } | ||
let!(:context) { { current_user: user } } | ||
|
||
it "returns me" do | ||
expect(subject_response_to_hash).to eq( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User, type: :model do | ||
it { is_expected.to have_secure_password } | ||
it { is_expected.to have_many(:sessions).dependent(:destroy) } | ||
it { is_expected.to normalize(:email_address).from(" [email protected] ").to("[email protected]") } | ||
it { is_expected.to have_one(:database_authentication).dependent(:destroy) } | ||
it { is_expected.to have_one(:email).dependent(:destroy) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters