added the creation/retrieval of apps in state management store
This commit is contained in:
parent
6c96ec694c
commit
89ee5fadf9
|
@ -12,7 +12,7 @@
|
||||||
"lint": "ng lint",
|
"lint": "ng lint",
|
||||||
"e2e": "ng e2e",
|
"e2e": "ng e2e",
|
||||||
"electron": "ng build && electron .",
|
"electron": "ng build && electron .",
|
||||||
"electron-aot": "ng build --prod && electron ."
|
"electron-prod": "ng build --prod && electron ."
|
||||||
},
|
},
|
||||||
"private": true,
|
"private": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|
|
@ -6,9 +6,7 @@ import { Observable } from "rxjs";
|
||||||
import { AuthService } from "../../services/auth.service";
|
import { AuthService } from "../../services/auth.service";
|
||||||
import { TokenData, AppData } from "../../services/models/mastodon.interfaces";
|
import { TokenData, AppData } from "../../services/models/mastodon.interfaces";
|
||||||
import { AccountsService } from "../../services/accounts.service";
|
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({
|
@Component({
|
||||||
selector: "app-register-new-account",
|
selector: "app-register-new-account",
|
||||||
|
@ -18,7 +16,9 @@ import { AddRegisteredApp, RegisteredAppsState, RegisteredAppsStateModel } from
|
||||||
export class RegisterNewAccountComponent implements OnInit {
|
export class RegisterNewAccountComponent implements OnInit {
|
||||||
@Input() mastodonFullHandle: string;
|
@Input() mastodonFullHandle: string;
|
||||||
result: string;
|
result: string;
|
||||||
registeredApps$: Observable<RegisteredAppsStateModel>;
|
// registeredApps$: Observable<RegisteredAppsStateModel>;
|
||||||
|
|
||||||
|
private authStorageKey: string = 'tempAuth';
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly authService: AuthService,
|
private readonly authService: AuthService,
|
||||||
|
@ -26,24 +26,25 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
private readonly store: Store,
|
private readonly store: Store,
|
||||||
private readonly activatedRoute: ActivatedRoute) {
|
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 => {
|
this.activatedRoute.queryParams.subscribe(params => {
|
||||||
const code = params['code'];
|
const code = params['code'];
|
||||||
if (!code) return;
|
if (!code) return;
|
||||||
|
|
||||||
console.warn(`got a code! ${code}`);
|
const appDataWrapper = <CurrentAuthProcess>JSON.parse(localStorage.getItem(this.authStorageKey));
|
||||||
const appDataWrapper = <AppDataWrapper>JSON.parse(localStorage.getItem('tempAuth'));
|
if(!appDataWrapper) return;
|
||||||
|
|
||||||
console.error('got appDataWrapper from local storage');
|
const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0];
|
||||||
console.error(appDataWrapper);
|
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 => {
|
.then(tokenData => {
|
||||||
console.warn('Got token data!');
|
console.warn('Got token data!');
|
||||||
console.warn(tokenData);
|
console.warn(tokenData);
|
||||||
|
|
||||||
localStorage.removeItem('tempAuth');
|
localStorage.removeItem(this.authStorageKey);
|
||||||
|
|
||||||
//TODO review all this
|
//TODO review all this
|
||||||
// this.accountsService.addNewAccount(appDataWrapper.instance, appDataWrapper.username, tokenData);
|
// this.accountsService.addNewAccount(appDataWrapper.instance, appDataWrapper.username, tokenData);
|
||||||
|
@ -55,10 +56,10 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this.registeredApps$.subscribe(x => {
|
// this.registeredApps$.subscribe(x => {
|
||||||
console.error('registeredApps$')
|
// console.error('registeredApps$')
|
||||||
console.warn(x);
|
// console.warn(x);
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
onSubmit(): boolean {
|
onSubmit(): boolean {
|
||||||
|
@ -66,16 +67,29 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
|
|
||||||
const username = fullHandle[0];
|
const username = fullHandle[0];
|
||||||
const instance = fullHandle[1];
|
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')
|
const alreadyRegisteredApps = this.getAllSavedApps();
|
||||||
.then((appData: AppData) => {
|
const instanceApps = alreadyRegisteredApps.filter(x => x.instance === instance);
|
||||||
this.processAndRedirectToAuthPage(username, instance, appData);
|
|
||||||
})
|
if (instanceApps.length !== 0) {
|
||||||
.catch(err => {
|
console.log('instance already registered');
|
||||||
console.error(err);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,8 +104,23 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
return localHostname;
|
return localHostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
private processAndRedirectToAuthPage(username: string, instance: string, app: AppData){
|
private getAllSavedApps(): AppInfo[] {
|
||||||
const appDataTemp = new AppDataWrapper(username, instance, app);
|
const snapshot = <RegisteredAppsStateModel>this.store.snapshot().registeredapps;
|
||||||
|
return snapshot.apps;
|
||||||
|
}
|
||||||
|
|
||||||
|
private saveNewApp(instance: string, app: AppData): Promise<any> {
|
||||||
|
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));
|
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}`;
|
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 {
|
class CurrentAuthProcess {
|
||||||
constructor(public username: string, public instance: string, public appData: AppData) {
|
constructor(public username: string, public instance: string) { }
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import { State, Action, StateContext } from '@ngxs/store';
|
import { State, Action, StateContext } from '@ngxs/store';
|
||||||
|
import { AppData } from '../services/models/mastodon.interfaces';
|
||||||
|
|
||||||
export class AddRegisteredApp {
|
export class AddRegisteredApp {
|
||||||
static readonly type = '[RegisteredApps] Add app';
|
static readonly type = '[RegisteredApps] Add app';
|
||||||
|
@ -6,13 +7,13 @@ export class AddRegisteredApp {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RegisteredAppsStateModel {
|
export interface RegisteredAppsStateModel {
|
||||||
registeredApps: AppInfo[];
|
apps: AppInfo[];
|
||||||
}
|
}
|
||||||
|
|
||||||
@State<RegisteredAppsStateModel>({
|
@State<RegisteredAppsStateModel>({
|
||||||
name: 'registeredapps',
|
name: 'registeredapps',
|
||||||
defaults: {
|
defaults: {
|
||||||
registeredApps: []
|
apps: []
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
export class RegisteredAppsState {
|
export class RegisteredAppsState {
|
||||||
|
@ -20,7 +21,7 @@ export class RegisteredAppsState {
|
||||||
AddRegisteredApp(ctx: StateContext<RegisteredAppsStateModel>, action: AddRegisteredApp) {
|
AddRegisteredApp(ctx: StateContext<RegisteredAppsStateModel>, action: AddRegisteredApp) {
|
||||||
const state = ctx.getState();
|
const state = ctx.getState();
|
||||||
ctx.patchState({
|
ctx.patchState({
|
||||||
registeredApps: [...state.registeredApps, action.app]
|
apps: [...state.apps, action.app]
|
||||||
});
|
});
|
||||||
|
|
||||||
// ctx.setState({
|
// ctx.setState({
|
||||||
|
@ -30,9 +31,6 @@ export class RegisteredAppsState {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AppInfo {
|
export class AppInfo {
|
||||||
id: number;
|
instance: string;
|
||||||
name: string;
|
app: AppData;
|
||||||
redirect_uri: string;
|
|
||||||
client_id: string;
|
|
||||||
client_secret: string;
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue