R/login_user.R

#' Login user
#'
#' @inheritParams add_user
#'
#' @return JSON web token.
#' @export
#'
#' @examples
login_user <- function(email, password) {

  # check if user exists
  con   <- mongolite::mongo('users', get_env('USERS_DB'))
  saved <- con$find(
    sprintf('{"email" : "%s"}', email)
  )

  if (!nrow(saved)) stop("Invalid credentials.")

  # check if password is correct
  valid <- sodium::password_verify(saved$password, password)

  if (!valid) stop("Invalid credentials.")

  jwt <- create_jwt(get_env('JWT_SECRET'), email = email, hashid = saved$hashid)
  return(jwt)
}
alexvpickering/authr documentation built on May 19, 2019, 10:48 p.m.