Adds an authentication header to an HTTP request intended to call a NocoDB API endpoint. The following credential sources are consulted in descending order and the first one applicable is used:
If
api_token
is provided, it is directly added to the request asxc-token
header.If
email
is provided, the corresponding user has signed in before in the current R session and the generated access token is not yet expired, it is re-used.If
email
is provided, the corresponding user has signed in before in a past R session and hence there's a cached refresh token available which is not yet expired, it's used to generate a fresh access token.If
email
andpassword
are provided, they are used to newlysign_in()
the corresponding user.
Access tokens (sources 2–4 above) are added to the request as xc-auth
header. Other than the api_token
, they expire after a certain amount of time,
configured by the NocoDB server via NC_JWT_EXPIRES_IN
(defaults to 10 hours).
Usage
req_auth(
req,
email = pal::pkg_config_val("email"),
password = pal::pkg_config_val("password"),
api_token = pal::pkg_config_val("api_token")
)
Value
A modified HTTP request.
Invalid token errors
If you did provide an
api_token
and encounter anInvalid token
error, it simply means theapi_token
is invalid (e.g. because it was revoked).If you did not provide an
api_token
and encounter anInvalid token
error, it means the access token generated by the last call tosign_in()
in the current R session has expired or was revoked meanwhile. This should only happen in rare edge cases since expired access tokens aren't re-used byreq_auth()
.If you encounter an
Invalid refresh token
error, it means the refresh token cached to disk by the last call tosign_in()
in a past R session has expired meanwhile. This should only happen in rare edge cases since expired refresh tokens aren't re-used byreq_auth()
.
The latter two errors should resolve by manually invoking sign_in()
.
If you would like to avoid token expiration errors altogether, consider using an API token
(api_token
) instead of email
and password
.
See also
Other common functions:
api()
,
req_basic()
Other functions to manage NocoDB authentication:
api_tokens()
,
assert_super_admin()
,
create_api_token()
,
delete_api_token()
,
is_signed_in()
,
is_super_admin()
,
refresh_sign_in()
,
sign_in()
,
sign_out()
,
sign_up_user()