Skip to content

Commit

Permalink
Merge pull request #45 from yogeshgadge/sso-branch
Browse files Browse the repository at this point in the history
Already logged in or SSO prep work
  • Loading branch information
yogeshgadge authored Feb 1, 2018
2 parents 5086083 + 297f835 commit 8fcd886
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
61 changes: 31 additions & 30 deletions src/app/user/user-menu/user-menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,51 @@ export class CBPUserMenuComponent implements OnInit, OnDestroy {
private _isToolbarExpanded = false;
userMenuExpanded = false;
user?: CBPUser;
loginProgress = false;

isLoggedIn = false;
userDataLoaded = false;
error: any;

private userServiceSubscription: Subscription;


constructor(@Inject(CBP_USER_SERVICE) private userService: CBPUserService) { }

ngOnInit() {}
get loginProgress(): boolean {
return this.userService.loginInProgress;
}
set loginProgress(progress: boolean) {
this.userService.loginInProgress = progress;
}

login() {
this.loginProgress = true;
this.isLoggedIn = false;
if (this.userServiceSubscription) {
this.userService.login();
} else {
this.userServiceSubscription = this.userService.login().subscribe({
next: (data: CBPUser) => {
if (data) {
this.user = data;
this.userDataLoaded = true;
this.loginProgress = false;
this.isLoggedIn = true;
} else {
this.loginProgress = false;
this.isLoggedIn = false;
this.user = data;
this.userDataLoaded = false;
}
},
error: (err: any) => {
console.log('errr' + err);
ngOnInit() {
this.userServiceSubscription = this.userService.getUser().subscribe({
next: (data: CBPUser) => {
if (data) {
this.user = data;
this.userDataLoaded = true;
this.loginProgress = false;
this.isLoggedIn = true;
} else {
this.loginProgress = false;
this.isLoggedIn = false;
this.error = err;
this.user = data;
this.userDataLoaded = false;
},
complete: () => {
console.log('completed');
}
});
}
},
error: (err: any) => {
this.loginProgress = false;
this.isLoggedIn = false;
this.error = err;
this.userDataLoaded = false;
}
});
}

login() {
this.loginProgress = true;
this.isLoggedIn = false;
this.userService.login();
}
ngOnDestroy() {
if (this.userServiceSubscription) {
Expand Down
14 changes: 14 additions & 0 deletions src/app/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ export class CBPUser {
*/
export abstract class CBPUserService {

/**
* Set to true or false
* @type {boolean}
*/

private _loginInProgress = false;
get loginInProgress(): boolean {
return this._loginInProgress;
};

set loginInProgress(loginInProgress: boolean ) {
this._loginInProgress = loginInProgress;
};

/**
* User may not loaded or since user may not be "logged in".
* If not logged in it internally calls login()
Expand Down
7 changes: 6 additions & 1 deletion src/demo/app/demo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ import {CBP_APPLICATIONS_SERVICE} from '../../app/applications/cbp-applications-
bootstrap: [DemoAppComponent]
})
export class DemoAppModule {
constructor(applicationsService: MockApplicationsService) {
constructor(applicationsService: MockApplicationsService, userService: MockUserService) {
// Handle your SSO login here for now
userService.loginInProgress = true;
// set this delay to zero if already loggedIn
// and implement userService so that getUser() immediately returns subject after login()
userService.login(3000);
applicationsService.getCurrentApp().subscribe(currentApp => {
currentApp.version = 'v0.0.0';
});
Expand Down
10 changes: 6 additions & 4 deletions src/mock-services/user.mock.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import {Subject} from 'rxjs/Subject';
import {ReplaySubject} from 'rxjs/ReplaySubject';

@Injectable()
export class MockUserService implements CBPUserService {
export class MockUserService extends CBPUserService {

private subject: ReplaySubject<CBPUser> = new ReplaySubject<CBPUser>();
private loggedIn = false;

constructor() {};
constructor() {
super();
};

getUser(): Subject<CBPUser> {
return this.subject;
}
login(): Subject<CBPUser> {
login(delay = 3000): Subject<CBPUser> {
setTimeout(() => {
this.loggedIn = true;
let user = new CBPUser();
Expand All @@ -26,7 +28,7 @@ export class MockUserService implements CBPUserService {
};
this.subject.next(user);
// this.subject.complete();
}, 3000);
}, delay);
return this.subject;
}

Expand Down

0 comments on commit 8fcd886

Please sign in to comment.