Last active
December 13, 2016 10:01
-
-
Save aegyed91/d986af5c639eac743965d504daa6409b to your computer and use it in GitHub Desktop.
rxjs good or not :f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Router } from '@angular/router'; | |
import { CanActivate, CanActivateChild, ActivatedRouteSnapshot } from '@angular/router'; | |
import { AuthS } from '../../routes/auth/auth.s'; | |
import { UserSt } from '../../routes/user/user.st'; | |
import { isEmpty } from 'lodash'; | |
@Injectable() | |
export class LoggedInG implements CanActivate/*, CanActivateChild*/ { | |
constructor(private router: Router, private auth: AuthS, public userSt: UserSt) { | |
} | |
canActivate(route: ActivatedRouteSnapshot) { | |
return new Promise((resolve, reject) => { | |
if (this.auth.isAuthenticated()) { | |
const tokenScopes = this.auth.getPayload().scope; | |
// TODO(tsm): handle scopes | |
if ((route.data as {scope}).scope && !(route.data as {scope}).scope.every(s => tokenScopes.includes(s))) { | |
// this.router.navigate(['/auth/login']); | |
// return resolve(false); | |
} | |
if (isEmpty(this.userSt.get())) { | |
return this.userSt.populate().then(() => resolve(true)).catch(err => { | |
return this.auth.oLogout().then(() => { | |
this.router.navigate(['/auth/login']); | |
return resolve(false); | |
}); | |
}); | |
} else { | |
return resolve(true); | |
} | |
} else { | |
this.router.navigate(['/auth/login']); | |
return resolve(false); | |
} | |
}); | |
} | |
// canActivateChild() { | |
// return true; | |
// } | |
} | |
@Injectable() | |
export class LoggedOutG implements CanActivate/*, CanActivateChild*/ { | |
constructor(private router: Router, private auth: AuthS) { | |
} | |
canActivate() { | |
if (this.auth.isAuthenticated()) { | |
this.router.navigate(['/user/profile']); | |
return false; | |
} else { | |
return true; | |
} | |
} | |
// canActivateChild() { | |
// return true; | |
// } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject, Subscription } from 'rxjs'; | |
import { get, set, assign, merge, isEmpty } from 'lodash'; | |
import { IS_DEV } from '../constants'; | |
import { ResMsgHandlerS } from './res-msg-handler.s'; | |
export interface IStateOpts { | |
initState?: Object; | |
} | |
@Injectable() | |
export class StateS { | |
state; | |
mh; | |
constructor(mh: ResMsgHandlerS) { | |
this.mh = mh; | |
this.state = new BehaviorSubject({}); | |
} | |
subscribe(next, error?, complete?): Subscription { | |
return this.state.distinctUntilChanged().share().subscribe(next, error, complete); | |
} | |
get(path?: string, defaultValue?: any) { | |
return isEmpty(path) | |
? this.state.getValue() | |
: get(this.state.getValue(), path, defaultValue); | |
} | |
set(path: string, value: any) { | |
this.state.next(set(this.get(), path, value)); | |
return this; | |
} | |
assign(obj: Object) { | |
this.state.next(assign({}, this.get(), obj)); | |
return this; | |
} | |
merge(obj: Object) { | |
this.state.next(merge({}, this.get(), obj)); | |
return this; | |
} | |
// fetch initial state from api | |
populate() { | |
} | |
// stuff u do after initial state set | |
afterPopulate() { | |
} | |
// set state to an empty object | |
clear() { | |
this.state.next({}); | |
} | |
// stuff u do b4 freeing up allocated mem | |
destroy() { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { JwtHttp } from 'ng2-ui-auth'; | |
import { API_BASE_URL } from '../../shared/constants'; | |
import { ResMsgHandlerS } from '../../shared/services/res-msg-handler.s'; | |
@Injectable() | |
export class UserS { | |
constructor(private http: JwtHttp, private mh: ResMsgHandlerS) { | |
} | |
getUser() { | |
return this.http.get(`${API_BASE_URL}/user/profile`).toPromise() | |
.then(res => res.json()) | |
.catch(err => Promise.reject(this.mh.normalizeErr(err))); | |
} | |
setUser(data) { | |
return this.http.post(`${API_BASE_URL}/user/profile`, data).toPromise().then(res => res.json()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { StateS } from '../../shared/services/state.s'; | |
import { UserS } from './user.s'; | |
import { ResMsgHandlerS } from '../../shared/services/res-msg-handler.s'; | |
@Injectable() | |
export class UserSt extends StateS { | |
constructor(public mh: ResMsgHandlerS, private userS: UserS) { | |
super(mh); | |
} | |
// fetch initial state from api | |
populate() { | |
return new Promise((resolve, reject) => { | |
this.userS.getUser() | |
.then(res => this.assign(res.user).afterPopulate()) | |
.then(() => resolve()) | |
.catch(err => reject(err)); | |
}); | |
} | |
// stuff u do after initial state set | |
afterPopulate() { | |
if (!this.get('isEmailVerified')) { | |
this.mh.showInToastr('Confirm your email address.', 2); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment