diff --git a/src/app/pages/register-new-account/register-new-account.component.ts b/src/app/pages/register-new-account/register-new-account.component.ts index 20579ee3..23298033 100644 --- a/src/app/pages/register-new-account/register-new-account.component.ts +++ b/src/app/pages/register-new-account/register-new-account.component.ts @@ -59,9 +59,6 @@ export class RegisterNewAccountComponent implements OnInit { console.error('registeredApps$') console.warn(x); }); - - - } onSubmit(): boolean { @@ -69,27 +66,38 @@ export class RegisterNewAccountComponent implements OnInit { const username = fullHandle[0]; const instance = fullHandle[1]; - console.log(`username ${username} instance ${instance}`); + + const redirect_uri = this.getLocalHostname() + '/register'; - let localUrl = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : ''); - - if (localUrl === 'file://') { - localUrl = 'http://localhost:4200'; - } - const redirect_uri = localUrl + '/register'; - - this.authService.createNewApplication(instance, redirect_uri) + this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi') .then((appData: AppData) => { - - const appDataTemp = new AppDataWrapper(username, instance, appData); - localStorage.setItem('tempAuth', JSON.stringify(appDataTemp)); - - let instanceUrl = `https://${instance}/oauth/authorize?scope=${encodeURIComponent('read write follow')}&response_type=code&redirect_uri=${encodeURIComponent(redirect_uri)}&client_id=${appData.client_id}`; - - window.location.href = instanceUrl; + this.processAndRedirectToAuthPage(username, instance, appData); + }) + .catch(err => { + console.error(err); }); return false; } + + private getLocalHostname(): string { + let localHostname = location.protocol + '//' + location.hostname + (location.port ? ':' + location.port : ''); + + //Electron hack + if (localHostname === 'file://') { + localHostname = 'http://localhost:4200'; + } + + return localHostname; + } + + private processAndRedirectToAuthPage(username: string, instance: string, app: AppData){ + const appDataTemp = new AppDataWrapper(username, instance, app); + localStorage.setItem('tempAuth', JSON.stringify(appDataTemp)); + + let instanceUrl = `https://${instance}/oauth/authorize?scope=${encodeURIComponent('read write follow')}&response_type=code&redirect_uri=${encodeURIComponent(app.redirect_uri)}&client_id=${app.client_id}`; + + window.location.href = instanceUrl; + } } class AppDataWrapper { diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index db4437e2..e776e5b0 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -17,15 +17,15 @@ export class AuthService { return this.httpClient.post(url, null).toPromise(); } - createNewApplication(instance: string, redirectUrl: string): Promise { - const url = 'https://' + instance + this.apiRoutes.createApp; + createNewApplication(instance: string, appName: string, redirectUrl: string, scopes: string, website: string): Promise { + const url = 'https://' + instance + this.apiRoutes.createApp; const formData = new FormData(); - formData.append('client_name', 'Sengi'); + formData.append('client_name', appName); formData.append('redirect_uris', redirectUrl); - formData.append('scopes', 'read write follow'); - formData.append('website', 'https://github.com/NicolasConstant/sengi'); + formData.append('scopes', scopes); + formData.append('website', website); return this.httpClient.post(url, formData).toPromise(); -} + } }