library(shiny) library(magrittr) library(ggplot2) library(plotly) library(DT) library(httr) source("connection_module.R",encoding="utf-8") loginUI <- function(id) { ns <- NS(id) fluidPage( useShinyjs(), tags$head( tags$script(src="js.cookie.js"), tags$script('Shiny.addCustomMessageHandler("tokenHandlerAfterLogin", function(token) { sessionStorage.setItem(\'token\', token); Shiny.onInputChange("token", token); window.location.replace(\'/#!/profil\'); } );'), tags$style(HTML(" @import url('//fonts.googleapis.com/css?family=Lobster|Cabin:400,700'); @import url('//fonts.googleapis.com/css2?family=Fjalla+One'); "))), theme = "style.css", # App title ---- fluidRow(column(12, wellPanel( textInput("login", label = strong("Login")), passwordInput("pass", label = strong("Haslo")), uiOutput("loginErr"), actionButton('loginBtn',"Zaloguj") ))%>% tagAppendAttributes(id = 'column-login') ) %>% tagAppendAttributes(id = 'row-login'), fluidRow( column(12, tags$span("© Copyright Wszystkie prawa zastrzeżone."))%>% tagAppendAttributes(id = 'column-copyright'), )%>% tagAppendAttributes(id = 'row-footer') ) } loginServer <- function(input, output,session) { isCorrect <- eventReactive(input$loginBtn, { tmp<-data.frame(login=input$login,pass=input$pass) to_send = list(login = tmp$login, password = tmp$pass) r<-httr::POST("http://localhost:8080/api/login",body=to_send,encode = 'json') if(r$status_code==200){ response<-(content(r)) session$sendCustomMessage(type='tokenHandlerAfterLogin', response$token) TRUE } else{ FALSE } }) output$loginErr<-renderUI({ if (isCorrect()==TRUE){ p("OK",style="color:white;text-align:center;") }else{ p("Uzytkownik nie istnieje lub wprowadzono bledne dane",style="color:yellow;text-align:center;") } }) observe({ if(((session$clientData)$url_hash=="#!/login") & (!is.null(input$token) & length(input$token)>0 )){ shinyjs::runjs('window.location.replace(\'/#!/home\');') } }) }