# R shiny app goes back to login page when it refreshes

**URL:** <https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049>\
**Category:** Get Help\
**Tags:** login-prompt-new-experience, login-experience\
**Created:** [December 18, 2024, 9:58am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049 "2024-12-18T09:58:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [December 18, 2024, 9:58am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/1 "2024-12-18T09:58:19Z")

</div>

Hi there,  
I have a R shiny app using shiny + bslib that uses auth0 package for authentication. It works fine for authentication, however, when user refreshes the page then the app reloads the login page again. I understand R shiny is a SPA application. I have used this package before on another shiny app successfully and when refreshed it kept the user logged in. The difference was the package I was using for the interface, back then I used shiny4Dash instead.

I have reviewed the following FAQ why-is-authentication-lost-after-refreshing-my-single-page-application, but no luck with the suggestions from there.

Would you have any ideas or pointers where to investigate the problem?

Thanks!

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [December 31, 2024, 4:13pm UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/3 "2024-12-31T16:13:31Z")

</div>

Hi @u1159820,

Are you using any Auth0 SDKs in this implementation?

Thanks,

Mary Beth

---

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [January 5, 2025, 9:08am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/4 "2025-01-05T09:08:02Z")

</div>

Hi @marybeth.hunter , I believe I am but it’s wrapped into an R package, in other words, I’m not using the SDK directly but using this R wrapper around it in the form of an R package. The strage thing is that I’ve used this same package on another app and it’s working fine, but this time it’s not working so other factors might be affecting this. I just don’t know where else to look.

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [January 6, 2025, 2:51pm UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/5 "2025-01-06T14:51:32Z")

</div>

Hi @u1159820,

Thanks for your reply!

I understand - can you provide as much information about the Auth0 implementation as possible? Screenshots, code snippets, etc., of any Auth0-related code, will be helpful for me to troubleshoot.

Thanks,

Mary Beth

---

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [January 17, 2025, 2:31am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/6 "2025-01-17T02:31:34Z")

</div>

Hi @marybeth.hunter ,

I’ll try to provide as much as possible. This is a reprex app in Shiny using auth0 R package to manage authentication.

```auto
library(shiny)
library(bslib)
library(ggplot2)
library(auth0)
data(penguins, package = "palmerpenguins")

ui <- page_sidebar(
  title = "Penguins dashboard",
  logoutButton(label = "Logout", id = "logout"),
  sidebar = sidebar(
    title = "Histogram controls",
    varSelectInput(
      "var", "Select variable",
      dplyr::select_if(penguins, is.numeric)
    ),
    numericInput("bins", "Number of bins", 30)
  ),
  card(
    card_header("Histogram"),
    plotOutput("p")
  )
)

server <- function(input, output, session) {
  output$p <- renderPlot({
    ggplot(penguins) +
      geom_histogram(aes(!!input$var), bins = input$bins) +
      theme_bw(base_size = 20)
  })
  
  observeEvent(input$logout, {
    logout()
  })
}

# shinyApp(ui, server)
auth0::shinyAppAuth0(ui, server, config_file = "_auth0.yml")

```

This can be run from RStudio (Run app as run external). The code above should be in a file named app.R. To work with auth0 package we use a yml file named \_auth0.yml located in the same folder as app.R, with the following content:

```auto
name: app_name
remote_url: 'https://remote_server.com'
auth0_config:
  api_url: !expr paste0('https://', env$AUTH0_USER, '.auth0.com')
  credentials:
    key: !expr env$AUTH0_KEY
    secret: !expr env$AUTH0_SECRET

```

For the app to work I’ve created and configured an Auth0 application on my [auth0.com](http://auth0.com) account with the corresponding callback URLs, allowed logout URLs, and allowed web origins to locahost:3838 (to work with the app locally), and remote\_server.com (when the app is deployed).

Now, about the issue, when I refresh the app in the browser there are two scenarios I’ve tested:

1. URL with code: [http://localhost:3838/?code=ffOmIXFJ2iGIlURnGzLGinqKzbDgQoyh5Xtlr3gMhgLfO&state=MdQUSLanHc](http://localhost:3838/?code=ffOmIXFJ2iGIlURnGzLGinqKzbDgQoyh5Xtlr3gMhgLfO&state=MdQUSLanHc)  
When I refresh the app in this scenario I get this error and the app stops.

```auto
Error in httr::oauth2.0_access_token: Forbidden (HTTP 403). Failed to get an access token.
  57: <Anonymous>
  56: stop
  55: stop_for_status
  54: httr::oauth2.0_access_token
  53: auth0_server_verify
  40: server
   3: runApp
   2: print.shiny.appobj
   1: <Anonymous>
Error in httr::oauth2.0_access_token(api, app(redirect_uri), params$code) : 
  Forbidden (HTTP 403). Failed to get an access token.

```

From the Auth0 dashboard, monitor console I get this message:

```auto
2025-01-17T13:53:34.946Z

Failed Exchange
Invalid authorization code

{
  "date": "2025-01-17T13:53:34.946Z",
  "type": "feacft",
  "description": "Invalid authorization code",
  "connection_id": "",

```

1. URL without the above code: [http://localhost:3838](http://localhost:3838)  
Using javscript I’ve been able to remove that code and now when the app is refreshed it doesn’t crash, but I takes the user back to login page even right after being logged in.

There is some information I can get from the auth0 package about the credentials: the access token, id\_token, scope (comes back with ‘openid profile’), expires\_in (comes back with ‘86400’) and token\_type (comes back with ‘Bearer’).

Complementing my reprex app, I’m going to leave this github repo link with instructions on how to integrated the auth0 package into a shiny app. [GitHub - curso-r/auth0: Authentication in shinyapps using Auth0 service](https://github.com/curso-r/auth0)

I hope this is enough info for you to debug and provide some light around why it doesn’t keep the authentication on when refreshing the app. I’ve used this package in another project in the past and this wasn’t happening. All I can think of is changes happened on the Auth0 side.

Regards,  
Christian.

---

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [January 23, 2025, 6:35am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/7 "2025-01-23T06:35:33Z")

</div>

Hi @marybeth.hunter , have you had any chance to look into this?

regards,  
Christian.

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [January 23, 2025, 10:10pm UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/8 "2025-01-23T22:10:40Z")

</div>

Hi @u1159820,

Thank you for your patience!

I have been looking into this and have a few things to look into:

- Since I am unfamiliar with Shiny, are there any session management options? I wonder if the session is being reset on refresh.
- Are you using `localStorage`?
- Can you see if the cookies are being cleared on refresh in the browser dev tools?

I wonder if changing the application type to Single Page Application would help. Currently, I see that your application is set to Regular Web Application.

Let me know!

Thanks,

Mary Beth

---

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [January 24, 2025, 4:31am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/10 "2025-01-24T04:31:46Z")

</div>

Hi @marybeth.hunter ,  
I’ve run more tests on my side and maybe you can validate this for me but it looks like the issue I’m having is related to the location of the tenant?  
This last test I did was to create a new account with a tenant in Australia where I’m logging from and when I created a new application with all the default values then I was able to refresh the app without losing access or being redirected to the login page again. It’s either something wrong with this current tenant I’m running or the location of the tenant matters in the app refresh process.

I’ll integrate this with my app and see if solves it completely.

Regards,  
Christian.

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [January 30, 2025, 7:23pm UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/11 "2025-01-30T19:23:01Z")

</div>

Hi @u1159820,

Interesting! Let me know if this solved your issue.

Thanks,

Mary Beth

---

<div class="post-metadata">

**Author:** ![u1159820](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/u1159820/32/67894_2.png) [@u1159820](https://community.auth0.com/u/u1159820)\
**Post date:** [January 31, 2025, 1:32am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/12 "2025-01-31T01:32:37Z")

</div>

Hi @marybeth.hunter ,  
I can confirm now that my issue is solved. Not sure what was causing it but the only difference between the two tenants AFAIK was the location. The one with the problem was in the US and this new one is in AU. So, I’m closing this post now as I’ve resolved it by creating a new tenant in the same location where I’m logging from.

Regards,  
Christian.

---

<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:** [February 14, 2025, 1:32am UTC](https://community.auth0.com/t/r-shiny-app-goes-back-to-login-page-when-it-refreshes/164049/13 "2025-02-14T01:32:40Z")

</div>

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