-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release 0.0.2
- Loading branch information
Showing
55 changed files
with
1,562 additions
and
447 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
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 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
class MainController < ApplicationController | ||
before_action :restrict_to_development | ||
before_action :authorize_request | ||
before_action :authenticate_user! | ||
|
||
def index | ||
render json: format_response(payload: 'You are logged in'), status: :ok | ||
payload = {msg: 'You are logged in', current_user: current_user} | ||
render json: format_response(payload: payload), status: :ok | ||
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,20 @@ | ||
class RegistrationsController < Devise::RegistrationsController | ||
respond_to :json | ||
|
||
def create | ||
build_resource(register_params) | ||
|
||
resource.save | ||
if resource.persisted? | ||
render json: format_response, status: :created | ||
else | ||
render json: format_response(errors: resource.errors.full_messages), status: :bad_request | ||
end | ||
end | ||
|
||
def register_params | ||
params.permit( | ||
:login, :password, :name, :email, :gender, :birthday | ||
) | ||
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,48 @@ | ||
class SessionsController < Devise::SessionsController | ||
before_action :validate_params, only: [:create] | ||
before_action :pass_params_to_user, only: [:create] | ||
|
||
respond_to :json | ||
|
||
def create | ||
self.resource = warden.authenticate(auth_options) | ||
if resource.nil? | ||
unauthorized_request(18) | ||
else | ||
sign_in(resource_name, resource) | ||
payload = {token: jwt_token} | ||
render json: format_response(payload: payload), status: :ok | ||
end | ||
end | ||
|
||
def respond_to_on_destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def pass_params_to_user | ||
request.params[:user] = ActiveSupport::HashWithIndifferentAccess.new( | ||
login: params[:login], email: params[:email], password: params[:password] | ||
) | ||
end | ||
|
||
def jwt_token | ||
request.env['warden-jwt_auth.token'] | ||
end | ||
|
||
def validate_params | ||
# Conditions to test and error code if not pass | ||
conditions = [ | ||
[(params[:login].present? or params[:email].present?), 28], | ||
[params[:password].present?, 8], | ||
[(params[:login].instance_of?(String) or params[:email].instance_of?(String)), 17], | ||
[params[:password].instance_of?(String), 17] | ||
] | ||
errors = conditions.collect { |condition| ErrorCodes.get_error_message(condition[1]) unless condition[0] } | ||
errors = errors.uniq.compact | ||
unless errors.empty? | ||
render json: format_response(errors: errors), status: :bad_request | ||
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,36 @@ | ||
class DeviseFailureApp < Devise::FailureApp | ||
def http_auth_body | ||
json_error_response | ||
end | ||
|
||
def self.format_response(args = {}) | ||
if not args[:errors].nil? | ||
validate_response_errors(args[:errors]) | ||
else | ||
args[:errors] = [] | ||
end | ||
|
||
args[:errors] = [args[:errors]] if args[:errors].instance_of? String | ||
args[:errors].uniq! | ||
args[:errors] = ErrorCodes.get_errors_by_messages(args[:errors]) | ||
return {data: args[:payload], errors: args[:errors]} | ||
end | ||
|
||
protected | ||
|
||
def json_error_response | ||
self.status = 401 | ||
self.content_type = "application/json" | ||
self.response_body = DeviseFailureApp.format_response(errors: i18n_message).to_json | ||
end | ||
|
||
def self.validate_response_errors(errors) | ||
raise_message = 'errors must be string or string list' | ||
unless errors.instance_of? String | ||
raise ArgumentError, raise_message unless errors.respond_to? :select | ||
if not errors.empty? and (errors.select {|s| s.instance_of? String}).empty? | ||
raise ArgumentError, raise_message | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.