-
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.
Merge pull request #14 from zmekonnen251/authentication_and_authoriza…
…tion Authentication and authorization
- Loading branch information
Showing
51 changed files
with
1,446 additions
and
30 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
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
85 changes: 85 additions & 0 deletions
85
app/controllers/api/v1/mentors/registrations_controller.rb
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,85 @@ | ||
class Api::V1::Mentors::RegistrationsController < Devise::RegistrationsController | ||
# before_action :configure_sign_up_params, only: [:create] | ||
# before_action :configure_account_update_params, only: [:update] | ||
respond_to :json | ||
|
||
# GET /resource/sign_up | ||
# def new | ||
# super | ||
# end | ||
|
||
# POST /resource | ||
def create | ||
build_resource(sign_up_params) | ||
|
||
resource.save | ||
|
||
if resource.persisted? | ||
render json: { message: 'Signed up successfully' }, status: :created | ||
else | ||
render json: { errors: resource.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# GET /resource/edit | ||
# def edit | ||
# super | ||
# end | ||
|
||
# PUT /resource | ||
# def update | ||
# super | ||
# end | ||
|
||
# DELETE /resource | ||
# def destroy | ||
# super | ||
# end | ||
|
||
# GET /resource/cancel | ||
# Forces the session data which is usually expired after sign | ||
# in to be expired now. This is useful if the user wants to | ||
# cancel oauth signing in/up in the middle of the process, | ||
# removing all OAuth session data. | ||
# def cancel | ||
# super | ||
# end | ||
|
||
# protected | ||
|
||
# If you have extra params to permit, append them to the sanitizer. | ||
# def configure_sign_up_params | ||
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) | ||
# end | ||
|
||
# If you have extra params to permit, append them to the sanitizer. | ||
# def configure_account_update_params | ||
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) | ||
# end | ||
|
||
# The path used after sign up. | ||
# def after_sign_up_path_for(resource) | ||
# super(resource) | ||
# end | ||
|
||
# The path used after sign up for inactive accounts. | ||
# def after_inactive_sign_up_path_for(resource) | ||
# super(resource) | ||
# end | ||
|
||
private | ||
|
||
def respond_with(resource, _opts = {}) | ||
register_success && return if resource.persisted? | ||
|
||
register_failed | ||
end | ||
|
||
def register_success | ||
render json: { message: 'Signed up successfully.' }, status: :ok | ||
end | ||
|
||
def register_failed | ||
render json: { message: 'Something went wrong.' }, status: :unprocessable_entity | ||
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,59 @@ | ||
class Api::V1::Mentors::SessionsController < Devise::SessionsController | ||
# before_action :configure_sign_in_params, only: [:create] | ||
respond_to :json | ||
|
||
def create | ||
possible_aud = request.headers['HTTP_JWT_AUD'].presence || request.headers['JWT_AUD'].presence | ||
self.resource = warden.authenticate!(auth_options) | ||
sign_in(resource_name, resource) | ||
if mentor_signed_in? | ||
last = resource.allowlisted_mentors_jwts.where(aud: possible_aud).last | ||
aud = possible_aud || 'UNKNOWN' | ||
|
||
if last.present? | ||
last.update_columns({ | ||
browser_data: params[:browser], | ||
os_data: params[:os], | ||
remote_ip: params[:ip] | ||
}) | ||
aud = last.aud | ||
end | ||
|
||
respond_with(resource, { aud: }) | ||
else | ||
render json: resource.errors, status: 401 | ||
end | ||
rescue StandardError | ||
render json: { errors: I18n.t('api.oops') }, status: 500 | ||
end | ||
|
||
private | ||
|
||
def current_token | ||
request.env['warden-jwt_auth.token'] | ||
end | ||
|
||
def respond_with(resource, opts = {}) | ||
render json: { | ||
mentor: resource, | ||
jwt: current_token, | ||
aud: opts[:aud] | ||
} | ||
end | ||
|
||
def respond_to_on_destroy | ||
render json: { message: 'Signed out successfully' } | ||
end | ||
|
||
# DELETE /resource/sign_out | ||
# def destroy | ||
# super | ||
# end | ||
|
||
# protected | ||
|
||
# If you have extra params to permit, append them to the sanitizer. | ||
# def configure_sign_in_params | ||
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) | ||
# 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,31 @@ | ||
class Api::V1::Users::RegistrationsController < Devise::RegistrationsController | ||
respond_to :json | ||
|
||
def create | ||
build_resource(sign_up_params) | ||
|
||
resource.save | ||
|
||
if resource.persisted? | ||
render json: { message: 'Signed up successfully' }, status: :created | ||
else | ||
render json: { errors: resource.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def respond_with(resource, _opts = {}) | ||
register_success && return if resource.persisted? | ||
|
||
register_failed | ||
end | ||
|
||
def register_success | ||
render json: { message: 'Signed up successfully.' }, status: :ok | ||
end | ||
|
||
def register_failed | ||
render json: { message: 'Something went wrong.' }, status: :unprocessable_entity | ||
end | ||
end |
Oops, something went wrong.