Created
September 20, 2016 19:13
-
-
Save TheSniper102/9613aff6e6b4f13614ca1b4b000b0358 to your computer and use it in GitHub Desktop.
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 'angular2/core'; | |
import { Http, Response, Headers } from 'angular2/http'; | |
import { Observable } from 'rxjs/Rx'; | |
import { ExceptionService, SpinnerService } from '../../blocks/blocks'; | |
import { CONFIG, MessageService } from '../../shared/shared'; | |
let authUrl = CONFIG.baseUrls.auth; | |
export interface UserData { | |
userID: number; | |
userName: string; | |
defaultLang: number; | |
admin: number; | |
active: number; | |
} | |
export interface LoginViewModel { | |
userName: string; | |
password: string; | |
} | |
@Injectable() | |
export class AuthService { | |
loggedIn = false; | |
authResult: any; | |
constructor(private _http: Http, | |
private _exceptionService: ExceptionService, | |
private _messageService: MessageService, | |
private _spinnerService: SpinnerService) { | |
this.loggedIn = !!sessionStorage.getItem('currentUser'); | |
} | |
login(loginViewModel: LoginViewModel) { | |
let body = JSON.stringify(loginViewModel); | |
var headers = new Headers(); | |
headers.append('Content-Type', 'application/json'); | |
this._spinnerService.show(); | |
return this._http | |
.post(`${authUrl}/login`, body, { headers: headers }) | |
.map(res => res.json()) | |
.map((res) => { | |
if (res.success) { | |
sessionStorage.setItem('currentUser', JSON.stringify(res.user)); | |
this.loggedIn = true; | |
} | |
return res.success; | |
}) | |
.finally(() => this._spinnerService.hide()); | |
} | |
logout() { | |
this._spinnerService.show(); | |
return this._http.get(`${authUrl}/logout`) | |
.map((response: Response) => response.json()) | |
.map((res) => { | |
if (res.success) { | |
sessionStorage.clear(); | |
this.loggedIn = false; | |
} | |
return false; | |
}) | |
.finally(() => this._spinnerService.hide()); | |
} | |
isLogged() { | |
this.loggedIn = !!sessionStorage.getItem('currentUser'); | |
return this.loggedIn; | |
} | |
getCurrentUser() | |
{ | |
if (this.isLogged) | |
{ | |
let value = sessionStorage.getItem('currentUser'); | |
if (value && value != "undefined" && value != "null") { | |
return <UserData>JSON.parse(value); | |
} | |
return null; | |
} | |
return null; | |
} | |
} |
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
// <reference path="../typings/es6-shim/es6-shim.d.ts"/> | |
import {bootstrap} from "angular2/platform/browser" | |
import { FORM_PROVIDERS } from 'angular2/common'; | |
import {ROUTER_PROVIDERS} from "angular2/router"; | |
import {HTTP_PROVIDERS} from "angular2/http"; | |
import { AuthService } from "./security/auth/auth.service"; | |
import {AppComponent} from "./app.component"; | |
bootstrap(AppComponent, [FORM_PROVIDERS, ROUTER_PROVIDERS, | |
HTTP_PROVIDERS, AuthService]) | |
.then(success => console.log(`Bootstrap success`)) | |
.catch(error => console.log(error));; | |
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 {Directive, Attribute, DynamicComponentLoader, ElementRef} from 'angular2/core'; | |
import {Router, RouterOutlet, ComponentInstruction} from 'angular2/router'; | |
import { AuthService } from '../security/auth/auth'; | |
@Directive({ | |
selector: 'router-outlet' | |
}) | |
export class LoggedInRouterOutlet extends RouterOutlet { | |
publicRoutes: Array<string>; | |
private parentRouter: Router; | |
constructor(_elementRef: ElementRef, _loader: DynamicComponentLoader, | |
_parentRouter: Router, @Attribute('name') nameAttr: string, | |
private _authService: AuthService) { | |
super(_elementRef, _loader, _parentRouter, nameAttr); | |
this.parentRouter = _parentRouter; | |
// The Boolean following each route below | |
// denotes whether the route requires authentication to view | |
this.publicRoutes = ['login']; | |
} | |
activate(instruction: ComponentInstruction) { | |
if (this._canActivate(instruction.urlPath)) { | |
return super.activate(instruction); | |
} | |
this.parentRouter.navigate(['Login']); | |
} | |
_canActivate(url) { | |
return this.publicRoutes.indexOf(url) !== -1 || this._authService.isLogged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment