2017-09-03 12:29:41 +02:00
|
|
|
import {Injectable} from "@angular/core";
|
2017-05-10 11:04:06 +02:00
|
|
|
import {Http, Response} from "@angular/http";
|
|
|
|
import "rxjs/add/operator/map";
|
|
|
|
|
|
|
|
import {AppConfig} from "../../app.config";
|
2017-07-27 16:38:35 +02:00
|
|
|
import {AwardingService} from "../awarding-service/awarding.service";
|
|
|
|
import {PromotionService} from "../promotion-service/promotion.service";
|
2017-09-23 11:53:10 +02:00
|
|
|
import {CookieService} from "ngx-cookie-service";
|
2017-05-10 11:04:06 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class LoginService {
|
2017-09-03 12:29:41 +02:00
|
|
|
constructor(private http: Http,
|
2017-07-27 16:38:35 +02:00
|
|
|
private config: AppConfig,
|
2017-09-23 11:53:10 +02:00
|
|
|
private cookieService: CookieService,
|
2017-07-27 16:38:35 +02:00
|
|
|
private awardingService: AwardingService,
|
|
|
|
private promotionService: PromotionService) {
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
login(username: string, password: string) {
|
2017-08-01 23:52:10 +02:00
|
|
|
return this.http.post(this.config.apiAuthenticationPath, {username: username, password: password})
|
2017-05-10 11:04:06 +02:00
|
|
|
.map((response: Response) => {
|
|
|
|
// login successful if there's a jwt token in the response
|
|
|
|
let user = response.json();
|
2017-09-23 11:53:10 +02:00
|
|
|
console.log(user);
|
2017-05-10 11:04:06 +02:00
|
|
|
if (user && user.token) {
|
2017-09-23 11:53:10 +02:00
|
|
|
// store user details and jwt token in cookie
|
|
|
|
this.cookieService.set('currentUser', JSON.stringify(user));
|
2017-07-27 16:44:08 +02:00
|
|
|
if (user.permission >= 2) {
|
2017-07-27 16:38:35 +02:00
|
|
|
const fraction = user.squad.fraction;
|
|
|
|
this.awardingService.checkUnprocessedAwards(fraction);
|
|
|
|
this.promotionService.checkUnconfirmedPromotions(fraction);
|
|
|
|
}
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-08 19:46:36 +02:00
|
|
|
signUp(username: string, password: string, secret: string) {
|
2017-08-01 23:52:10 +02:00
|
|
|
return this.http.post(this.config.apiSignupPath, {username: username, password: password, secret: secret})
|
2017-06-08 19:46:36 +02:00
|
|
|
.map((response: Response) => {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
logout() {
|
2017-09-23 11:53:10 +02:00
|
|
|
this.cookieService.delete('currentUser');
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
isLoggedIn() {
|
2017-09-23 11:53:10 +02:00
|
|
|
return !!this.cookieService.get('currentUser');
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 12:49:59 +02:00
|
|
|
hasPermission(level: number) {
|
2017-09-23 11:53:10 +02:00
|
|
|
return this.isLoggedIn() && this.getCurrentUser().permission >= level;
|
2017-06-08 16:58:28 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 11:53:10 +02:00
|
|
|
getCurrentUser() {
|
|
|
|
return JSON.parse(this.cookieService.get('currentUser'));
|
2017-07-23 10:57:46 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 20:51:15 +02:00
|
|
|
hasSquad() {
|
2017-09-23 11:53:10 +02:00
|
|
|
return this.getCurrentUser().squad != null;
|
2017-07-10 20:51:15 +02:00
|
|
|
}
|
|
|
|
|
2017-05-10 11:04:06 +02:00
|
|
|
}
|