Sengi-Windows-MacOS-Linux/src/app/services/auth.service.ts

40 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-03-17 04:52:10 +01:00
import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";
2018-09-08 06:49:11 +02:00
import { ApiRoutes } from './models/api.settings';
import { AppData, TokenData } from "./models/mastodon.interfaces";
2018-03-17 04:52:10 +01:00
@Injectable()
export class AuthService {
2018-09-09 07:29:23 +02:00
private apiRoutes = new ApiRoutes();
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
constructor(
private readonly httpClient: HttpClient) {
}
2018-03-17 04:52:10 +01:00
getInstanceLoginUrl(instance: string, client_id: string, redirect_uri: string): string{
return `https://${instance}/oauth/authorize?scope=${encodeURIComponent('read write follow')}&response_type=code&redirect_uri=${encodeURIComponent(redirect_uri)}&client_id=${client_id}`;
}
2018-09-09 07:29:23 +02:00
getToken(instance: string, client_id: string, client_secret: string, code: string, redirect_uri: string): Promise<TokenData> {
const url = `https://${instance}/oauth/token?client_id=${client_id}&client_secret=${client_secret}&grant_type=authorization_code&code=${code}&redirect_uri=${encodeURIComponent(redirect_uri)}`;
2018-03-17 04:52:10 +01:00
2018-09-09 07:29:23 +02:00
return this.httpClient.post<TokenData>(url, null).toPromise();
}
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
createNewApplication(instance: string, appName: string, redirectUrl: string, scopes: string, website: string): Promise<AppData> {
const url = 'https://' + instance + this.apiRoutes.createApp;
const formData = new FormData();
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
formData.append('client_name', appName);
formData.append('redirect_uris', redirectUrl);
formData.append('scopes', scopes);
formData.append('website', website);
2018-09-08 06:49:11 +02:00
2018-09-09 07:29:23 +02:00
return this.httpClient.post<AppData>(url, formData).toPromise();
}
2018-03-17 04:52:10 +01:00
}
export class CurrentAuthProcess {
constructor(public username: string, public instance: string) { }
}