TakeCareApp/app/login_module.R

115 lines
2.5 KiB
R
Raw Permalink 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-20 15:46:02 +01:00
tags$script('Shiny.addCustomMessageHandler("tokenHandlerAfterLogin",
2020-12-19 23:13:44 +01:00
function(token) {
2020-12-20 15:46:02 +01:00
sessionStorage.setItem(\'token\', token);
Shiny.onInputChange("token", token);
Shiny.onInputChange("auth", 1);
2020-12-19 23:13:44 +01:00
window.location.href=\'/#!/profil\';
2020-12-19 23:13:44 +01:00
}
);'),
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",
# window.location.replace(\'/#!/profil\');
2020-12-12 12:38:54 +01:00
# 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))
2020-12-14 13:47:03 +01:00
2020-12-20 15:46:02 +01:00
session$sendCustomMessage(type='tokenHandlerAfterLogin', response$token)
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){
2021-01-22 13:42:15 +01:00
2020-12-14 13:47:03 +01:00
}else{
2021-01-19 23:04:09 +01:00
p("Użytkownik istnieje lub wprowadzono błędne dane",style="color:yellow;text-align:center;")
2020-12-14 13:47:03 +01:00
}
})
2020-12-19 23:13:44 +01:00
2020-12-14 13:47:03 +01:00
observe({
if((session$clientData)$url_hash=="#!/login"){
if(!is.null(input$auth) & length(input$auth)>0 ){
print("redirect from login page if token is set")
shinyjs::runjs('window.location.replace(\'/#!/home\');')
}
2020-12-14 13:47:03 +01:00
2020-12-20 15:46:02 +01:00
2020-12-28 16:04:02 +01:00
2020-12-14 13:47:03 +01:00
}
})
2020-12-12 12:38:54 +01:00
}