src/app/core/auth/auth.service.ts
Properties |
|
Methods |
|
constructor(http: Http, router: Router, notificationsService: NotificationsService, permissionsService: PermissionsService)
|
||||||||||||||||||||
Defined in src/app/core/auth/auth.service.ts:23
|
||||||||||||||||||||
Parameters :
|
Private cleanToken |
cleanToken()
|
Defined in src/app/core/auth/auth.service.ts:149
|
Returns :
void
|
getHeaderName |
getHeaderName()
|
Defined in src/app/core/auth/auth.service.ts:109
|
Returns :
string
|
getToken |
getToken()
|
Defined in src/app/core/auth/auth.service.ts:105
|
Returns :
any
|
getUser |
getUser()
|
Defined in src/app/core/auth/auth.service.ts:131
|
Returns :
any
|
isLoggedIn |
isLoggedIn()
|
Defined in src/app/core/auth/auth.service.ts:101
|
Returns :
boolean
|
Private processError | ||||||||
processError(response: Response)
|
||||||||
Defined in src/app/core/auth/auth.service.ts:143
|
||||||||
Parameters :
Returns :
any
|
processUnauthorized | ||||||||
processUnauthorized(response: Response)
|
||||||||
Defined in src/app/core/auth/auth.service.ts:135
|
||||||||
Parameters :
Returns :
void
|
resetPassword |
resetPassword(hash: string, password: string)
|
Defined in src/app/core/auth/auth.service.ts:86
|
Returns :
any
|
setUser |
setUser()
|
Defined in src/app/core/auth/auth.service.ts:113
|
Returns :
void
|
signIn |
signIn(email: string, password: string, rememberMe: boolean)
|
Defined in src/app/core/auth/auth.service.ts:37
|
Returns :
any
|
signOut |
signOut()
|
Defined in src/app/core/auth/auth.service.ts:65
|
Returns :
any
|
signUp |
signUp(password: string, hash: string)
|
Defined in src/app/core/auth/auth.service.ts:51
|
Returns :
any
|
startResetPassword | ||||||||
startResetPassword(email: string)
|
||||||||
Defined in src/app/core/auth/auth.service.ts:79
|
||||||||
Parameters :
Returns :
any
|
Private userChanged |
userChanged()
|
Defined in src/app/core/auth/auth.service.ts:155
|
Returns :
void
|
Readonly Private authTokenKey |
authTokenKey:
|
Default value : 'xZaSwqS'
|
Defined in src/app/core/auth/auth.service.ts:14
|
Readonly Private errorMessageKey |
errorMessageKey:
|
Default value : "X-Error-Message"
|
Defined in src/app/core/auth/auth.service.ts:16
|
Private loggedIn |
loggedIn:
|
Type : boolean
|
Default value : true
|
Defined in src/app/core/auth/auth.service.ts:18
|
Readonly Private tokenInHeader |
tokenInHeader:
|
Default value : "X-Auth-Token"
|
Defined in src/app/core/auth/auth.service.ts:15
|
Private user |
user:
|
Type : User
|
Default value : new EmptyUser
|
Defined in src/app/core/auth/auth.service.ts:19
|
Public userState |
userState:
|
Default value : this.userSubject.asObservable()
|
Defined in src/app/core/auth/auth.service.ts:23
|
Private userSubject |
userSubject:
|
Default value : new Subject<User>()
|
Defined in src/app/core/auth/auth.service.ts:21
|
import {Injectable} from "@angular/core";
import {Http, Response} from "@angular/http";
import {Router} from "@angular/router";
import {NotificationsService} from "../notifications/notifications.service";
import {Observable, Subject} from "rxjs";
import {PermissionsService} from "./permissions.service";
import {User, EmptyUser} from "../models/auth/user.model";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class AuthService {
private readonly authTokenKey = 'xZaSwqS';
private readonly tokenInHeader = "X-Auth-Token";
private readonly errorMessageKey = "X-Error-Message";
private loggedIn: boolean = true;
private user: User = new EmptyUser;
private userSubject = new Subject<User>();
public userState = this.userSubject.asObservable();
constructor(private http: Http,
private router: Router,
private notificationsService: NotificationsService,
private permissionsService: PermissionsService) {
this.loggedIn = !!localStorage.getItem(this.authTokenKey);
if (this.loggedIn) {
this.setUser()
}
}
signIn(email: string, password: string, rememberMe: boolean = false) {
return this.http.post('/auth/api/v1/authentication/signIn', JSON.stringify({email, password, rememberMe}))
.map(res => res.json())
.map(res => {
if (!!res.token) {
localStorage.setItem(this.authTokenKey, res.token);
this.loggedIn = true;
this.setUser();
}
return res
})
.catch(rs => this.processError(rs))
}
signUp(password: string, hash: string) {
return this.http.post('/auth/api/v1/authentication/signUp/' + hash, JSON.stringify({password}))
.map(res => res.json())
.map(res => {
if (!!res.token) {
localStorage.setItem(this.authTokenKey, res.token);
this.loggedIn = true;
this.setUser();
}
return res
})
.catch(rs => this.processError(rs))
}
signOut() {
return this.http.post('/auth/api/v1/authentication/signOut', {})
.catch(error => {
if (error.status === 401) {
this.cleanToken()
}
return Observable.throw(error);
})
.map(rs => {
localStorage.removeItem(this.authTokenKey);
this.loggedIn = false;
});
}
startResetPassword(email: string) {
localStorage.removeItem(this.authTokenKey);
return this.http.post('auth/api/v1/authentication/password/forgot', JSON.stringify({email}))
.map(res => res.json())
.catch(rs => this.processError(rs))
}
resetPassword(hash: string, password: string) {
localStorage.removeItem(this.authTokenKey);
return this.http.post('/auth/api/v1/authentication/password/recover/' + hash, JSON.stringify({newPassword: password}))
.map(res => res.json())
.map(res => {
if (!!res.token) {
localStorage.setItem(this.authTokenKey, res.token);
this.loggedIn = true;
this.setUser();
}
return res
})
.catch(rs => this.processError(rs))
}
isLoggedIn() {
return this.loggedIn;
}
getToken() {
return localStorage.getItem(this.authTokenKey);
}
getHeaderName() {
return this.tokenInHeader;
}
setUser() {
this.http.get('auth/api/v1/authentication/identity')
.map(res => res.json())
.subscribe(
(user: User) => {
this.user = user;
this.permissionsService.setPermissions(user.role.permissions);
this.userChanged();
},
error => {
if (error.status === 401) {
this.cleanToken()
}
console.error(error);
}
)
}
getUser() {
return this.user;
}
processUnauthorized(response: Response): void {
console.log('Process unauthorized', response);
if (response.status === 401) {
this.cleanToken();
this.notificationsService.error(response.json())
}
}
private processError(response: Response) {
this.processUnauthorized(response);
console.log('process error', response);
return Observable.throw(response);
}
private cleanToken() {
localStorage.removeItem(this.authTokenKey);
this.loggedIn = false;
this.router.navigate(['/auth']);
}
private userChanged() {
this.userSubject.next(this.user);
}
}