-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: dev
Are you sure you want to change the base?
Conversation
andrewr224
commented
Apr 6, 2018
•
edited
Loading
edited
- Create delete job on token creation
- Improve phone/mail validation
- Implement email verification
- Implement reset password
117ed28
to
52caa23
Compare
key_type: type, | ||
expired_at: expired_at | ||
) | ||
ExpireTokenJob.set(wait_until: token.expired_at).perform_now(token_id: token.id) if token |
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it work? :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why wouldn't it?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
app/jobs/expire_token_job.rb
Outdated
class ExpireTokenJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(params = {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**params
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def initialize(params = {}) | ||
@user = params[:user] | ||
@type = params[:type] || :auth | ||
@expired_at = params[:expired_at] || Time.zone.now + 1.month |
There was a problem hiding this comment.
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
2b286f8
to
136fcbb
Compare
136fcbb
to
ad3964b
Compare
8ab1e3c
to
bbeef09
Compare
There was a problem hiding this 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? |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user_valid?
app/jobs/expire_token_job.rb
Outdated
@@ -0,0 +1,9 @@ | |||
class ExpireTokenJob | |||
include Sidekiq::Worker |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
app/jobs/expire_token_job.rb
Outdated
class ExpireTokenJob | ||
include Sidekiq::Worker | ||
|
||
def perform(params = {}) |
There was a problem hiding this comment.
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.
spec/factories/identity.rb
Outdated
factory :identity do | ||
trait :email do | ||
uid { Faker::Internet.email } | ||
provider 'email'.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not symbol?
spec/factories/identity.rb
Outdated
|
||
trait :phone do | ||
uid { Faker::PhoneNumber.phone_number } | ||
provider 'phone'.freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
spec/jobs/expire_token_job_spec.rb
Outdated
|
||
subject { described_class.perform_at(token.expired_at, token_id: token.id) } | ||
|
||
let!(:token) { create(:token) } |
There was a problem hiding this comment.
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:
- Definition of variables ->
let
,let!
subject
before
after
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
b7625cb
to
e5d190a
Compare
e5d190a
to
ad5edd3
Compare
ad5edd3
to
fa53410
Compare