# How to redirect to another url when auth failed

**URL:** https://meta.discourse.org/t/how-to-redirect-to-another-url-when-auth-failed/201264
**Category:** SSO
**Created:** [August 21, 2021, 2:33pm UTC](https://meta.discourse.org/t/how-to-redirect-to-another-url-when-auth-failed/201264 "2021-08-21T14:33:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cxlearndiscourse](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cxlearndiscourse](https://meta.discourse.org/u/cxlearndiscourse)
#### Post date: [August 21, 2021, 2:33pm UTC](https://meta.discourse.org/t/how-to-redirect-to-another-url-when-auth-failed/201264/1 "2021-08-21T14:33:56Z")

</div>

I would like to redirect to another url when auth failed.  
Any idea please thanks!

```plaintext
plugins
  myplugin
    lib
      auth
        my_auth_controller.rb
          def after_authenticate(auth_hash)

            if not auth
              redirect_to "https://example.com"
            end 

            result = Auth::Result.new
          end

```

---

<div class="post-metadata">

### Author: ![nehakakar](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/nehakakar/32/317424_2.png) [@nehakakar](https://meta.discourse.org/u/nehakakar)
#### Post date: [August 19, 2023, 9:58am UTC](https://meta.discourse.org/t/how-to-redirect-to-another-url-when-auth-failed/201264/2 "2023-08-19T09:58:55Z")

</div>

1. In your code, you have the **if not auth** condition, but there is no corresponding **else** statement.
2. The **redirect\_to** method should be used within a controller action. If you’re using it in a separate method like **after\_authenticate** , make sure it’s within the scope of a controller.  
So here is the correct code.

```plaintext
def after_authenticate(auth_hash)
  if not auth
    redirect_to "https://example.com"
  else
    # Authentication succeeded, continue with the authentication process
    result = Auth::Result.new
  end
end

```

The result world be, If the authentication fails **auth** is not true, it will redirect to “[https://example.com](https://example.com/)”. Otherwise, if authentication succeeds, it will continue with the authentication process.  
To verify, You may check browser developer tools to inspect network requests and redirects or use any online tool such as [redirect checker](https://redirectchecker.com/) This can help you identify redirection issue and status code.
