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 #29 from sad16/12-oauth
oauth
- Loading branch information
Showing
41 changed files
with
1,041 additions
and
28 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
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 AuthorizationsController < ApplicationController | ||
rescue_from ActiveRecord::RecordNotFound, with: :not_found | ||
|
||
def enter_email | ||
@auth = Authorization.find_by!(enter_email_token: params[:enter_email_token]) | ||
Services::Authorizations::CheckEnterEmail.new.call(@auth) | ||
|
||
rescue Services::Authorizations::CheckEnterEmail::ExpiredError | ||
redirect_to root_path, alert: 'Time to enter email has expired' | ||
end | ||
|
||
def update_email | ||
auth = Authorization.find_by!(enter_email_token: params[:enter_email_token]) | ||
auth = Services::Authorizations::UpdateEmail.new.call(auth, params[:email]) | ||
Services::Authorizations::SendConfirmationMail.new.call(auth) | ||
|
||
redirect_to new_user_session_path, notice: 'Сonfirmation mail was sent to the email' | ||
rescue Services::Authorizations::UpdateEmail::ExpiredError | ||
redirect_to root_path, alert: 'Time to enter email has expired' | ||
end | ||
|
||
def confirm_email | ||
auth = Authorization.find_by!(confirm_email_token: params[:confirm_email_token]) | ||
Services::Authorizations::ConfirmEmail.new.call(auth) | ||
|
||
redirect_to new_user_session_path, notice: 'Email confirmed. You can sign in' | ||
rescue Services::Authorizations::ConfirmEmail::ExpiredError | ||
redirect_to root_path, alert: 'Time to confirm email has expired' | ||
end | ||
|
||
private | ||
|
||
def not_found | ||
head 404 | ||
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 OauthCallbacksController < Devise::OmniauthCallbacksController | ||
def github | ||
result = Services::Authorizations::Oauth.new.call(omniauth_data) | ||
redirect_process(result) | ||
end | ||
|
||
def vkontakte | ||
result = Services::Authorizations::Oauth.new.call(omniauth_data) | ||
redirect_process(result) | ||
end | ||
|
||
private | ||
|
||
def redirect_process(oauth) | ||
if oauth[:user] | ||
sign_in_and_redirect oauth[:user], event: :authentication | ||
|
||
kind = oauth[:auth].provider.to_s.capitalize | ||
set_flash_message(:notice, :success, kind: kind) if is_navigational_format? | ||
elsif oauth[:auth] | ||
token = oauth[:auth].enter_email_token | ||
redirect_to auth_enter_email_path(enter_email_token: token), notice: 'Enter your email for confirmation' | ||
else | ||
redirect_to root_path, alert: 'Something went wrong' | ||
end | ||
end | ||
|
||
def omniauth_data | ||
request.env['omniauth.auth'] | ||
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,6 @@ | ||
class AuthorizationMailer < ApplicationMailer | ||
def confirmation_email(auth) | ||
@auth = auth | ||
mail(to: @auth.email, subject: 'Confirmation Email') | ||
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,6 @@ | ||
class Authorization < ApplicationRecord | ||
belongs_to :user, optional: true | ||
|
||
validates :provider, :uid, presence: true | ||
validates :uid, uniqueness: { scope: [:provider] } | ||
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
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,19 @@ | ||
module Services | ||
module Answers | ||
class MarkAsBest < ApplicationService | ||
class Error < StandardError; end | ||
class UserNotAuthorError < Error; end | ||
|
||
def call(user, answer) | ||
if user.author_of?(answer.question) | ||
ActiveRecord::Base.transaction do | ||
answer.mark_as_best | ||
answer.assign_reward | ||
end | ||
else | ||
raise UserNotAuthorError | ||
end | ||
end | ||
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
class ApplicationService | ||
def call | ||
raise NotImplementedError | ||
module Services | ||
class ApplicationService | ||
def call | ||
raise NotImplementedError | ||
end | ||
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,17 @@ | ||
module Services | ||
module Authorizations | ||
class CheckEnterEmail < ApplicationService | ||
class Error < StandardError; end | ||
class ExpiredError < Error; end | ||
|
||
def call(auth) | ||
if auth.enter_email_token_expires_at < Time.current | ||
auth.update!(enter_email_token: nil, enter_email_token_expires_at: nil) | ||
raise ExpiredError | ||
end | ||
|
||
true | ||
end | ||
end | ||
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,21 @@ | ||
module Services | ||
module Authorizations | ||
class ConfirmEmail < ApplicationService | ||
class Error < StandardError; end | ||
class ExpiredError < Error; end | ||
|
||
def call(auth) | ||
if auth.confirm_email_token_expires_at < Time.current | ||
auth.update!(confirm_email_token: nil, confirm_email_token_expires_at: nil) | ||
raise ExpiredError | ||
end | ||
|
||
auth.update!( | ||
email_confirmation_at: Time.current, | ||
confirm_email_token: nil, | ||
confirm_email_token_expires_at: nil | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.