Added logout
Change-Id: I1c01af0c2575e56b795514d5af56bd0fbe4f3133
diff --git a/src/app/services/rest/auth.service.ts b/src/app/services/rest/auth.service.ts
index 7c73c28..c53e3f4 100644
--- a/src/app/services/rest/auth.service.ts
+++ b/src/app/services/rest/auth.service.ts
@@ -14,35 +14,51 @@
@Injectable()
export class AuthService {
- private xosToken:string;
- private xosSessionId:string;
- // Resolve HTTP using the constructor
- constructor (private http: Http, private cookieService:CookieService) {
+ private xosToken: string;
+ private xosSessionId: string;
+ // resolve HTTP using the constructor
+ constructor (private http: Http, private cookieService: CookieService) {
}
// check if the user is authenticated
- isAuthenticated(){
+ isAuthenticated(): string {
this.xosToken = this.cookieService.get('xoscsrftoken');
this.xosSessionId = this.cookieService.get('xossessionid');
return this.xosToken;
}
// save cookies
- storeAuth(auth:IAuthResponse){
+ storeAuth(auth: IAuthResponse): void {
this.cookieService.put('xoscsrftoken', auth.xoscsrftoken);
this.cookieService.put('xossessionid', auth.xossessionid);
}
- // Log the user in
- login(auth: IAuthRequest) : Observable<IAuthResponse> {
+ // remove cookies
+ removeAuth(): void {
+ this.cookieService.remove('xoscsrftoken');
+ this.cookieService.remove('xossessionid');
+ }
+
+ // log the user in
+ login(auth: IAuthRequest): Observable<IAuthResponse> {
return this.http.post(`${AppConfig.apiEndpoint}/utility/login/`, auth)
- .map((res:Response) => res.json())
- .map((auth:IAuthResponse) => {
+ .map((res: Response) => res.json())
+ .map((auth: IAuthResponse) => {
this.storeAuth(auth);
auth.user = JSON.parse(auth.user);
return auth;
})
.catch((error:any) => Observable.throw(error.json().error || 'Server error'));
}
+
+ // logout the user
+ logout(): Observable<any> {
+ return this.http.post(`${AppConfig.apiEndpoint}/utility/logout/`, {xossessionid: this.xosSessionId})
+ .map((res: Response) => {
+ this.removeAuth();
+ return res.text();
+ })
+ .catch((error: any) => Observable.throw(error.json().error || 'Server error'));
+ }
}