2016-05-09 23:04:56 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-12-27 23:09:47 +08:00
|
|
|
import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO";
|
2017-07-04 01:17:49 +08:00
|
|
|
import {BehaviorSubject} from "rxjs/BehaviorSubject";
|
2016-12-27 06:36:38 +08:00
|
|
|
import {UserService} from "./user.service";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
2016-12-27 06:36:38 +08:00
|
|
|
import {Cookie} from "ng2-cookies";
|
2016-05-05 00:34:54 +08:00
|
|
|
import {ErrorCodes} from "../../../../common/entities/Error";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../../../common/config/public/Config";
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
declare module ServerInject {
|
2017-06-11 04:32:56 +08:00
|
|
|
export let user: UserDTO;
|
2016-04-10 00:06:29 +08:00
|
|
|
}
|
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
@Injectable()
|
2016-05-09 23:04:56 +08:00
|
|
|
export class AuthenticationService {
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public user: BehaviorSubject<UserDTO>;
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
constructor(private _userService: UserService) {
|
2017-07-04 01:17:49 +08:00
|
|
|
this.user = new BehaviorSubject(null);
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
//picking up session..
|
|
|
|
if (this.isAuthenticated() == false && Cookie.get('pigallery2-session') != null) {
|
|
|
|
if (typeof ServerInject !== "undefined" && typeof ServerInject.user !== "undefined") {
|
2017-07-04 01:17:49 +08:00
|
|
|
this.user.next(ServerInject.user);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
this.getSessionUser();
|
|
|
|
} else {
|
2017-07-04 01:17:49 +08:00
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
this.user.next(<UserDTO>{name: "", password: "", role: UserRoles.Admin});
|
|
|
|
}
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
private async getSessionUser(): Promise<void> {
|
|
|
|
try {
|
|
|
|
this.user.next(await this._userService.getSessionUser());
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
2016-05-05 04:20:54 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-05 04:20:54 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
public async login(credential: LoginCredential): Promise<UserDTO> {
|
|
|
|
try {
|
|
|
|
const user = await this._userService.login(credential);
|
|
|
|
this.user.next(user);
|
|
|
|
return user;
|
|
|
|
} catch (error) {
|
|
|
|
if (typeof error.code !== "undefined") {
|
|
|
|
console.log(ErrorCodes[error.code] + ", message: ", error.message);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public isAuthenticated(): boolean {
|
|
|
|
if (Config.Client.authenticationRequired === false) {
|
|
|
|
return true;
|
2016-05-02 03:30:43 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
return !!(this.user.value && this.user.value != null);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
public logout() {
|
|
|
|
this._userService.logout();
|
2017-07-04 01:17:49 +08:00
|
|
|
this.user.next(null);
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-17 05:15:03 +08:00
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
|
|
|
|
}
|