HTTP Access Authentication
HTTP access supports authenticating CloudBase user login credentials. After enabling authentication, developers can add x-cloudbase-credentials: <login credentials>
to the HTTP header to invoke cloud functions.
Enabling HTTP Access Authentication
In the HTTP Access Service management page of the CloudBase console, you can enable or disable authentication for each path in the left-side path list.
After enabling access authentication, requests without authentication information or with invalid authentication information will fail.
Using the SDK to Obtain the HTTP Authentication Header
Taking the Web SDK as an example, after logging in with a CloudBase-supported login method, you can use Auth.getAuthHeader()
to obtain the authentication header:
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx",
});
/**
Execute the login process, omitted here...
*/
const authHeader = cloudbase.auth().getAuthHeader();
// { 'x-cloudbase-credentials': '<credentials>' }
Including the Authentication Header in Requests
We use axios to make an HTTP request to the HTTP service URL, including the authentication header:
const axios = require("axios");
const cloudbase = require("@cloudbase/js-sdk");
const app = cloudbase.init({
env: "xxxx",
});
/**
Execute the login process, omitted here...
*/
const authHeader = cloudbase.auth().getAuthHeader();
axios({
method: "post",
url: "https://env-id.service.tcloudbase.com/xxxx",
data: {
/* ... */
},
headers: {
...authHeader,
},
}).then((res) => {
//...
});
HTTP Request Sample:
POST /aaa/bbb HTTP/1.1
Host: env-id.service.tcloudbase.com
X-Cloudbase-Credentials: <credentials>
<Other headers>: <...>
<Payload...>