# StateParameters (Ruby on Rails application) omniauth auth0

**URL:** <https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348>\
**Category:** Get Help\
**Tags:** other, missing-category\
**Created:** [August 10, 2023, 12:16pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348 "2023-08-10T12:16:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![csuntay](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/csuntay/32/49779_2.png) [@csuntay](https://community.auth0.com/u/csuntay)\
**Post date:** [August 10, 2023, 12:16pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/1 "2023-08-10T12:16:48Z")

</div>

I’m following this [article](https://auth0.com/docs/secure/attack-protection/state-parameters) and I don’t know how to add the start paramater to the request in the example: [tenant.auth0.com/authorize?...&state=xyzABC123](http://tenant.auth0.com/authorize?...&state=xyzABC123)

I’m following the quickstart guide after I create an RWA, add the routes, auth0controller and for the erb or front view this button  
\<%= button\_to “Have an account? Login”,  
“/auth/auth0”,  
method: :post,  
class: ‘link–primary-auth0’%\>  
\<% end %\>

DO I create a new method  
def authorize  
create the random hex for the state to pass to auth0 callback?

end  
Routes: get ‘/auth/authorize’ =\> ‘auth0#authorize’

---

<div class="post-metadata">

**Author:** ![csuntay](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/csuntay/32/49779_2.png) [@csuntay](https://community.auth0.com/u/csuntay)\
**Post date:** [August 10, 2023, 1:53pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/2 "2023-08-10T13:53:04Z")

</div>

**Front end**

```auto
<%= button_to “Have an account? Login”,
“/auth/auth0”,
method: :post,
class: ‘link–primary-auth0’%>
<% end %>

```

**Controller**

```auto
 def callback
    auth_info = request.env['omniauth.auth']
    session[:userinfo] = auth_info.dig('extra', 'raw_info')

    email = session.dig(:userinfo, :email)
    user = User.find_by(email: email)

    sign_in(user) if user.present?

    redirect_to '/'
  end

```

Routes

```auto
  get '/auth/auth0/callback' => 'auth0#callback'
  get '/auth/failure' => 'auth0#failure'
  get '/auth/logout' => 'auth0#logout'

```

---

<div class="post-metadata">

**Author:** ![csuntay](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/csuntay/32/49779_2.png) [@csuntay](https://community.auth0.com/u/csuntay)\
**Post date:** [August 10, 2023, 2:29pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/3 "2023-08-10T14:29:01Z")

</div>

I saw this thread: [Adding an extra param to auth0 call](https://community.auth0.com/t/adding-an-extra-param-to-auth0-call/57720)

but how will i add the state parameter here:

```auto
Rails.application.config.middleware.use OmniAuth::Builder do
  provider(
    :auth0,
    ENV["AUTH0_EXTERNAL_RWA_CLIENT_ID"],
    ENV["AUTH0_EXTERNAL_RWA_CLIENT_SECRET"],
    ENV["AUTH0_EXTERNAL_DOMAIN"],
    callback_path: '/auth/auth0/callback',
    authorize_params: {
      scope: 'openid profile email',
      connection: ENV["AUTH0_EXTERNAL_DATABASE_CONNECTION"],
      state: ???????
    }
  )
end

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 11, 2023, 2:31pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/4 "2023-08-11T14:31:11Z")

</div>

Hi @csuntay,

Welcome to the Auth0 Community!

You don’t need to manually add the state param to the request, it’s added by default.

I’m not an expert with ruby, but it looks like it’s implemented here:

> <https://github.com/omniauth/omniauth-oauth2/blob/3e7ee11c84153e6f0c19d38a5e63cda550f6925c/lib/omniauth/strategies/oauth2.rb#L63>

---

<div class="post-metadata">

**Author:** ![csuntay](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/csuntay/32/49779_2.png) [@csuntay](https://community.auth0.com/u/csuntay)\
**Post date:** [August 11, 2023, 2:36pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/5 "2023-08-11T14:36:23Z")

</div>

Hi Dan,

But I’m confused on how do i implement this? [Prevent Attacks and Redirect Users with OAuth 2.0 State Parameters](https://auth0.com/docs/secure/attack-protection/state-parameters)

I just want to redirect back to the last link before the authorization not the redirect\_to ‘/’ in the controller

```auto
def callback
    auth_info = request.env['omniauth.auth']
    session[:userinfo] = auth_info.dig('extra', 'raw_info')

    email = session.dig(:userinfo, :email)
    user = User.find_by(email: email)

    sign_in(user) if user.present?

    redirect_to '/'
  end

```

I tried redirect\_to request.original\_fullpath but i’m getting nothing

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 11, 2023, 2:48pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/6 "2023-08-11T14:48:14Z")

</div>

Does passing the state in the `authorize_params` object not work? Is it being overwritten?

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 25, 2023, 1:07pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/7 "2023-08-25T13:07:34Z")

</div>

A post was split to a new topic: [Custom state param in omniauth auth0](https://community.auth0.com/t/custom-state-param-in-omniauth-auth0/114500)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 27, 2023, 2:12pm UTC](https://community.auth0.com/t/stateparameters-ruby-on-rails-application-omniauth-auth0/113348/9 "2023-09-27T14:12:05Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
