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

Json render #88

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ test/dummy/tmp/
test/dummy/.sass-cache
test/dummy/config/application.yml
coverage
.DS_Store
.idea/
72 changes: 44 additions & 28 deletions app/controllers/devise_token_auth/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ class PasswordsController < DeviseTokenAuth::ApplicationController
# sending emails
def create
unless resource_params[:email]
return render json: {
success: false,
@render = Hashie::Mash.new({
status: 'error',
errors: ['You must provide an email address.']
}, status: 401
})

return render 'devise_token_auth/passwords/create_errors', status: 401
end

unless params[:redirect_url]
return render json: {
success: false,
@render = Hashie::Mash.new({
status: 'error',
errors: ['Missing redirect url.']
}, status: 401
})

return render 'devise_token_auth/passwords/create_errors', status: 401
end

# honor devise configuration for case_insensitive_keys
Expand Down Expand Up @@ -47,11 +51,12 @@ def create
})

if @resource.errors.empty?
render json: {
success: true,
@render = Hashie::Mash.new({
status: 'success',
message: "An email has been sent to #{email} containing "+
"instructions for resetting your password."
}
'instructions for resetting your password.'
})
render 'devise_token_auth/passwords/create_success'
else
errors = @resource.errors
end
Expand All @@ -60,10 +65,12 @@ def create
end

if errors
render json: {
@render = Hashie::Mash.new({
success: false,
errors: errors
}, status: 400
})

render 'devise_token_auth/passwords/create_errors', status: 400
end
end

Expand Down Expand Up @@ -104,42 +111,51 @@ def edit
def update
# make sure user is authorized
unless @resource
return render json: {
success: false,
@render = Hashie::Mash.new({
status: 'error',
errors: ['Unauthorized']
}, status: 401
})
render 'devise_token_auth/passwords/update_errors', status: 401
end

# make sure account doesn't use oauth2 provider
unless @resource.provider == 'email'
return render json: {
success: false,
errors: ["This account does not require a password. Sign in using "+
@render = Hashie::Mash.new({
status: 'error',
errors: ['This account does not require a password. Sign in using '+
"your #{@resource.provider.humanize} account instead."]
}, status: 422
})

render 'devise_token_auth/passwords/update_errors', status: 422
end

# ensure that password params were sent
unless password_resource_params[:password] and password_resource_params[:password_confirmation]
return render json: {
success: false,
@render = Hashie::Mash.new({
status: 'error',
errors: ['You must fill out the fields labeled "password" and "password confirmation".']
}, status: 422
})

render 'devise_token_auth/passwords/update_errors', status: 422
end

if @resource.update_attributes(password_resource_params)
return render json: {
success: true,
@render = Hashie::Mash.new({
status: 'success',
data: {
user: @resource,
message: "Your password has been successfully updated."
message: 'Your password has been successfully updated.'
}
}
})

render 'devise_token_auth/passwords/update_success'
else
return render json: {
@render = Hashie::Mash.new({
success: false,
errors: @resource.errors
}, status: 422
})

render 'devise_token_auth/passwords/update_errors', status: 422
end
end

Expand Down
123 changes: 66 additions & 57 deletions app/controllers/devise_token_auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RegistrationsController < DeviseTokenAuth::ApplicationController

def create
@resource = resource_class.new(sign_up_params)
@resource.provider = "email"
@resource.provider = 'email'

# honor devise configuration for case_insensitive_keys
if resource_class.case_insensitive_keys.include?(:email)
Expand All @@ -18,97 +18,106 @@ def create

# success redirect url is required
unless params[:confirm_success_url]
return render json: {
@render = Hashie::Mash.new({
status: 'error',
data: @resource,
errors: ["Missing `confirm_success_url` param."]
}, status: 403
end
data: @resource,
errors: ['Missing `confirm_success_url` param.']
})

begin
# override email confirmation, must be sent manually from ctrl
resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
if @resource.save
return render 'devise_token_auth/registrations/missing_confirm_success_url'
end

unless @resource.confirmed?
# user will require email authentication
@resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: params[:confirm_success_url]
})
# override email confirmation, must be sent manually from ctrl
resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
if @resource.save

else
# email auth has been bypassed, authenticate user
@client_id = SecureRandom.urlsafe_base64(nil, false)
@token = SecureRandom.urlsafe_base64(nil, false)
unless @resource.confirmed?
# user will require email authentication
@resource.send_confirmation_instructions({
client_config: params[:config_name],
redirect_url: params[:confirm_success_url]
})

@resource.tokens[@client_id] = {
token: BCrypt::Password.create(@token),
expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
}
else
# email auth has been bypassed, authenticate user
@client_id = SecureRandom.urlsafe_base64(nil, false)
@token = SecureRandom.urlsafe_base64(nil, false)

@resource.save!
@resource.tokens[@client_id] = {
token: BCrypt::Password.create(@token),
expiry: (Time.now + DeviseTokenAuth.token_lifespan).to_i
}

update_auth_header
end
@resource.save!

render json: {
status: 'success',
data: @resource.as_json
}
else
clean_up_passwords @resource
render json: {
status: 'error',
data: @resource,
errors: @resource.errors.to_hash.merge(full_messages: @resource.errors.full_messages)
}, status: 403
update_auth_header
end
rescue ActiveRecord::RecordNotUnique

@render = Hashie::Mash.new({
status: 'success',
data: @resource.as_json
})

render 'devise_token_auth/registrations/create_success'
else
clean_up_passwords @resource
render json: {
status: 'error',
data: @resource,
errors: ["An account already exists for #{@resource.email}"]
}, status: 403

@render = Hashie::Mash.new({
status: 'error',
data: @resource,
errors: @resource.errors.to_hash.merge(full_messages: @resource.errors.full_messages)
})

render 'devise_token_auth/registrations/create_errors', status: 403
end

end

def update
if @resource

if @resource.update_attributes(account_update_params)
render json: {
@render = Hashie::Mash.new({
status: 'success',
data: @resource.as_json
}
})

render 'devise_token_auth/registrations/update_success'
else
render json: {
@render = Hashie::Mash.new({
status: 'error',
errors: @resource.errors
}, status: 403
})

render 'devise_token_auth/registrations/update_errors', status: 403
end
else
render json: {
@render = Hashie::Mash.new({
status: 'error',
errors: ["User not found."]
}, status: 404
errors: ['User not found.']
})

render 'devise_token_auth/registrations/update_errors', status: 404
end
end

def destroy
if @resource
@resource.destroy

render json: {
@render = Hashie::Mash.new({
status: 'success',
message: "Account with uid #{@resource.uid} has been destroyed."
}
})

render 'devise_token_auth/registrations/destroy_success'
else
render json: {
@render = Hashie::Mash.new({
status: 'error',
errors: ["Unable to locate account for destruction."]
}, status: 404
errors: ['Unable to locate account for destruction.']
})

render 'devise_token_auth/registrations/destroy_errors', status: 404
end
end

Expand Down
43 changes: 27 additions & 16 deletions app/controllers/devise_token_auth/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,33 @@ def create

sign_in(:user, @resource, store: false, bypass: false)

render json: {
@render = Hashie::Mash.new({
status: 'success',
data: @resource.as_json(except: [
:tokens, :created_at, :updated_at
])
}
})

render 'devise_token_auth/sessions/create_success', status: 401
elsif @resource and not @resource.confirmed?
render json: {
success: false,

@render = Hashie::Mash.new({
status: 'error',
errors: [
"A confirmation email was sent to your account at #{@resource.email}. "+
"You must follow the instructions in the email before your account "+
"can be activated"
'You must follow the instructions in the email before your account '+
'can be activated'
]
}, status: 401
})

render 'devise_token_auth/sessions/create_errors', status: 401
else
render json: {
errors: ["Invalid login credentials. Please try again."]
}, status: 401
@render = Hashie::Mash.new({
status: 'error',
errors: ['Invalid login credentials. Please try again.']
})

render 'devise_token_auth/sessions/create_errors', status: 401
end
end

Expand All @@ -65,14 +72,18 @@ def destroy
user.tokens.delete(client_id)
user.save!

render json: {
success:true
}, status: 200
@render = Hashie::Mash.new({
status: 'error'
})

render 'devise_token_auth/sessions/destroy_success'
else
render json: {
errors: ["User was not found or was not logged in."]
}, status: 404
@render = Hashie::Mash.new({
status: 'error',
errors: ['User was not found or was not logged in.']
})

render 'devise_token_auth/sessions/destroy_errors', status: 404
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/devise_token_auth/passwords/create_errors.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :message
2 changes: 2 additions & 0 deletions app/views/devise_token_auth/passwords/update_errors.json.rabl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :data
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :data, :errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :data
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object @render
attributes :status, :message
Loading