better account validation, fix #118

This commit is contained in:
Nicolas Constant 2019-06-23 19:37:49 -04:00
parent 8ad9ecb95b
commit 06982bd891
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
1 changed files with 31 additions and 6 deletions

View File

@ -4,10 +4,11 @@ import { ActivatedRoute, Router } from "@angular/router";
import { HttpErrorResponse } from "@angular/common/http";
import { AuthService, CurrentAuthProcess } from "../../services/auth.service";
import { TokenData } from "../../services/models/mastodon.interfaces";
import { TokenData, Account } from "../../services/models/mastodon.interfaces";
import { RegisteredAppsStateModel, AppInfo } from "../../states/registered-apps.state";
import { AccountInfo, AddAccount } from "../../states/accounts.state";
import { AccountInfo, AddAccount, AccountsStateModel } from "../../states/accounts.state";
import { NotificationService } from "../../services/notification.service";
import { MastodonService } from '../../services/mastodon.service';
@Component({
selector: "app-register-new-account",
@ -23,6 +24,7 @@ export class RegisterNewAccountComponent implements OnInit {
private authStorageKey: string = 'tempAuth';
constructor(
private readonly mastodonService: MastodonService,
private readonly notificationService: NotificationService,
private readonly authService: AuthService,
private readonly store: Store,
@ -45,13 +47,26 @@ export class RegisterNewAccountComponent implements OnInit {
}
const appInfo = this.getAllSavedApps().filter(x => x.instance === appDataWrapper.instance)[0];
let usedTokenData: TokenData;
this.authService.getToken(appDataWrapper.instance, appInfo.app.client_id, appInfo.app.client_secret, code, appInfo.app.redirect_uri)
.then((tokenData: TokenData) => {
usedTokenData = tokenData;
return this.mastodonService.retrieveAccountDetails({ 'instance': appDataWrapper.instance, 'id': '', 'username': '', 'order': 0, 'isSelected': true, 'token': tokenData });
})
.then((account: Account) => {
var username = account.username.toLowerCase();
var instance = appDataWrapper.instance.toLowerCase();
if(this.isAccountAlreadyPresent(username, instance)){
this.notificationService.notify(`Account @${username}@${instance} is already registered`, true);
this.router.navigate(['/home']);
return;
}
const accountInfo = new AccountInfo();
accountInfo.username = appDataWrapper.username.toLowerCase();
accountInfo.instance = appDataWrapper.instance.toLowerCase();
accountInfo.token = tokenData;
accountInfo.username = username;
accountInfo.instance = instance;
accountInfo.token = usedTokenData;
this.store.dispatch([new AddAccount(accountInfo)])
.subscribe(() => {
@ -68,6 +83,16 @@ export class RegisterNewAccountComponent implements OnInit {
ngOnInit() {
}
private isAccountAlreadyPresent(username: string, instance: string): boolean{
const accounts = <AccountInfo[]>this.store.snapshot().registeredaccounts.accounts;
for (let acc of accounts) {
if(acc.instance === instance && acc.username == username){
return true;
}
}
return false;
}
private displayError(type: RegistrationErrorTypes) {
this.hasError = true;
switch (type) {