Skip to content

Commit

Permalink
✏️ Fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ShixiangWang committed Sep 3, 2019
1 parent e19e122 commit bb7ae1d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
13 changes: 7 additions & 6 deletions R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ yq_config = function() {
}
}

message("Checking if the login name exist...")
r = yq(paste0("GET /users/", config$login))
if (status_code(r) != 200) stop("Bad login or internet, please check!", call. = FALSE)

if (config$token != "") {
message("Found token, checking it...")
r = yq(
Expand All @@ -54,6 +50,12 @@ yq_config = function() {
}
}

} else {
config$token = NULL
config = compact(config)
message("Checking if the login name exist...")
r = yq(paste0("GET /users/", config$login))
if (status_code(r) != 200) stop("Bad login or internet, please check!", call. = FALSE)
}

message("Saving info to ", .yuque_config_path)
Expand All @@ -71,6 +73,7 @@ update_config = function(x, path) {
for (i in names(x)) {
if (i %in% up_names) {
message("Updating ", i)
} else {
message("Adding ", i)
}
up_config[[i]] = x[[i]]
Expand All @@ -79,5 +82,3 @@ update_config = function(x, path) {
}

}

#jsonlite::read_json("test.json", simplifyVector = TRUE)
3 changes: 2 additions & 1 deletion R/list.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# yq_list_* funs
yq_list_groups = function() {
yq("GET ")
auth = get_auth()
yq("/users/:login/groups", login = auth$login, .token = auth$token)
}
13 changes: 10 additions & 3 deletions R/token.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,18 @@ yq_auth <- function(token) {
}
}

must_have_token = function() {
if(yq_token() == "") {
get_auth = function(need_token = FALSE) {
token = yq_token()
if (need_token) {
if (token == "") {
stop("This feature needs token. \nPlease run ?yq_whoami for more.",
call. = FALSE)
}
}
if (file.exists(.yuque_config_path)) {
login = jsonlite::read_json(.yuque_config_path, simplifyVector = TRUE)$login
} else {
TRUE
stop("This feature needs login name. \nPlease run ?yq_config or ?yq_whoami for more.")
}
return(list(login = login, token = token))
}

0 comments on commit bb7ae1d

Please sign in to comment.