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_tokenis provided, it is directly added to the request asxc-tokenheader.If
emailis 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
emailis 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
emailandpasswordare 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 = funky::config_val("email"),
password = funky::config_val("password"),
api_token = funky::config_val("api_token")
)Arguments
- req
A httr2 request object.
character(1)
E-mail address of the NocoDB user to authenticate with.- password
character(1)
Password of the NocoDB user to authenticate with.- api_token
character(1)
NocoDB API token. Takes precedence overemailandpasswordif provided.
Value
A modified HTTP request.
Invalid token errors
If you did provide an
api_tokenand encounter anInvalid tokenerror, it simply means theapi_tokenis invalid (e.g. because it was revoked).If you did not provide an
api_tokenand encounter anInvalid tokenerror, 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 tokenerror, 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()