2016-05-01 00:01:54 +08:00
|
|
|
///<reference path="../../../browser.d.ts"/>
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
import {Injectable} from "@angular/core";
|
2016-05-01 00:01:54 +08:00
|
|
|
import {User} from "../../../../common/entities/User";
|
|
|
|
import {Event} from "../../../../common/event/Event";
|
|
|
|
import {UserService} from "./user.service.ts";
|
|
|
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
|
|
|
import {Message} from "../../../../common/entities/Message";
|
2016-05-09 23:04:56 +08:00
|
|
|
import {Cookie} from "ng2-cookies/ng2-cookies";
|
2016-05-05 00:34:54 +08:00
|
|
|
import {ErrorCodes} from "../../../../common/entities/Error";
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
declare module ServerInject {
|
|
|
|
export var user;
|
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
|
|
|
|
2016-03-19 16:58:27 +08:00
|
|
|
private _user:User = null;
|
2016-05-17 05:15:03 +08:00
|
|
|
public OnUserChanged:Event<User>;
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
constructor(private _userService:UserService) {
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged = new Event();
|
2016-03-21 03:06:14 +08:00
|
|
|
|
|
|
|
//picking up session..
|
2016-05-09 23:04:56 +08:00
|
|
|
if (this.isAuthenticated() == false && Cookie.getCookie('pigallery2-session') != null) {
|
|
|
|
if (typeof ServerInject !== "undefined" && typeof ServerInject.user !== "undefined") {
|
2016-04-10 00:06:29 +08:00
|
|
|
console.log("user found");
|
|
|
|
this.setUser(ServerInject.user);
|
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
this.getSessionUser();
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
private getSessionUser() {
|
|
|
|
this._userService.getSessionUser().then((message:Message<User>) => {
|
|
|
|
if (message.error) {
|
2016-03-26 18:19:10 +08:00
|
|
|
console.log(message.error);
|
2016-05-09 23:04:56 +08:00
|
|
|
} else {
|
2016-03-21 03:06:14 +08:00
|
|
|
this._user = message.result;
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged.trigger(this._user);
|
2016-03-21 03:06:14 +08:00
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
private setUser(user:User) {
|
2016-05-05 04:20:54 +08:00
|
|
|
this._user = user;
|
2016-05-17 05:15:03 +08:00
|
|
|
this.OnUserChanged.trigger(this._user);
|
2016-05-05 04:20:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public login(credential:LoginCredential) {
|
2016-05-09 23:04:56 +08:00
|
|
|
this._userService.login(credential).then((message:Message<User>) => {
|
|
|
|
if (message.error) {
|
|
|
|
console.log(ErrorCodes[message.error.code] + "message: " + message.error.message);
|
|
|
|
} else {
|
2016-04-10 00:06:29 +08:00
|
|
|
this.setUser(message.result);
|
2016-03-19 16:58:27 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-05-05 04:20:54 +08:00
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
public isAuthenticated():boolean {
|
2016-03-21 03:06:14 +08:00
|
|
|
return (this._user && this._user != null) ? true : false;
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
public getUser() {
|
2016-05-02 03:30:43 +08:00
|
|
|
return this._user;
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2016-05-17 05:15:03 +08:00
|
|
|
public logout() {
|
|
|
|
this._userService.logout();
|
|
|
|
this.setUser(null);
|
|
|
|
}
|
|
|
|
|
2016-03-13 18:28:29 +08:00
|
|
|
|
|
|
|
}
|