Running Prettier in Project
This commit is contained in:
@@ -1,17 +1,25 @@
|
||||
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import {first, firstValueFrom, map, Observable, of, Subject, take, tap} from 'rxjs';
|
||||
import {
|
||||
first,
|
||||
firstValueFrom,
|
||||
map,
|
||||
Observable,
|
||||
of,
|
||||
Subject,
|
||||
take,
|
||||
tap,
|
||||
} from 'rxjs';
|
||||
import { catchError } from 'rxjs/operators';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { HttpError } from '../model/httpError/httpError.model';
|
||||
import { User } from '../model/user/user.model';
|
||||
import * as http from "http";
|
||||
import * as http from 'http';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthService {
|
||||
|
||||
private userAuthenticated!: User;
|
||||
|
||||
authSubject = new Subject<User | HttpError | null>();
|
||||
@@ -20,30 +28,35 @@ export class AuthService {
|
||||
|
||||
readonly BACKEND_OAUTH_PATH = environment.backendOAuthPath;
|
||||
|
||||
constructor(private http: HttpClient) { }
|
||||
constructor(private http: HttpClient) {}
|
||||
login(userAuthAtempt: User): void {
|
||||
this.validateUser(this.loginUser(userAuthAtempt));
|
||||
}
|
||||
|
||||
googleLogin() {
|
||||
window.open(this.BACKEND_OAUTH_PATH + '/oauth2/authorization/google', '_self');
|
||||
window.open(
|
||||
this.BACKEND_OAUTH_PATH + '/oauth2/authorization/google',
|
||||
'_self',
|
||||
);
|
||||
}
|
||||
|
||||
githubLogin() {
|
||||
window.open(this.BACKEND_OAUTH_PATH + '/oauth2/authorization/github', '_self');
|
||||
window.open(
|
||||
this.BACKEND_OAUTH_PATH + '/oauth2/authorization/github',
|
||||
'_self',
|
||||
);
|
||||
}
|
||||
|
||||
loginGoogleUser(p: any): void {
|
||||
this.validateUser(this.fetchGoogleOAuthToken(p))
|
||||
this.validateUser(this.fetchGoogleOAuthToken(p));
|
||||
}
|
||||
|
||||
loginGithubUser(p: any): void {
|
||||
this.validateUser(this.fetchGithubOAuthToken(p))
|
||||
this.validateUser(this.fetchGithubOAuthToken(p));
|
||||
}
|
||||
|
||||
signup(userAuthAtempt: User): void {
|
||||
this.validateUser(this.createUser(userAuthAtempt));
|
||||
|
||||
}
|
||||
|
||||
refresh(): void {
|
||||
@@ -56,7 +69,7 @@ export class AuthService {
|
||||
|
||||
logout() {
|
||||
this.authSubject.next(null);
|
||||
this.destroySessions().subscribe()
|
||||
this.destroySessions().subscribe();
|
||||
}
|
||||
|
||||
deleteAccount() {
|
||||
@@ -66,7 +79,7 @@ export class AuthService {
|
||||
addProfilePicture(file: File): void {
|
||||
const fileType = file.type.split('/')[1];
|
||||
this.getAddProfilePictureUrl(fileType).subscribe({
|
||||
next: (url: string|null) => {
|
||||
next: (url: string | null) => {
|
||||
if (url != null) {
|
||||
this.uploadProfilePicture(url, file).then(
|
||||
(response: Observable<any>) => {
|
||||
@@ -75,169 +88,160 @@ export class AuthService {
|
||||
this.processProfilePicture().subscribe(
|
||||
() => {
|
||||
this.refresh();
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private loginUser(userAuthAtempt: User): Observable<User|any> {
|
||||
|
||||
private loginUser(userAuthAtempt: User): Observable<User | any> {
|
||||
let loginParams = new URLSearchParams();
|
||||
loginParams.set("username", userAuthAtempt.username!);
|
||||
loginParams.set("password", userAuthAtempt.password!);
|
||||
loginParams.set('username', userAuthAtempt.username!);
|
||||
loginParams.set('password', userAuthAtempt.password!);
|
||||
|
||||
let headers = new HttpHeaders({
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
});
|
||||
|
||||
return this.http.post<User>(
|
||||
this.BACKEND_PATH + "/user/login",
|
||||
loginParams,
|
||||
{ headers: headers, withCredentials: true }
|
||||
).pipe(
|
||||
first()
|
||||
)
|
||||
return this.http
|
||||
.post<User>(this.BACKEND_PATH + '/user/login', loginParams, {
|
||||
headers: headers,
|
||||
withCredentials: true,
|
||||
})
|
||||
.pipe(first());
|
||||
}
|
||||
|
||||
private fetchGoogleOAuthToken(p: any): Observable<User|any> {
|
||||
private fetchGoogleOAuthToken(p: any): Observable<User | any> {
|
||||
let params = new HttpParams({
|
||||
fromObject: p,
|
||||
});
|
||||
|
||||
let params = new HttpParams(
|
||||
{
|
||||
fromObject: p
|
||||
}
|
||||
);
|
||||
|
||||
return this.http.get<User>(
|
||||
this.BACKEND_OAUTH_PATH + '/login/oauth2/code/google',
|
||||
{
|
||||
return this.http
|
||||
.get<User>(this.BACKEND_OAUTH_PATH + '/login/oauth2/code/google', {
|
||||
withCredentials: true,
|
||||
params: params
|
||||
},
|
||||
).pipe(
|
||||
first()
|
||||
);
|
||||
params: params,
|
||||
})
|
||||
.pipe(first());
|
||||
}
|
||||
|
||||
private fetchGithubOAuthToken(p: any): Observable<User|any> {
|
||||
private fetchGithubOAuthToken(p: any): Observable<User | any> {
|
||||
let params = new HttpParams({
|
||||
fromObject: p,
|
||||
});
|
||||
|
||||
let params = new HttpParams(
|
||||
{
|
||||
fromObject: p
|
||||
}
|
||||
);
|
||||
|
||||
return this.http.get<User>(
|
||||
this.BACKEND_OAUTH_PATH + '/login/oauth2/code/github',
|
||||
{
|
||||
return this.http
|
||||
.get<User>(this.BACKEND_OAUTH_PATH + '/login/oauth2/code/github', {
|
||||
withCredentials: true,
|
||||
params: params
|
||||
},
|
||||
).pipe(
|
||||
first()
|
||||
);
|
||||
params: params,
|
||||
})
|
||||
.pipe(first());
|
||||
}
|
||||
|
||||
private createUser(newUser: User) {
|
||||
return this.http.post<User>(
|
||||
this.BACKEND_PATH + "/user/signup",
|
||||
newUser,
|
||||
{ withCredentials: true }
|
||||
).pipe(
|
||||
first()
|
||||
)
|
||||
return this.http
|
||||
.post<User>(this.BACKEND_PATH + '/user/signup', newUser, {
|
||||
withCredentials: true,
|
||||
})
|
||||
.pipe(first());
|
||||
}
|
||||
|
||||
private refreshAccessToken() {
|
||||
return this.http.post<User>(
|
||||
this.BACKEND_PATH + "/user/login/refresh",
|
||||
this.BACKEND_PATH + '/user/login/refresh',
|
||||
this.userAuthenticated.refreshToken,
|
||||
{ withCredentials: true }
|
||||
{ withCredentials: true },
|
||||
);
|
||||
}
|
||||
|
||||
private validateSession(): Observable<User> {
|
||||
return this.http.get<User>(
|
||||
this.BACKEND_PATH + '/session/validate',
|
||||
{ withCredentials: true }
|
||||
);
|
||||
}
|
||||
|
||||
private destroySessions() {
|
||||
return this.http.delete(
|
||||
this.BACKEND_PATH + '/session/destroy',
|
||||
{ withCredentials: true }
|
||||
);
|
||||
}
|
||||
|
||||
private deleteAccountRequest() {
|
||||
let headers = this.createAuthorizationHeader()
|
||||
|
||||
return this.http.delete(
|
||||
this.BACKEND_PATH + `/user/delete`,
|
||||
{ headers: headers, withCredentials: true }
|
||||
);
|
||||
}
|
||||
|
||||
private validateUser(userAuthAtempt: Observable<User>) {
|
||||
userAuthAtempt.pipe(
|
||||
catchError(error => {
|
||||
if (error.status == 0) {
|
||||
return of(<HttpError>{
|
||||
title: "Service Unavailable",
|
||||
status: 500,
|
||||
details: "Service Unavailable, please try again later.",
|
||||
developerMessage: "Service Unavailable, please try again later.",
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
}
|
||||
return of(<HttpError>error.error);
|
||||
}),
|
||||
first()
|
||||
).subscribe({
|
||||
next: userAuthentication => {
|
||||
this.userAuthenticated = <User>userAuthentication;
|
||||
this.authSubject.next(this.userAuthenticated);
|
||||
}
|
||||
return this.http.get<User>(this.BACKEND_PATH + '/session/validate', {
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
|
||||
private getAddProfilePictureUrl(fileType: string): Observable<string|null> {
|
||||
return this.http.post<{ presigned_url: string, file_key: string }>(
|
||||
this.BACKEND_PATH + '/user/profile-picture?fileType=' + fileType,
|
||||
null,
|
||||
{
|
||||
headers: this.createAuthorizationHeader(),
|
||||
withCredentials: true
|
||||
}
|
||||
).pipe(
|
||||
first(),
|
||||
map((res) => {
|
||||
if (!!res && !!res.presigned_url) {
|
||||
return res.presigned_url;
|
||||
}
|
||||
return null
|
||||
})
|
||||
)
|
||||
private destroySessions() {
|
||||
return this.http.delete(this.BACKEND_PATH + '/session/destroy', {
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
|
||||
private async uploadProfilePicture(url: string, file: File): Promise<Observable<any>> {
|
||||
private deleteAccountRequest() {
|
||||
let headers = this.createAuthorizationHeader();
|
||||
|
||||
return this.http.delete(this.BACKEND_PATH + `/user/delete`, {
|
||||
headers: headers,
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
|
||||
private validateUser(userAuthAtempt: Observable<User>) {
|
||||
userAuthAtempt
|
||||
.pipe(
|
||||
catchError((error) => {
|
||||
if (error.status == 0) {
|
||||
return of(<HttpError>{
|
||||
title: 'Service Unavailable',
|
||||
status: 500,
|
||||
details:
|
||||
'Service Unavailable, please try again later.',
|
||||
developerMessage:
|
||||
'Service Unavailable, please try again later.',
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
return of(<HttpError>error.error);
|
||||
}),
|
||||
first(),
|
||||
)
|
||||
.subscribe({
|
||||
next: (userAuthentication) => {
|
||||
this.userAuthenticated = <User>userAuthentication;
|
||||
this.authSubject.next(this.userAuthenticated);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private getAddProfilePictureUrl(
|
||||
fileType: string,
|
||||
): Observable<string | null> {
|
||||
return this.http
|
||||
.post<{ presigned_url: string; file_key: string }>(
|
||||
this.BACKEND_PATH +
|
||||
'/user/profile-picture?fileType=' +
|
||||
fileType,
|
||||
null,
|
||||
{
|
||||
headers: this.createAuthorizationHeader(),
|
||||
withCredentials: true,
|
||||
},
|
||||
)
|
||||
.pipe(
|
||||
first(),
|
||||
map((res) => {
|
||||
if (!!res && !!res.presigned_url) {
|
||||
return res.presigned_url;
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
private async uploadProfilePicture(
|
||||
url: string,
|
||||
file: File,
|
||||
): Promise<Observable<any>> {
|
||||
const fileData = await this.readAsArrayBuffer(file);
|
||||
let headers = new HttpHeaders({
|
||||
'Content-Type': file.type
|
||||
})
|
||||
return this.http.put(
|
||||
url,
|
||||
fileData,
|
||||
{
|
||||
headers: headers,
|
||||
}
|
||||
);
|
||||
'Content-Type': file.type,
|
||||
});
|
||||
return this.http.put(url, fileData, {
|
||||
headers: headers,
|
||||
});
|
||||
}
|
||||
|
||||
private processProfilePicture() {
|
||||
@@ -246,21 +250,22 @@ export class AuthService {
|
||||
null,
|
||||
{
|
||||
headers: this.createAuthorizationHeader(),
|
||||
withCredentials: true
|
||||
}
|
||||
)
|
||||
withCredentials: true,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private createAuthorizationHeader(): HttpHeaders {
|
||||
return new HttpHeaders({
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer ' + this.userAuthenticated.accessToken?.token
|
||||
Authorization:
|
||||
'Bearer ' + this.userAuthenticated.accessToken?.token,
|
||||
});
|
||||
}
|
||||
|
||||
private async readAsArrayBuffer(file: File): Promise<ArrayBuffer> {
|
||||
const reader = new FileReader();
|
||||
reader.readAsArrayBuffer(file)
|
||||
reader.readAsArrayBuffer(file);
|
||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||
reader.onload = () => {
|
||||
resolve(reader.result as ArrayBuffer);
|
||||
|
||||
Reference in New Issue
Block a user