|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
|
import {Headers, Http, Request} from '@angular/http';
|
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
|
|
|
|
import {Login} from '../../models/login';
|
|
|
|
|
import {User} from 'src/app/models/user';
|
|
|
|
|
import {DatasharingService} from '../datasharing.service';
|
|
|
|
@ -8,69 +8,17 @@ import {environment} from 'src/environments/environment';
|
|
|
|
|
import {FriendRequest} from 'src/app/models/friendRequest';
|
|
|
|
|
import {FriendInfo} from 'src/app/models/friendinfo';
|
|
|
|
|
import {GroupInfo} from 'src/app/models/groupinfo';
|
|
|
|
|
import {IUser} from '../../models/interfaces/IUser';
|
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
|
import {tap} from 'rxjs/operators';
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class LoginService {
|
|
|
|
|
|
|
|
|
|
constructor(private http: Http, private data: DatasharingService, private router: Router) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public login(login: Login, errorCb: any) {
|
|
|
|
|
|
|
|
|
|
const headers = new Headers();
|
|
|
|
|
headers.set('Content-Type', 'application/json');
|
|
|
|
|
|
|
|
|
|
return this.http.post(environment.graphQLUrl, this.buildJson(login))
|
|
|
|
|
.subscribe(response => {
|
|
|
|
|
this.loginSuccess();
|
|
|
|
|
this.updateUserInfo(response.json());
|
|
|
|
|
}, errorCb
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public loginSuccess() {
|
|
|
|
|
this.router.navigateByUrl('');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public updateUserInfo(response: any) {
|
|
|
|
|
const user: User = new User();
|
|
|
|
|
let friendRequest: FriendRequest = new FriendRequest();
|
|
|
|
|
user.loggedIn = true;
|
|
|
|
|
user.userID = response.data.login.id;
|
|
|
|
|
user.username = response.data.login.name;
|
|
|
|
|
user.handle = response.data.login.handle;
|
|
|
|
|
user.email = response.data.login.email;
|
|
|
|
|
user.points = response.data.login.points;
|
|
|
|
|
user.level = response.data.login.level;
|
|
|
|
|
for (const friend of response.data.login.friends) {
|
|
|
|
|
user.friends.push(new FriendInfo(friend.id, friend.name, friend.level));
|
|
|
|
|
}
|
|
|
|
|
for (const group of response.data.login.groups) {
|
|
|
|
|
user.groups.push(new GroupInfo(group.id, group.name));
|
|
|
|
|
}
|
|
|
|
|
user.chatIDs = response.data.login.chats;
|
|
|
|
|
for (const request of response.data.login.sentRequests) {
|
|
|
|
|
user.sentRequestUserIDs.push(request.receiver.id);
|
|
|
|
|
}
|
|
|
|
|
for (const request of response.data.login.receivedRequests) {
|
|
|
|
|
friendRequest = new FriendRequest();
|
|
|
|
|
friendRequest.id = request.id;
|
|
|
|
|
friendRequest.senderUserID = request.sender.id;
|
|
|
|
|
friendRequest.senderUsername = request.sender.name;
|
|
|
|
|
friendRequest.senderHandle = request.sender.handle;
|
|
|
|
|
user.receivedRequests.push(friendRequest);
|
|
|
|
|
}
|
|
|
|
|
if (JSON.parse(response.data.login.settings).darkmode === 'true') {
|
|
|
|
|
user.darkmode = true;
|
|
|
|
|
}
|
|
|
|
|
this.data.changeUserInfo(user);
|
|
|
|
|
interface ILoginRequestResult {
|
|
|
|
|
data: {
|
|
|
|
|
login: IUser;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public buildJson(login: Login): any {
|
|
|
|
|
const body = {
|
|
|
|
|
query: `mutation($email: String!, $pwHash: String!) {
|
|
|
|
|
const graphqlQuery = `mutation($email: String!, $pwHash: String!) {
|
|
|
|
|
login(email: $email, passwordHash: $pwHash) {
|
|
|
|
|
id,
|
|
|
|
|
name,
|
|
|
|
@ -94,12 +42,42 @@ export class LoginService {
|
|
|
|
|
},
|
|
|
|
|
settings
|
|
|
|
|
}
|
|
|
|
|
}`
|
|
|
|
|
, variables: {
|
|
|
|
|
}`;
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class LoginService extends BaseService {
|
|
|
|
|
|
|
|
|
|
constructor(private http: HttpClient, private datasharingService: DatasharingService, private router: Router) {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the body for the login request
|
|
|
|
|
* @param login
|
|
|
|
|
*/
|
|
|
|
|
private static buildRequestBody(login: Login): any {
|
|
|
|
|
return {
|
|
|
|
|
query: graphqlQuery,
|
|
|
|
|
variables: {
|
|
|
|
|
email: login.email,
|
|
|
|
|
pwHash: login.passwordHash,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a login request and returns the data of the logged in user.
|
|
|
|
|
* @param login
|
|
|
|
|
*/
|
|
|
|
|
public login(login: Login) {
|
|
|
|
|
const body = LoginService.buildRequestBody(login);
|
|
|
|
|
return this.http.post<ILoginRequestResult>(environment.graphQLUrl, body)
|
|
|
|
|
.pipe(tap(response => {
|
|
|
|
|
const user = new User();
|
|
|
|
|
user.assignFromResponse(response.data.login);
|
|
|
|
|
this.datasharingService.changeUserInfo(user);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|