跳到主要内容

Authenticate a user

This example demonstrates how to authenticate a user in ONLYOFFICE DocSpace using the API and check the authentication with a token received.

Before you start

Replace https://yourportal.onlyoffice.com with your actual DocSpace portal URL, replace USER_CREDENTIALS with your credentials.

Full example
// Set API base URL
const BASE_URL = 'https://yourportal.onlyoffice.com';

// User credentials for authentication
const USER_CREDENTIALS = {
userName: 'your-user-name',
password: 'your-password',
};

// Step 1: Authenticate with your login and password
function authenticate() {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(USER_CREDENTIALS),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Authentication failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
if (!data) return null;
const authToken = data?.response?.token;
return authToken || null;
})
.catch((err) => {
console.log(`Authentication error: ${err}`);
return null;
});
}

// Step 2: Check authentication with a token you received
function checkAuthentication(token) {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'GET',
headers: { Authorization: token },
})
.then((res) => {
if (res.status === 200) {
console.log(`User is authenticated with token ${token}.`);
} else {
const text = await res.text();
console.log(`Authentication check failed. Status code: ${res.status}, Message: ${text}`);
}
})
.catch((err) => {
console.log(`Authentication check error: ${err}`);
});
}

// Run
authenticate().then((token) => {
if (token) {
checkAuthentication(token);
}
});

Step 1: Authenticate a user

A POST request is sent to /api/2.0/authentication to authenticate with USER_CREDENTIALS.

function authenticate() {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(USER_CREDENTIALS),
})
.then((res) => {
if (res.status === 200) return res.json();
const text = await res.text();
console.log(`Authentication failed. Status code: ${res.status}, Message: ${text}`);
return null;
})
.then((data) => {
if (!data) return null;
const authToken = data?.response?.token;
return authToken || null;
})
.catch((err) => {
console.log(`Authentication error: ${err}`);
return null;
});
}

Step 2: Check authentication

A GET request is sent to /api/2.0/authentication to check authentication success.

function checkAuthentication(token) {
return fetch(`${BASE_URL}/api/2.0/authentication`, {
method: 'GET',
headers: { Authorization: token },
})
.then((res) => {
if (res.status === 200) {
console.log(`User is authenticated with token ${token}.`);
} else {
const text = await res.text();
console.log(`Authentication check failed. Status code: ${res.status}, Message: ${text}`);
}
})
.catch((err) => {
console.log(`Authentication check error: ${err}`);
});
}