refactor: Use functional design for the authentication of apps

This commit is contained in:
Faruk AYDIN
2022-10-03 21:14:54 +03:00
parent 0abab06124
commit 4d5be952ce
17 changed files with 523 additions and 22 deletions

View File

@@ -1,5 +1,7 @@
import Context from '../../types/express/context';
import axios from 'axios';
import prepareGlobalVariableForConnection from '../../helpers/global-variable/connection';
import App from '../../models/app';
type Params = {
input: {
@@ -19,29 +21,24 @@ const createAuthData = async (
})
.throwIfNotFound();
const appClass = (await import(`../../apps/${connection.key}`)).default;
if (!connection.formattedData) {
return null;
}
const appInstance = new appClass(connection);
const authLink = await appInstance.authenticationClient.createAuthData();
const authInstance = (await import(`../../apps/${connection.key}2/auth`))
.default;
const app = App.findOneByKey(connection.key);
const $ = prepareGlobalVariableForConnection(connection, app);
await authInstance.createAuthData($);
try {
await axios.get(authLink.url);
await axios.get(connection.formattedData.url as string);
} catch (error) {
throw new Error('Error occured while creating authorization URL!');
}
await connection.$query().patch({
formattedData: {
...connection.formattedData,
...authLink,
},
});
return authLink;
return connection.formattedData;
};
export default createAuthData;