refactorization app creation calling order

This commit is contained in:
Nicolas Constant 2018-09-09 18:54:06 -04:00
parent 6784f43cfc
commit 4f117e87cd
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 89 additions and 94 deletions

View File

@ -34,7 +34,7 @@ export class RegisterNewAccountComponent implements OnInit {
if (!code) return; if (!code) return;
const appDataWrapper = <CurrentAuthProcess>JSON.parse(localStorage.getItem(this.authStorageKey)); const appDataWrapper = <CurrentAuthProcess>JSON.parse(localStorage.getItem(this.authStorageKey));
if(!appDataWrapper) return; if (!appDataWrapper) return;
const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0]; const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0];
console.warn('appInfo'); console.warn('appInfo');
@ -45,7 +45,7 @@ export class RegisterNewAccountComponent implements OnInit {
const accountInfo = new AccountInfo(); const accountInfo = new AccountInfo();
accountInfo.username = appDataWrapper.username; accountInfo.username = appDataWrapper.username;
accountInfo.instance = appDataWrapper.instance; accountInfo.instance = appDataWrapper.instance;
accountInfo.token= tokenData; accountInfo.token = tokenData;
this.store.dispatch([new AddAccount(accountInfo)]) this.store.dispatch([new AddAccount(accountInfo)])
.subscribe(() => { .subscribe(() => {
@ -70,39 +70,34 @@ export class RegisterNewAccountComponent implements OnInit {
const username = fullHandle[0]; const username = fullHandle[0];
const instance = fullHandle[1]; const instance = fullHandle[1];
this.checkAndCreateApplication(instance)
.then((appData: AppData) => {
this.redirectToInstanceAuthPage(username, instance, appData);
});
return false;
}
private checkAndCreateApplication(instance: string): Promise<AppData> {
const alreadyRegisteredApps = this.getAllSavedApps(); const alreadyRegisteredApps = this.getAllSavedApps();
const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance); const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance);
if (instanceApps.length !== 0) { if (instanceApps.length !== 0) {
console.log('instance already registered'); console.log('instance already registered');
const appData = instanceApps[0].app; return Promise.resolve(instanceApps[0].app);
this.redirectToInstanceAuthPage(username, instance, appData);
} else { } else {
console.log('instance not registered'); console.log('instance not registered');
const redirect_uri = this.getLocalHostname() + '/register'; const redirect_uri = this.getLocalHostname() + '/register';
this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi') return this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi')
.then((appData: AppData) => { .then((appData: AppData) => {
this.saveNewApp(instance, appData) return this.saveNewApp(instance, appData)
.then(() => { .then(() => { return appData; });
this.redirectToInstanceAuthPage(username, instance, appData);
});
}) })
.catch(err => {
console.error(err);
});
} }
return false;
} }
private getLocalHostname(): string { private getLocalHostname(): string {
let localHostname = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : ''); let localHostname = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : '');
//Electron hack
if (localHostname === 'file://') {
localHostname = 'http://localhost:4200';
}
return localHostname; return localHostname;
} }