Skip to content
This repository

'Could not authorize you from Facebook because "Csrf detected".' #73

tomsoderlund opened this Issue July 23, 2012 · 50 comments
Joel Van Horn
joelvh commented July 26, 2012

Omniauth-oauth2 causes the CSRF detected error because something is redirecting to the callback URL twice in a row. First time the "state" value is there and your callback processes. Then immediately after, something redirects to the callback again, but the second time, the "state" value has been deleted from session and causes the error.

Here is where the state is verified and removed from session.

The question is, what is redirecting to the callback twice in a row? It only happens once in a while for me. My guess is Devise is accidentally storing the callback URL as the redirect URL? But not sure.....

Aidan Feldman

I'm seeing this twice in my logs each time I authorize:

(facebook) Callback phase initiated.

I'm trying to get post-auth redirects working with omniauth+devise, but it seems that the double redirects are clearing session['omniauth.origin'] as well. As a workaround, I created a new controller action that essentially does the following:

# app/controllers/omniauth_controller.rb

# GET /auth/fb_sso_proxy?return_to=...
def fb_sso_proxy
  session[:user_return_to] = params[:return_to] if params[:return_to]
  redirect_to user_omniauth_authorize_path(:facebook)

and do my authorization via Facebook through there - the redirect is then handled by Devise instead of OmniAuth. Would love to be able to remove this, though.

Mike Gaffney

I'm getting the same issue but my error message unhelpful:

Started GET "/auth/facebook/callback?signed_request=REDACTED" for at 2012-08-08 23:18:32 +0000
(facebook) Callback phase initiated.
(facebook) Authentication failure! invalid_credentials: OmniAuth::Strategies::OAuth2::CallbackError, OmniAuth::Strategies::OAuth2::CallbackError

Started GET "/auth/failure?message=invalid_credentials&strategy=facebook" for at 2012-08-08 23:18:32 +0000

ActionController::RoutingError (No route matches [GET] "/auth/failure"):

If I take the csrf check out things work fine

Jason Green

+1 on this however I only get it when using iphone and not through browser

Aaron Michal

in my initial tests it seems to be related browser caching. Shift-reload in Chrome works always. If i let the browser request caching it fails (most of the time)

Ryan Clark

I am getting this error with or without sandbox enabled. Can't seem to find a real solution...

Ilya Scharrenbroich

Got the same problem, seems to only happen when my app gets embedded in the FB Canvas. I just switch back to the 1.4.0 omniauth-facebook gem and it works. I hope someone can fix this issue.

Daniel Podaru

+1, same problem, only when using Canvas. Downgraded to 1.4.0 and it seems to work.

Adam Braus

+1 getting this problem when I try to upgrade.

Ian Drysdale

+1, Downgraded to 1.4.0 and all seems good again?

Jacky Alciné

We shouldn't have to downgrade to get this working.

I propose (since I'm kind of busy but I might try to do this) to do a diff of 1.4.0 and 1.4.1 and see what on Earth changed that made this happen.

Ryan Clark

I fixed this in my app and it now works with 1.4.1. Unfortunately, I fixed it many commits ago and am not entirely sure of the solution, but I definitely found that I had the FB credentials in both my devise and my omniauth initializers. This could have been making FB think I was trying to login more than once. Let me know if this helps.. I can take a closer look and see what else I did if this isn't the fix you are all looking for.

Jacky Alciné

@IAMRYO that makes sense, but if that's the case, when Devise and Omniauth are used in collaboration and a strategy is defined in both libraries, one should take precedence over the other. If they're the same, then just use Devise's configuration.

Daniel Podaru

@IAMRYO , That might be a different problem. I'm not using omniauth-facebook with devise.

Jacky Alciné

I'm not sure if it does, but if Omniauth can communicate with its parent authorization scheme, then it shouldn't be breaking DRY.


@IAMRYO Thank you! I had the same problem, where I first defined an omniauth initializer and then added the strategy to the devise config. Removing the omniauth initializer fixed this for me.


+1 -- works on 1.4.0, but not 1.4.1. FWIW, I am not using Devise ... just straight-up OAuth2 + omniauth-facebook


+1 had a devise initializer with an omniauth section and separate omniauth initializer. both defined fb credentials. removing one and restarting worked.

Gregory McCue

@miraclecoder -- +1 This worked for me too. I'm using Devise and Omniauth-facebook, removing the omniauth Facebook initialization removed the duplicate callback phase.

Martin Gogov

@IAMRYO , +1
Removing the devise config from a devise + omniauth app worked fine (omniauth-facebook 1.4.1)

Marius Butuc
  1. Ensured that the FB credentials are initialized only once
  2. Downgraded to omniauth-facebook v1.4.0, that relies on pre-1.1+ omniauth-oauth2 (1.0.3)

Same issue:

Could not authenticate you from Facebook because "Csrf detected".

It happens on heroku, yet I cannot reproduce on dev.

Jeremy Haile

Happening for me too with devise and omniauth-facebook. Just spent about 5 hours trying to get it working before finding this!


FYI - this same 'Csrf detected' problem can also be caused by having a domain name mismatch between your live site and the site url configured within your facebook application.


+1 in case there was any doubt. Removed my omniauth initializer and it started working again (1.4.1)

Betjamin Richards

Running into the same issue here with Rails 3.2.13 and Omniauth_facebook 1.4.1 . Downgrading the 1.4.0 didn't fix for me and I'm only calling it in the devise initializer.

Betjamin Richards

And the mystery deepens...

Using the exact same account I can authenticate with Facebook correctly using Google Chrome, but using Safari I encounter the afforementioned error.

Safari version: Version 6.0.4 (7536.29.13)
Google Chrome version: Version 27.0.1453.93

Betjamin Richards

I've fixed this issue now. Mine was caused by another issue to the one described above. Posting here for anybody else who may have a similar cause:

Zolzaya Erdenebaatar

+1 getting this problem

novito commented July 06, 2013

I'm getting this issue with Rails 4.

Sergio Tapia Gutierrez

Running into this issue on Rails 4 as well. What should we do?

Cezar Halmagean

+1 same here (rails 4) while using JS


Did you all try #73 (comment)


In my case, it turned out to be somewhat of a red herring; the first issue was that I was still in sandbox mode so the domain didn't match in production, and the second issue had to do with certain data unexpectedly missing from the auth hash that Facebook returned. Regardless, neither problem was related to the omniauth-facebook gem; I've been using Rails 4.0 and Ruby 2.0 for months now and I haven't experienced any other issues.


In my case downgrading omniauth-facebook to 1.4.0 made it work.

Josef Chmel

One of possible reasons : response from facebook "[14/Aug/2013:11:33:07 +0200] "GET /users/auth/facebook/callback?error_code=2000&error_message=The+specified+feature+has+been+temporarily+disabled+for+this+application&state=75efb6d35c7988871ff407f039c799a0fb8ae1f27a9d0fd5 HTTP/1.1" 500 441 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0""

But information in message is misleading. Right reason is that user is underage for example (app has age restriction).

Ilya Katz

downgrade worked

kuon commented August 20, 2013

Seems linked to #107 and #70

artem avetisyan



Telling OmniAuth to ignore the state(:provider_ignores_state => true) worked both with 1.4.1 and 1.4.0.

provider :facebook, APP_CONFIG['FB_APP_ID'], APP_CONFIG['FB_APP_SECRET'], {:scope => fb_permissions, :provider_ignores_state => true}

This is where I found the fix

I am using
omniauth (1.1.4)
omniauth-facebook (1.4.1)

Christos Vontas

Same issue here too!
I am using Rails 4.0 and Ruby 2.0, without Devise at the time, so fix from @betjaminrichards doesn't apply.
Also, tried the fix from @binnyg but it didn't work either.

Only downgrading worked for me.

Josef Šimánek

Can anyone try if this #96 works?

/cc @cv711

Rémi Vion

avec rails 4 et ruby 2:
omniauth seulement:

  • provider :facebook, xxxxxxxxx, yyyyyyyyy, {:provider_ignores_state => true}

omniauth + devise:

  • config.omniauth :facebook, xxxxxxxxx, yyyyyyyyy, {:provider_ignores_state => true}
Ken Ip kenips referenced this issue in doorkeeper-gem/doorkeeper November 27, 2013

CSRF Support? #324

Mark Dodwell

This is the same issue as #107. Closing this one to avoid splitting the discussion further.

Mark Dodwell mkdynamic closed this December 11, 2013

Since this is open, plus I found this via Google more readily than #107, I'll leave this here in hopes it will help future generations:

I am on omniauth-facebook 1.6.0 and still needed to add provider_ignores_state: true in my Omniauth config like so:

config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], {
  strategy_class: OmniAuth::Strategies::Facebook,
  provider_ignores_state: true,

After adding that config option, the CSRF error goes away.

Guilherme Simões

provider_ignores_state: true had already been mentioned. However, be aware that you are making your app susceptible to CSRF attacks by using that option.

provider_ignores_state: true is NOT a permanent solution.

Barry Allard

@guilhermesimoes Is there a non chmod 777 option? As much as I'd like to engage Igor H., I'd prefer that we not have a security fail too soon.

Guilherme Simões

True, you wouldn't want to mess with Homakov.

This is probably not the answer you're looking for but omniauth-facebook should work out of the box. If you use it solo (without any other gem) and set it up properly it should work. I'm using it with devise at this moment and it's working fine as well.

However, it seems to me like any deviation from the normal omniauth-facebook flow seems to trigger CSRF detection. So far, here are some of the things that seem to do it:

  • Defining your app credentials in two different initializers, usually omniauth.rb and devise.rb. source

  • Having a domain name mismatch between your live site and the site url configured within your facebook application. source

  • Leaving the facebook application in sandbox mode, so the domain name doesn't match the production one. source

  • Adding a before_filter :authenticate to the OmniauthCallbacksController or ApplicationController (since OmniauthCallbacksController inherits from ApplicationController). source

  • Using omniauth-facebook in conjunction with Facebook's client-side flow. source

  • Messing with the state param.

Barry Allard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.