The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/app/cloud/server/functions/connectWorkspace.js

58 lines
1.4 KiB

import { HTTP } from 'meteor/http';
import { getRedirectUri } from './getRedirectUri';
import { retrieveRegistrationStatus } from './retrieveRegistrationStatus';
import { Settings } from '../../../models';
import { settings } from '../../../settings';
import { saveRegistrationData } from './saveRegistrationData';
export function connectWorkspace(token) {
const { connectToCloud } = retrieveRegistrationStatus();
if (!connectToCloud) {
Settings.updateValueById('Register_Server', true);
}
// shouldn't get here due to checking this on the method
// but this is just to double check
if (!token) {
return new Error('Invalid token; the registration token is required.');
}
const redirectUri = getRedirectUri();
const regInfo = {
email: settings.get('Organization_Email'),
client_name: settings.get('Site_Name'),
redirect_uris: [redirectUri],
};
const cloudUrl = settings.get('Cloud_Url');
let result;
try {
result = HTTP.post(`${ cloudUrl }/api/oauth/clients`, {
headers: {
Authorization: `Bearer ${ token }`,
},
data: regInfo,
});
} catch (e) {
if (e.response && e.response.data && e.response.data.error) {
console.error(`Failed to register with Rocket.Chat Cloud. Error: ${ e.response.data.error }`);
} else {
console.error(e);
}
return false;
}
const { data } = result;
if (!data) {
return false;
}
Promise.await(saveRegistrationData(data));
return true;
}