logout <- function(session,access_token,authorization,base_url,id_token) {
    if (!is.null(session$userData$access_token)) {
      
      revoke_url <- paste0( base_url, "/revoke")
       
      res_token <- POST(
        url = revoke_url,
        add_headers(
          `accept` = "application/json",
          `authorization` = authorization,
          `cache-control` = "no-cache"
        ),
        body = list(
          token = access_token,
          token_type_hint = "access_token"
        ),
        encode = "form"
      )
      
      if (status_code(res_token) == 200) {
        print("Token revoked successfully.")
      } else {
        
        print(res_token)
      }
      
      id_token <- id_token
      logout_url <- paste0( base_url, "/logout?id_token_hint=", id_token)
      
      response <- GET(logout_url)
      
      if (status_code(response) == 200) {
        print("Session ended successfully.")
      } else {
        data <- content(response, "parsed")
      }
      session$userData$authorize <- 0
      runjs(sprintf("window.location.href = '%s';
                    deleteAllCookies();", uri_url))
    }
}

login <- function(session, url_search,random_bytes,authorize_url,uri_url,response_type,random_state,client_id,client_secret,output,token_url,userinfo_url) {
    # Vérifier si aucun paramètre n'est présent dans l'URL
    if (!grepl("^[?&].*", url_search)) {
      random_state <- toupper(paste0(sprintf("%02x", as.integer(random_bytes)), collapse = ""))
      scope <- "openid profile"
      auth_url <- sprintf("%s?client_id=%s&scope=%s&redirect_uri=%s&response_type=%s&state=%s", 
                          authorize_url,client_id, scope, URLencode(uri_url), response_type,random_state)
      jscommand <- sprintf("window.location.href ='%s';",auth_url)
      runjs(jscommand)
    } else if (grepl("\\bcode\\b", url_search)) {
      
      
      authorization =  base64enc::base64encode(charToRaw(paste0(client_id,":",client_secret)))
      authorization = paste("Basic",authorization)
      session$userData$authorization <- authorization
      code=param_get(url_search,'code')[[1]]
      body = list(
        'grant_type' = 'authorization_code',
        'redirect_uri' = uri_url,
        'code' = code,
        'state' = random_state,
        "max_age" = 0
      )
      headers = c(
        'Accept' = 'application/json',
        'Content-Type' = 'application/x-www-form-urlencoded',
        'Authorization' = authorization
      )
      res <- VERB("POST", url = token_url, body = body, add_headers(headers), encode = 'form')
      if (res$status_code == 200){
        res_content <- content(res,"parsed",encoding = "UTF-8")
        access_token <- res_content$access_token
        headers2 = c("Authorization" = paste("Bearer",access_token))
        res2 <- VERB("GET",url=userinfo_url,add_headers(headers2))
        session$userData$id_token <- res_content$id_token
        name = content(res2, "parsed", encoding = "UTF-8")$name
        output$welcome <- renderText({paste("Bonjour",name)})
        session$userData$authorize = 1
        session$userData$access_token <- access_token
      } else {
        print('Vous n avez pas l autorisation')
      }
      
    }
}

notConnected <- function(authorize) {
    if (authorize == 0) {
      runjs("shinyjs.hideAllElements();")
    }
}