From 89ee5fadf95058c7f98abb496901d064986b38e8 Mon Sep 17 00:00:00 2001 From: Nicolas Constant Date: Sat, 8 Sep 2018 12:44:25 -0400 Subject: [PATCH] added the creation/retrieval of apps in state management store --- package.json | 2 +- .../register-new-account.component.ts | 87 ++++++++++++------- src/app/states/registered-apps.state.ts | 14 ++- 3 files changed, 64 insertions(+), 39 deletions(-) diff --git a/package.json b/package.json index 241e7374..24a110f4 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "lint": "ng lint", "e2e": "ng e2e", "electron": "ng build && electron .", - "electron-aot": "ng build --prod && electron ." + "electron-prod": "ng build --prod && electron ." }, "private": true, "dependencies": { 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 23298033..08ec9f17 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 @@ -6,9 +6,7 @@ import { Observable } from "rxjs"; import { AuthService } from "../../services/auth.service"; import { TokenData, AppData } from "../../services/models/mastodon.interfaces"; import { AccountsService } from "../../services/accounts.service"; -import { AddRegisteredApp, RegisteredAppsState, RegisteredAppsStateModel } from "../../states/registered-apps.state"; - - +import { AddRegisteredApp, RegisteredAppsState, RegisteredAppsStateModel, AppInfo } from "../../states/registered-apps.state"; @Component({ selector: "app-register-new-account", @@ -18,7 +16,9 @@ import { AddRegisteredApp, RegisteredAppsState, RegisteredAppsStateModel } from export class RegisterNewAccountComponent implements OnInit { @Input() mastodonFullHandle: string; result: string; - registeredApps$: Observable; + // registeredApps$: Observable; + + private authStorageKey: string = 'tempAuth'; constructor( private readonly authService: AuthService, @@ -26,24 +26,25 @@ export class RegisterNewAccountComponent implements OnInit { private readonly store: Store, private readonly activatedRoute: ActivatedRoute) { - this.registeredApps$ = this.store.select(state => state.registeredapps.registeredApps); + // this.registeredApps$ = this.store.select(state => state.registeredapps.registeredApps); this.activatedRoute.queryParams.subscribe(params => { const code = params['code']; if (!code) return; - console.warn(`got a code! ${code}`); - const appDataWrapper = JSON.parse(localStorage.getItem('tempAuth')); + const appDataWrapper = JSON.parse(localStorage.getItem(this.authStorageKey)); + if(!appDataWrapper) return; - console.error('got appDataWrapper from local storage'); - console.error(appDataWrapper); + const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0]; + console.warn('appInfo'); + console.warn(appInfo); - this.authService.getToken(appDataWrapper.instance, appDataWrapper.appData.client_id, appDataWrapper.appData.client_secret, code, appDataWrapper.appData.redirect_uri) + this.authService.getToken(appDataWrapper.instance, appInfo.app.client_id, appInfo.app.client_secret, code, appInfo.app.redirect_uri) .then(tokenData => { console.warn('Got token data!'); console.warn(tokenData); - localStorage.removeItem('tempAuth'); + localStorage.removeItem(this.authStorageKey); //TODO review all this // this.accountsService.addNewAccount(appDataWrapper.instance, appDataWrapper.username, tokenData); @@ -55,10 +56,10 @@ export class RegisterNewAccountComponent implements OnInit { } ngOnInit() { - this.registeredApps$.subscribe(x => { - console.error('registeredApps$') - console.warn(x); - }); + // this.registeredApps$.subscribe(x => { + // console.error('registeredApps$') + // console.warn(x); + // }); } onSubmit(): boolean { @@ -66,16 +67,29 @@ export class RegisterNewAccountComponent implements OnInit { const username = fullHandle[0]; const instance = fullHandle[1]; - - const redirect_uri = this.getLocalHostname() + '/register'; - this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi') - .then((appData: AppData) => { - this.processAndRedirectToAuthPage(username, instance, appData); - }) - .catch(err => { - console.error(err); - }); + const alreadyRegisteredApps = this.getAllSavedApps(); + const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance); + + if (instanceApps.length !== 0) { + console.log('instance already registered'); + const appData = instanceApps[0].app; + this.redirectToInstanceAuthPage(username, instance, appData); + + } else { + console.log('instance not registered'); + const redirect_uri = this.getLocalHostname() + '/register'; + this.authService.createNewApplication(instance, 'Sengi', redirect_uri, 'read write follow', 'https://github.com/NicolasConstant/sengi') + .then((appData: AppData) => { + this.saveNewApp(instance, appData) + .then(() => { + this.redirectToInstanceAuthPage(username, instance, appData); + }); + }) + .catch(err => { + console.error(err); + }); + } return false; } @@ -90,8 +104,23 @@ export class RegisterNewAccountComponent implements OnInit { return localHostname; } - private processAndRedirectToAuthPage(username: string, instance: string, app: AppData){ - const appDataTemp = new AppDataWrapper(username, instance, app); + private getAllSavedApps(): AppInfo[] { + const snapshot = this.store.snapshot().registeredapps; + return snapshot.apps; + } + + private saveNewApp(instance: string, app: AppData): Promise { + const appInfo = new AppInfo(); + appInfo.instance = instance; + appInfo.app = app; + + return this.store.dispatch([ + new AddRegisteredApp(appInfo) + ]).toPromise(); + } + + private redirectToInstanceAuthPage(username: string, instance: string, app: AppData) { + const appDataTemp = new CurrentAuthProcess(username, instance); 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}`; @@ -100,8 +129,6 @@ export class RegisterNewAccountComponent implements OnInit { } } -class AppDataWrapper { - constructor(public username: string, public instance: string, public appData: AppData) { - - } +class CurrentAuthProcess { + constructor(public username: string, public instance: string) { } } \ No newline at end of file diff --git a/src/app/states/registered-apps.state.ts b/src/app/states/registered-apps.state.ts index e2095f11..c33b6137 100644 --- a/src/app/states/registered-apps.state.ts +++ b/src/app/states/registered-apps.state.ts @@ -1,4 +1,5 @@ import { State, Action, StateContext } from '@ngxs/store'; +import { AppData } from '../services/models/mastodon.interfaces'; ​ export class AddRegisteredApp { static readonly type = '[RegisteredApps] Add app'; @@ -6,13 +7,13 @@ export class AddRegisteredApp { } export interface RegisteredAppsStateModel { - registeredApps: AppInfo[]; + apps: AppInfo[]; } @State({ name: 'registeredapps', defaults: { - registeredApps: [] + apps: [] } }) export class RegisteredAppsState { @@ -20,7 +21,7 @@ export class RegisteredAppsState { AddRegisteredApp(ctx: StateContext, action: AddRegisteredApp) { const state = ctx.getState(); ctx.patchState({ - registeredApps: [...state.registeredApps, action.app] + apps: [...state.apps, action.app] }); // ctx.setState({ @@ -30,9 +31,6 @@ export class RegisteredAppsState { } export class AppInfo { - id: number; - name: string; - redirect_uri: string; - client_id: string; - client_secret: string; + instance: string; + app: AppData; } \ No newline at end of file