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

Feature/padlock auth #15

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open

Feature/padlock auth #15

wants to merge 9 commits into from

Conversation

andrewr224
Copy link
Member

@andrewr224 andrewr224 commented Apr 6, 2018

  • Create delete job on token creation
  • Improve phone/mail validation
  • Implement email verification
  • Implement reset password

@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch 3 times, most recently from 117ed28 to 52caa23 Compare April 10, 2018 14:55
key_type: type,
expired_at: expired_at
)
ExpireTokenJob.set(wait_until: token.expired_at).perform_now(token_id: token.id) if token
Copy link
Member Author

Choose a reason for hiding this comment

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

This seems to work, but, and I know a good but when I see one, that's probably not how it's supposed to be used. Please advise

def create
@user = V1::Padlock::Registrations::Create.call(user_params)

return render json: @user.errors, status: :unprocessable_entity unless @user.persisted?
Copy link
Member

Choose a reason for hiding this comment

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

if @user.new_record? => same

Copy link
Member Author

Choose a reason for hiding this comment

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

unless persisted seems to better convey my intention here, namely - I wanna render errors unless the user was saved

Copy link
Member

Choose a reason for hiding this comment

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

Ok, just noticed that => persisted? == !(new_record? || destroyed?)

Copy link
Member

Choose a reason for hiding this comment

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

Also, you have defined alias for this - user_valid?

def create
@user = V1::Padlock::Sessions::Create.call(params)

return render json: nil, status: :unauthorized unless @user
Copy link
Member

Choose a reason for hiding this comment

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

head :unauthorized

def destroy
return render json: nil, status: :unprocessable_entity unless token

return render json: nil, head: :ok if ::Padlock::TokenDestroyer.call(token: token)
Copy link
Member

Choose a reason for hiding this comment

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

does it work? :D

Copy link
Member Author

Choose a reason for hiding this comment

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

Why wouldn't it?

Copy link
Member

Choose a reason for hiding this comment

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

Because render checks status key. Change :ok, to :unprocessable_entity. Theoretically you'll see the same result.

Copy link
Member

Choose a reason for hiding this comment

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

..so you can ignore status here at all, because it is 200, when response has body, by default

end

def destroy
return render json: nil, status: :unprocessable_entity unless token
Copy link
Member

Choose a reason for hiding this comment

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

same as above

class ExpireTokenJob < ApplicationJob
queue_as :default

def perform(params = {})
Copy link
Member

@ailecksandr ailecksandr Apr 10, 2018

Choose a reason for hiding this comment

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

**params

Copy link
Member Author

Choose a reason for hiding this comment

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

ArgumentError: wrong number of arguments (given 1, expected 0)

def identity
@identity ||= case
when email
Identity.find_by(uid: email)
Copy link
Member

Choose a reason for hiding this comment

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

def uid
  email || phone
end

def identity
  @identity ||= Identity.find_by(uid: uid)
end

Copy link
Member Author

Choose a reason for hiding this comment

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

5-levels-of-customer-satisfaction

def initialize(params = {})
@user = params[:user]
@type = params[:type] || :auth
@expired_at = params[:expired_at] || Time.zone.now + 1.month
Copy link
Member

Choose a reason for hiding this comment

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

use fetch when you set default values in initializer.

But if you need to wrap cases like this:

Object.new(type: nil)

and have :auth instead of nil
=> use your approach

@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch 2 times, most recently from 2b286f8 to 136fcbb Compare April 11, 2018 07:12
@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch from 136fcbb to ad3964b Compare April 11, 2018 09:47
@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch 2 times, most recently from 8ab1e3c to bbeef09 Compare April 11, 2018 14:23
Copy link
Member

@ailecksandr ailecksandr left a comment

Choose a reason for hiding this comment

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

Almost good to go. Plenty of small changes have left now
Well done!

attr_reader :user

def user_valid?
user&.persisted?
Copy link
Member

@ailecksandr ailecksandr Apr 11, 2018

Choose a reason for hiding this comment

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

This is not a predicate, you can receive nil here

user && user.persisted?

And IMO, user_persisted? looks better, because user_valid? is a little bit smooth description here

Copy link
Member Author

Choose a reason for hiding this comment

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

Rubocop wouldn't stand it

end

def insert_token
response.headers[ACCESS_TOKEN_KEY] = user.tokens.last.key if user_valid?
Copy link
Member

Choose a reason for hiding this comment

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

thread-unsafe token
(someone else can add token to user right after creation of user)

Remember about refactoring here.

Now you can define has_one association last_access_token in User model

@@ -0,0 +1,27 @@
module V1
Copy link
Member

Choose a reason for hiding this comment

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

We need to think about moving whole this module to lib folder.

It’s for libraries that are not packaged as gems but could be. (c) source

def create
@user = V1::Padlock::Registrations::Create.call(user_params)

return render json: @user.errors, status: :unprocessable_entity unless @user.persisted?
Copy link
Member

Choose a reason for hiding this comment

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

Also, you have defined alias for this - user_valid?

def create
@user = V1::Padlock::Sessions::Create.call(params)

return head :unauthorized unless @user
Copy link
Member

Choose a reason for hiding this comment

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

user_valid?

@@ -0,0 +1,9 @@
class ExpireTokenJob
include Sidekiq::Worker
Copy link
Member

Choose a reason for hiding this comment

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

You can move include to ApplicationJob and then inherit from it. Would be cleaner

Copy link
Member Author

Choose a reason for hiding this comment

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

ArgumentError: You cannot include Sidekiq::Worker in an ActiveJob: ApplicationJob

class ExpireTokenJob
include Sidekiq::Worker

def perform(params = {})
Copy link
Member

Choose a reason for hiding this comment

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

Use explicit arguments, or at least key arguments (:+1:) here.

We haven't defined constructor for job to provide attributes assigning, call them from the params hash all the time looks messy.

factory :identity do
trait :email do
uid { Faker::Internet.email }
provider 'email'.freeze
Copy link
Member

Choose a reason for hiding this comment

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

Why not symbol?


trait :phone do
uid { Faker::PhoneNumber.phone_number }
provider 'phone'.freeze
Copy link
Member

Choose a reason for hiding this comment

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

same


subject { described_class.perform_at(token.expired_at, token_id: token.id) }

let!(:token) { create(:token) }
Copy link
Member

Choose a reason for hiding this comment

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

Let's apply one conventional call order:

  1. Definition of variables -> let, let!
  2. subject
  3. before
  4. after

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed, but I vote for subject/let/before/after

Copy link
Member Author

Choose a reason for hiding this comment

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

That's the order they use in style guide
https://github.com/reachlocal/rspec-style-guide#good-example-2

@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch 3 times, most recently from b7625cb to e5d190a Compare April 13, 2018 13:28
@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch from e5d190a to ad5edd3 Compare May 4, 2018 11:40
@andrewr224 andrewr224 force-pushed the feature/padlock-auth branch from ad5edd3 to fa53410 Compare May 4, 2018 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants