Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authenticate/Inviting #1

Merged
merged 11 commits into from
Sep 8, 2019
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.11'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'haml-rails', '~> 2.0'
gem 'bcrypt', '~> 3.1.7'
gem 'jwt', '2.2.1'
gem 'devise_invitable', '~> 2.0.0'

group :development, :test do
gem 'pry-byebug'
gem 'rspec-rails', '~> 3.8'
gem 'factory_bot_rails'
gem 'letter_opener'
end

group :development do
Expand Down
27 changes: 24 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
arel (9.0.0)
bcrypt (3.1.13)
bootsnap (1.4.5)
Expand All @@ -52,6 +54,15 @@ GEM
concurrent-ruby (1.1.5)
crass (1.0.4)
database_cleaner (1.7.0)
devise (4.7.1)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 4.1.0)
responders
warden (~> 1.2.3)
devise_invitable (2.0.1)
actionmailer (>= 5.0)
devise (>= 4.6)
diff-lcs (1.3)
erubi (1.8.0)
erubis (2.7.0)
Expand Down Expand Up @@ -79,7 +90,10 @@ GEM
ruby_parser (~> 3.5)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jwt (2.2.1)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.7.0)
launchy (~> 2.2)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -100,13 +114,15 @@ GEM
nio4r (2.5.1)
nokogiri (1.10.4)
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.7.0)
byebug (~> 11.0)
pry (~> 0.10)
public_suffix (4.0.1)
puma (3.12.1)
rack (2.0.7)
rack-test (1.1.0)
Expand Down Expand Up @@ -139,6 +155,9 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
responders (3.0.0)
actionpack (>= 5.0)
railties (>= 5.0)
rspec-core (3.8.2)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.4)
Expand Down Expand Up @@ -177,6 +196,8 @@ GEM
tilt (2.0.9)
tzinfo (1.2.5)
thread_safe (~> 0.1)
warden (1.2.8)
rack (>= 2.0.6)
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
Expand All @@ -185,12 +206,12 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0)
database_cleaner
devise_invitable (~> 2.0.0)
factory_bot_rails
haml-rails (~> 2.0)
jwt (= 2.2.1)
letter_opener
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry-byebug
Expand Down
11 changes: 0 additions & 11 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
rescue_from BasicAuthenticate::NotAuthenticated, with: :not_authenticated

private

def authorize!
AuthenticateByToken.new(request.headers['Authorization']).call
end

def not_authenticated
render json: { error: 'Not Authenticated' }, status: :unauthorized
end
end
9 changes: 0 additions & 9 deletions app/controllers/authentication_controller.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class HomeController < ApplicationController
before_action :authorize!
before_action :authenticate_user!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это имеет смысл перенести в application_controller

# GET /
def homepage
render json: { success: 'ok' }, status: :ok
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/invites_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

class InvitesController < ApplicationController
before_action :authorize!
# GET /send_invite
def send_invite
prefix = SecureRandom.hex(3)
InviteMailer.with(email: "#{prefix}[email protected]", password: 'qwerty').send_invite.deliver_later
before_action :authenticate_user!

# GET /invite
def invite

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

такое название метода не по REST.
И судя по всему оно меняет стейт в базе, поэтому надо юзать POST #create

User.invite!(email: '[email protected]', fullname: 'John Doe')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

почему email захардкожен?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Из-за того, что логики пока никакой нет.

render json: { success: 'invite sent' }, status: :ok
end
end
19 changes: 0 additions & 19 deletions app/controllers/users_controller.rb

This file was deleted.

10 changes: 0 additions & 10 deletions app/mailers/invite_mailer.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true

class User < ApplicationRecord
has_secure_password
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:invitable

validates :email, :password, presence: true
validates :email, uniqueness: true
Expand Down
24 changes: 0 additions & 24 deletions app/services/authenticate_by_token.rb

This file was deleted.

28 changes: 0 additions & 28 deletions app/services/get_token.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/views/invite_mailer/send_invite.html.haml

This file was deleted.

3 changes: 2 additions & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@
# Tell Action Mailer not to deliver emails to the real world.
# The :test delivery method accumulates sent emails in the
# ActionMailer::Base.deliveries array.
config.action_mailer.delivery_method = :test
config.action_mailer.delivery_method = :letter_opener

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
Expand Down
Loading