TakeCareApp/app/login_module.R

109 lines
2.5 KiB
R
Raw Normal View History

2020-12-12 12:38:54 +01:00
library(shiny)
library(magrittr)
library(ggplot2)
library(plotly)
library(DT)
2020-12-19 23:13:44 +01:00
library(httr)
2020-12-12 12:38:54 +01:00
2020-12-14 13:47:03 +01:00
source("connection_module.R",encoding="utf-8")
2020-12-12 12:38:54 +01:00
loginUI <- function(id) {
ns <- NS(id)
fluidPage(
2020-12-14 13:47:03 +01:00
useShinyjs(),
2020-12-12 12:38:54 +01:00
tags$head(
2020-12-14 13:47:03 +01:00
tags$script(src="js.cookie.js"),
2020-12-19 23:13:44 +01:00
tags$script('Shiny.addCustomMessageHandler("tokenHandlerAfterLogin",
function(token) {
var cookie = Cookies.set(\'token\', token, { expires: 7 });
Shiny.onInputChange("token", cookie);
window.location.replace(\'/#!/profil\');
}
);'),
2020-12-12 12:38:54 +01:00
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")),
2020-12-14 13:47:03 +01:00
passwordInput("pass", label = strong("Haslo")),
uiOutput("loginErr"),
2020-12-12 12:38:54 +01:00
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')
)
}
2020-12-14 13:47:03 +01:00
loginServer <- function(input, output,session) {
isCorrect <- eventReactive(input$loginBtn, {
tmp<-data.frame(login=input$login,pass=input$pass)
2020-12-19 23:13:44 +01:00
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){
2020-12-14 13:47:03 +01:00
2020-12-19 23:13:44 +01:00
response<-(content(r))
session$sendCustomMessage(type='tokenHandlerAfterLogin', response$token)
2020-12-14 13:47:03 +01:00
2020-12-19 23:13:44 +01:00
2020-12-14 13:47:03 +01:00
2020-12-19 23:13:44 +01:00
TRUE
}
else{
2020-12-14 13:47:03 +01:00
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;")
}
})
2020-12-19 23:13:44 +01:00
2020-12-14 13:47:03 +01:00
observe({
if(((session$clientData)$url_hash=="#!/login") & (!is.null(input$token) & length(input$token)>0 )){
print("dziala")
shinyjs::runjs('window.location.replace(\'/#!/home\');')
}
})
2020-12-12 12:38:54 +01:00
}