Authentication functionnal
This commit is contained in:
parent
04a3cb92e5
commit
7e78496214
|
@ -107,6 +107,7 @@
|
||||||
<SubType>Code</SubType>
|
<SubType>Code</SubType>
|
||||||
</TypeScriptCompile>
|
</TypeScriptCompile>
|
||||||
<TypeScriptCompile Include="src\app\models\stream.models.ts" />
|
<TypeScriptCompile Include="src\app\models\stream.models.ts" />
|
||||||
|
<TypeScriptCompile Include="src\app\services\accounts.service.ts" />
|
||||||
<TypeScriptCompile Include="src\app\services\auth.service.ts" />
|
<TypeScriptCompile Include="src\app\services\auth.service.ts" />
|
||||||
<TypeScriptCompile Include="src\app\services\models\api.settings.ts" />
|
<TypeScriptCompile Include="src\app\services\models\api.settings.ts" />
|
||||||
<TypeScriptCompile Include="src\app\services\models\mastodon.interfaces.ts" />
|
<TypeScriptCompile Include="src\app\services\models\mastodon.interfaces.ts" />
|
||||||
|
|
|
@ -14,6 +14,7 @@ import { StreamsSelectionFooterComponent } from "./components/streams-selection-
|
||||||
import { TootComponent } from "./components/toot/toot.component";
|
import { TootComponent } from "./components/toot/toot.component";
|
||||||
import { RegisterNewAccountComponent } from "./pages/register-new-account/register-new-account.component";
|
import { RegisterNewAccountComponent } from "./pages/register-new-account/register-new-account.component";
|
||||||
import { AuthService } from "./services/auth.service";
|
import { AuthService } from "./services/auth.service";
|
||||||
|
import { AccountsService } from "./services/accounts.service";
|
||||||
|
|
||||||
const routes: Routes = [
|
const routes: Routes = [
|
||||||
{ path: "", redirectTo: "home", pathMatch: "full" },
|
{ path: "", redirectTo: "home", pathMatch: "full" },
|
||||||
|
@ -39,7 +40,7 @@ const routes: Routes = [
|
||||||
NgxElectronModule,
|
NgxElectronModule,
|
||||||
RouterModule.forRoot(routes)
|
RouterModule.forRoot(routes)
|
||||||
],
|
],
|
||||||
providers: [AuthService],
|
providers: [AuthService, AccountsService],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
export class AppModule { }
|
export class AppModule { }
|
||||||
|
|
|
@ -1,30 +1,51 @@
|
||||||
import { Component, OnInit } from "@angular/core";
|
import { Component, OnInit, OnDestroy } from "@angular/core";
|
||||||
|
import { Subscription, BehaviorSubject } from "rxjs";
|
||||||
|
|
||||||
import { AccountWrapper } from "../../models/account.models";
|
import { AccountWrapper } from "../../models/account.models";
|
||||||
|
import { AccountsService, LocalAccount } from "../../services/accounts.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: "app-left-side-bar",
|
selector: "app-left-side-bar",
|
||||||
templateUrl: "./left-side-bar.component.html",
|
templateUrl: "./left-side-bar.component.html",
|
||||||
styleUrls: ["./left-side-bar.component.css"]
|
styleUrls: ["./left-side-bar.component.css"]
|
||||||
})
|
})
|
||||||
export class LeftSideBarComponent implements OnInit {
|
export class LeftSideBarComponent implements OnInit, OnDestroy {
|
||||||
accounts: AccountWrapper[] = [];
|
accounts: AccountWrapper[] = [];
|
||||||
|
|
||||||
constructor() { }
|
private sub: Subscription;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly accountsService: AccountsService) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
|
this.accountsService.init.then(() => {
|
||||||
|
this.sub = this.accountsService.accountsSubject.subscribe((accounts: LocalAccount[]) => {
|
||||||
|
this.accounts.length = 0;
|
||||||
|
|
||||||
const acc1 = new AccountWrapper();
|
for (let acc of accounts) {
|
||||||
acc1.username = "@mastodon.social@Gargron";
|
const acc1 = new AccountWrapper();
|
||||||
acc1.avatar = "https://files.mastodon.social/accounts/avatars/000/000/001/original/4df197532c6b768c.png";
|
acc1.username = acc.mastodonAccount.username;
|
||||||
this.accounts.push(acc1);
|
acc1.avatar = acc.mastodonAccount.avatar;
|
||||||
|
this.accounts.push(acc1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
const acc2 = new AccountWrapper();
|
//const acc1 = new AccountWrapper();
|
||||||
acc2.username = "@mastodon.art@DearMsDearn";
|
//acc1.username = "@mastodon.social@Gargron";
|
||||||
acc2.avatar = "https://curate.mastodon.art/gallery/accounts/avatars/000/015/092/original/3a112863f2dd22a27764179912dc8984.gif";
|
//acc1.avatar = "https://files.mastodon.social/accounts/avatars/000/000/001/original/4df197532c6b768c.png";
|
||||||
this.accounts.push(acc2);
|
//this.accounts.push(acc1);
|
||||||
|
|
||||||
|
//const acc2 = new AccountWrapper();
|
||||||
|
//acc2.username = "@mastodon.art@DearMsDearn";
|
||||||
|
//acc2.avatar = "https://curate.mastodon.art/gallery/accounts/avatars/000/015/092/original/3a112863f2dd22a27764179912dc8984.gif";
|
||||||
|
//this.accounts.push(acc2);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnDestroy(): void {
|
||||||
|
this.sub.unsubscribe();
|
||||||
}
|
}
|
||||||
|
|
||||||
toogleAccount(accountId: number): boolean {
|
toogleAccount(accountId: number): boolean {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Component, OnInit, Input } from "@angular/core";
|
import { Component, OnInit, Input } from "@angular/core";
|
||||||
import { AuthService } from "../../services/auth.service";
|
import { AuthService } from "../../services/auth.service";
|
||||||
import { TokenData } from "../../services/models/mastodon.interfaces";
|
import { TokenData } from "../../services/models/mastodon.interfaces";
|
||||||
|
import { AccountsService } from "../../services/accounts.service";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: "app-register-new-account",
|
selector: "app-register-new-account",
|
||||||
|
@ -14,7 +15,8 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
result: string;
|
result: string;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly authService: AuthService) { }
|
private readonly authService: AuthService,
|
||||||
|
private readonly accountsService: AccountsService) { }
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
}
|
}
|
||||||
|
@ -23,6 +25,9 @@ export class RegisterNewAccountComponent implements OnInit {
|
||||||
this.authService.getToken(this.mastodonNode, this.email, this.password)
|
this.authService.getToken(this.mastodonNode, this.email, this.password)
|
||||||
.then((res: TokenData) => {
|
.then((res: TokenData) => {
|
||||||
this.result = res.access_token;
|
this.result = res.access_token;
|
||||||
|
|
||||||
|
this.accountsService.addNewAccount(this.mastodonNode, this.email, res);
|
||||||
|
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
this.result = err;
|
this.result = err;
|
||||||
|
|
|
@ -0,0 +1,97 @@
|
||||||
|
import { Injectable } from "@angular/core";
|
||||||
|
import { Http, Headers, Response } from "@angular/http";
|
||||||
|
import { Subject, BehaviorSubject } from "rxjs";
|
||||||
|
|
||||||
|
import { TokenData, Account } from "./models/mastodon.interfaces";
|
||||||
|
import { ApiRoutes } from "./models/api.settings";
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class AccountsService {
|
||||||
|
private localAccountKey = "localAccounts";
|
||||||
|
private apiRoutes = new ApiRoutes();
|
||||||
|
|
||||||
|
accountsSubject: Subject<LocalAccount[]>;
|
||||||
|
init: Promise<any>; //TODO load this service before any UI action
|
||||||
|
|
||||||
|
constructor(private readonly httpService: Http) {
|
||||||
|
this.init = this.getAllLocalAccount().then((accounts) => {
|
||||||
|
this.accountsSubject = new BehaviorSubject<LocalAccount[]>(accounts);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addNewAccount(mastodonInstance: string, email: string, token: TokenData) {
|
||||||
|
const newAccount = new LocalAccount();
|
||||||
|
newAccount.mastodonInstance = mastodonInstance;
|
||||||
|
newAccount.email = email;
|
||||||
|
newAccount.tokenData = token;
|
||||||
|
|
||||||
|
this.getAllLocalAccount().then((allAccounts) => {
|
||||||
|
allAccounts.push(newAccount);
|
||||||
|
this.saveAccounts(allAccounts);
|
||||||
|
|
||||||
|
this.accountsSubject.next(allAccounts);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private getAllLocalAccount(): Promise<LocalAccount[]> {
|
||||||
|
const allSavedAccounts = this.retrieveSavedAccounts();
|
||||||
|
const allAccounts: LocalAccount[] = [];
|
||||||
|
const allTasks: Promise<any>[] = [];
|
||||||
|
|
||||||
|
for (let savedAcc of allSavedAccounts) {
|
||||||
|
const promise = this.retrieveMastodonDetails(savedAcc)
|
||||||
|
.then((acc) => {
|
||||||
|
allAccounts.push(acc);
|
||||||
|
})
|
||||||
|
.catch(err => console.error(err));
|
||||||
|
|
||||||
|
allTasks.push(promise);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.all(allTasks).then(() => {
|
||||||
|
return allAccounts;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private retrieveMastodonDetails(account: SavedLocalAccount): Promise<LocalAccount> {
|
||||||
|
const localAccount = new LocalAccount();
|
||||||
|
localAccount.mastodonInstance = account.mastodonInstance;
|
||||||
|
localAccount.email = account.email;
|
||||||
|
localAccount.tokenData = account.tokenData;
|
||||||
|
|
||||||
|
const header = new Headers();
|
||||||
|
header.append("Authorization", `Bearer ${localAccount.tokenData.access_token}`);
|
||||||
|
|
||||||
|
return this.httpService.get(localAccount.mastodonInstance + this.apiRoutes.getCurrentAccount, { headers: header }).toPromise()
|
||||||
|
.then((res: Response) => {
|
||||||
|
const mastodonAccount = res.json() as Account;
|
||||||
|
localAccount.mastodonAccount = mastodonAccount;
|
||||||
|
return localAccount;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private retrieveSavedAccounts(): SavedLocalAccount[] {
|
||||||
|
const savedData = <SavedLocalAccount[]>JSON.parse(localStorage.getItem(this.localAccountKey));
|
||||||
|
if (savedData) {
|
||||||
|
return savedData;
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
private saveAccounts(accounts: SavedLocalAccount[]) {
|
||||||
|
localStorage.setItem(this.localAccountKey, JSON.stringify(accounts));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SavedLocalAccount {
|
||||||
|
mastodonInstance: string;
|
||||||
|
email: string;
|
||||||
|
tokenData: TokenData;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LocalAccount implements SavedLocalAccount {
|
||||||
|
mastodonAccount: Account;
|
||||||
|
mastodonInstance: string;
|
||||||
|
email: string;
|
||||||
|
tokenData: TokenData;
|
||||||
|
}
|
Loading…
Reference in New Issue