This commit is contained in:
Nicolas Constant 2018-09-08 12:00:16 -04:00
parent be6211a04b
commit 6c96ec694c
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 33 additions and 25 deletions

View File

@ -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 {

View File

@ -17,15 +17,15 @@ export class AuthService {
return this.httpClient.post<TokenData>(url, null).toPromise();
}
createNewApplication(instance: string, redirectUrl: string): Promise<AppData> {
const url = 'https://' + instance + this.apiRoutes.createApp;
createNewApplication(instance: string, appName: string, redirectUrl: string, scopes: string, website: string): Promise<AppData> {
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<AppData>(url, formData).toPromise();
}
}
}