Commit 76c28d75 authored by Victor Costa's avatar Victor Costa

Share login state between angular theme and noosfero

parent 6e474a07
Pipeline #10403937 passed with stages
in 12 minutes and 39 seconds
......@@ -5,7 +5,7 @@ import {SessionService} from './session.service';
import { RestangularModule, RestangularHttp, Restangular } from 'ngx-restangular';
import { async, fakeAsync, tick, TestBed, ComponentFixture, flushMicrotasks } from '@angular/core/testing';
import {MockBackend, MockConnection} from '@angular/http/testing';
import {Http, Headers, RequestOptions, URLSearchParams, Request, RequestMethod, JsonpModule, HttpModule, BaseRequestOptions} from '@angular/http';
import {Jsonp, Http, Headers, RequestOptions, URLSearchParams, Request, RequestMethod, JsonpModule, HttpModule, BaseRequestOptions} from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import * as helpers from '../../spec/helpers';
......@@ -24,6 +24,7 @@ describe("Services", () => {
providers: [
{ provide: PersonService, useValue: mocks.personService},
{ provide: SessionService, useValue: mocks.sessionService},
{ provide: Jsonp, useValue: mocks.jsonp },
AuthService,
].concat(helpers.provideMockBackend())
});
......
......@@ -16,7 +16,9 @@ export class AuthService {
private sessionService: SessionService,
private restangular: Restangular,
private personService: PersonService
) { }
) {
this.reloadUser();
}
loginFromCookie() {
if (this.sessionService.currentUser()) return;
......@@ -25,13 +27,15 @@ export class AuthService {
}
reloadUser() {
if (this.currentUser() && this.currentUser().person) {
this.personService.getLoggedPerson().then((result: noosfero.RestResult<noosfero.Person>) => {
this.currentUser().person = result.data;
}).catch((error: any) => {
this.sessionService.destroy();
});
}
this.personService.getLoggedPerson().then((result: noosfero.RestResult<noosfero.Person>) => {
const person = result.data;
const user = person['user'];
user.person = person;
person['user'] = null;
this.loginSuccessCallback({data: user});
}).catch((error: any) => {
this.logout();
});
}
private loginSuccessCallback(response: any) {
......@@ -61,9 +65,8 @@ export class AuthService {
public logout() {
const user: noosfero.User = this.sessionService.currentUser();
this.sessionService.destroy();
this.logoutSuccess.next(user);
this.jsonp.get('/account/logout'); // FIXME logout from noosfero to sync login state
this.jsonp.get('/account/logout').subscribe(); // logout from noosfero to sync login state
}
public isAuthenticated() {
......
......@@ -182,7 +182,8 @@ export function getMocks() {
search: () => Observable.of([mocks.profile]),
getFriendshipState: () => Promise.resolve({ }),
addFriend: () => Promise.resolve({ data: {} }),
removeFriend: () => Promise.resolve({ data: {} })
removeFriend: () => Promise.resolve({ data: {} }),
getLoggedPerson: () => Promise.resolve({ data: {} })
},
communityService: {
sendInvitations: (communityId: number, people: noosfero.Person[]) => Observable.of({ success: true }),
......@@ -335,6 +336,9 @@ export function getMocks() {
bodyStateClassesService: {
start: () => {},
changeClasses: new EventEmitter()
},
jsonp: {
get: () => Observable.of({})
}
};
return mocks;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment