Trying to Sign in with Apple on my Rails6 app. It looks like I may be making multiple omniauth calls? But I don't have anything referenced in my devise.rb file.
Here's the error log:
> Started POST "/users/auth/apple" for 71.232.157.93 at 2021-11-18
> 15:58:12 +0000 DEBUG -- omniauth: (apple) Request phase initiated.
> Started POST "/users/auth/apple/callback" for 71.232.157.93 at
> 2021-11-18 15:58:17 +0000 DEBUG -- omniauth: (apple) Callback phase
> initiated. ERROR -- omniauth: (apple) Authentication failure!
> nonce_mismatch: OmniAuth::Strategies::OAuth2::CallbackError,
> nonce_mismatch | nonce mismatch
> Processing by CallbacksController#failure as HTML
> HTTP Origin header (https://appleid.apple.com) didn't match request.base_url (https://www.mywebsite.com)
> Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms | Allocations: 292)
> ERROR -- omniauth: (apple) Authentication failure! ActionController::InvalidAuthenticityToken: ActionController::InvalidAuthenticityToken, ActionController::InvalidAuthenticityToken
> Processing by CallbacksController#failure as HTML
> HTTP Origin header (https://appleid.apple.com) didn't match request.base_url (https://www.mywebsite.com)
> Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms | Allocations: 162)
> ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
gemfile
gem 'omniauth-apple'
gem "omniauth-rails_csrf_protection", "~> 1.0"
routes.rb:
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
config/initializers/omniauth.rb:
Rails.application.config.middleware.use OmniAuth::Builder do
result = OpenSSL::PKey::EC.new(env[APPLE_PEM])
key = result.to_pem
provider :apple, 'com.myapp.auth', '',
{
scope: 'email name',
team_id: 'ENV[APPLE_TEAM_ID]',
key_id: 'ENV[APPLE_KEY_ID]',
pem: key,
redirect_uri: 'https://www.mywebsite.com/users/auth/apple/callback',
provider_ignores_state: true
}
end
user.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:omniauthable, :omniauth_providers => [:apple]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.phone = '000-000-0000'
user.password = Devise.friendly_token[0,20]
user.remember_me = true
end
end
callbacks_controller.rb
class CallbacksController < Devise::OmniauthCallbacksController
def apple
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
view.html.erb
<%= link_to user_apple_omniauth_authorize_path, method: 'post' do %>
<div id="appleid-signin" class="apple-signin"></div>
<% end %>