First iteration of auth service

This commit is contained in:
Nicolas Constant 2018-03-16 23:52:10 -04:00
parent 8940611c84
commit c3ec804956
6 changed files with 153 additions and 21 deletions

View File

@ -81,6 +81,7 @@
<Folder Include="src\app\models\" />
<Folder Include="src\app\pages\" />
<Folder Include="src\app\services\" />
<Folder Include="src\app\services\models\" />
<Folder Include="src\assets\" />
<Folder Include="src\environments\" />
</ItemGroup>
@ -106,6 +107,8 @@
<SubType>Code</SubType>
</TypeScriptCompile>
<TypeScriptCompile Include="src\app\models\stream.models.ts" />
<TypeScriptCompile Include="src\app\services\auth.service.ts" />
<TypeScriptCompile Include="src\app\services\models\api.settings.ts" />
<TypeScriptCompile Include="src\environments\environment.prod.ts" />
<TypeScriptCompile Include="src\environments\environment.ts" />
<TypeScriptCompile Include="src\main.ts" />

View File

@ -1,16 +1,19 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { BrowserModule } from "@angular/platform-browser";
import { FormsModule } from "@angular/forms";
import { HttpModule } from "@angular/http";
import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";
import { NgxElectronModule } from 'ngx-electron';
import { NgxElectronModule } from "ngx-electron";
import { AppComponent } from './app.component';
import { LeftSideBarComponent } from './components/left-side-bar/left-side-bar.component';
import { StreamsMainDisplayComponent } from './pages/streams-main-display/streams-main-display.component';
import { StreamComponent } from './components/stream/stream.component';
import { StreamsSelectionFooterComponent } from './components/streams-selection-footer/streams-selection-footer.component';
import { TootComponent } from './components/toot/toot.component';
import { RegisterNewAccountComponent } from './pages/register-new-account/register-new-account.component';
import { AppComponent } from "./app.component";
import { LeftSideBarComponent } from "./components/left-side-bar/left-side-bar.component";
import { StreamsMainDisplayComponent } from "./pages/streams-main-display/streams-main-display.component";
import { StreamComponent } from "./components/stream/stream.component";
import { StreamsSelectionFooterComponent } from "./components/streams-selection-footer/streams-selection-footer.component";
import { TootComponent } from "./components/toot/toot.component";
import { RegisterNewAccountComponent } from "./pages/register-new-account/register-new-account.component";
import { AuthService } from "./services/auth.service";
const routes: Routes = [
{ path: "", redirectTo: "home", pathMatch: "full" },
@ -30,11 +33,13 @@ const routes: Routes = [
RegisterNewAccountComponent
],
imports: [
BrowserModule,
BrowserModule,
HttpModule,
FormsModule,
NgxElectronModule,
RouterModule.forRoot(routes)
],
providers: [],
providers: [AuthService],
bootstrap: [AppComponent]
})
export class AppModule { }

View File

@ -1,6 +1,18 @@
<form (ngSubmit)="onSubmit()">
<label>Mastodon Node Address</label>
<input type="text" class="form-control" [(ngModel)]="mastodonNode" name="mastodonNode" /><br />
<label>Email</label>
<input type="text" class="form-control" [(ngModel)]="email" name="email" /><br />
<label>Password</label>
<input type="password" class="form-control" [(ngModel)]="password" name="password" /><br/>
<button type="submit" class="btn btn-success">Submit</button>
</form>
<p>
register-new-account works!
<a href title="close" [routerLink]="['/home']">close</a>
Result:<br/>
{{ result }}
</p>
<a href title="close" [routerLink]="['/home']">close</a>

View File

@ -1,15 +1,32 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, Input } from "@angular/core";
import { AuthService } from "../../services/auth.service";
@Component({
selector: 'app-register-new-account',
templateUrl: './register-new-account.component.html',
styleUrls: ['./register-new-account.component.css']
selector: "app-register-new-account",
templateUrl: "./register-new-account.component.html",
styleUrls: ["./register-new-account.component.css"]
})
export class RegisterNewAccountComponent implements OnInit {
@Input() mastodonNode: string;
@Input() email: string;
@Input() password: string;
result: string;
constructor() { }
constructor(
private readonly authService: AuthService) { }
ngOnInit() {
}
onSubmit(): boolean {
this.authService.getToken(this.mastodonNode, this.email, this.password)
.then((res: string) => {
this.result = res;
})
.catch(err => {
this.result = err;
});
return false;
}
}

View File

@ -0,0 +1,48 @@
import { Injectable } from "@angular/core";
import { Http, Response, RequestOptions } from "@angular/http";
import { ApiRoutes } from "./models/api.settings";
@Injectable()
export class AuthService {
private apiRoutes = new ApiRoutes();
constructor(
private readonly httpService: Http) {
}
getToken(
mastodonNode: string, email: string, password: string): Promise<string> {
//TODO retrieve those via API
const clientId = localStorage.getItem("client_id");
const clientSecret = localStorage.getItem("client_secret");
//Retrieve Token
const url = this.getHostUrl(mastodonNode) + this.apiRoutes.getToken;
const options = new RequestOptions();
const formData = new FormData();
formData.append("client_id", clientId);
formData.append("client_secret", clientSecret);
formData.append("grant_type", "password");
formData.append("username", email);
formData.append("password", password);
formData.append("scope", "read write follow");
return this.httpService.post(url, formData, options).toPromise()
.then((res: Response) => {
const result = res.json();
console.warn(result);
return result;
});
}
private getHostUrl(url: string): string {
url = url.replace("http://", "");
if (!url.startsWith("https://")) {
url = "https://" + url;
}
return url;
}
}

View File

@ -0,0 +1,47 @@
export class ApiRoutes {
createApp = "/api/v1/apps";
getToken = "/oauth/token";
getAccount = "/api/v1/accounts/{0}";
getCurrentAccount = "/api/v1/accounts/verify_credentials";
getAccountFollowers = "/api/v1/accounts/{0}/followers";
getAccountFollowing = "/api/v1/accounts/{0}/following";
getAccountStatuses = "/api/v1/accounts/{0}/statuses";
follow = "/api/v1/accounts/{0}/follow";
unfollow = "/api/v1/accounts/{0}/unfollow";
block = "/api/v1/accounts/{0}/block";
unblock = "/api/v1/accounts/{0}/unblock";
mute = "/api/v1/accounts/{0}/mute";
unmute = "/api/v1/accounts/{0}/unmute";
getAccountRelationships = "/api/v1/accounts/relationships";
searchForAccounts = "/api/v1/accounts/search";
getBlocks = "/api/v1/blocks";
getFavourites = "/api/v1/favourites";
getFollowRequests = "/api/v1/follow_requests";
authorizeFollowRequest = "/api/v1/follow_requests/authorize";
rejectFollowRequest = "/api/v1/follow_requests/reject";
followRemote = "/api/v1/follows";
getInstance = "/api/v1/instance";
uploadMediaAttachment = "/api/v1/media";
getMutes = "/api/v1/mutes";
getNotifications = "/api/v1/notifications";
getSingleNotifications = "/api/v1/notifications/{0}";
clearNotifications = "/api/v1/notifications/clear";
getReports = "/api/v1/reports";
reportUser = "/api/v1/reports";
search = "/api/v1/search";
getStatus = "/api/v1/statuses/{0}";
getStatusContext = "/api/v1/statuses/{0}/context";
getStatusCard = "/api/v1/statuses/{0}/card";
getStatusRebloggedBy = "/api/v1/statuses/{0}/reblogged_by";
getStatusFavouritedBy = "/api/v1/statuses/{0}/favourited_by";
postNewStatus = "/api/v1/statuses";
deleteStatus = "/api/v1/statuses/{0}";
reblogStatus = "/api/v1/statuses/{0}/reblog";
unreblogStatus = "/api/v1/statuses/{0}/unreblog";
favouritingStatus = "/api/v1/statuses/{0}/favourite";
unfavouritingStatus = "/api/v1/statuses/{0}/unfavourite";
getHomeTimeline = "/api/v1/timelines/home";
getPublicTimeline = "/api/v1/timelines/public";
getHastagTimeline = "/api/v1/timelines/tag/{0}";
}