Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Observable } from 'rxjs/Observable';
- import { BehaviorSubject } from 'rxjs';
- import { HttpClient, HttpHeaders, HttpErrorResponse } from '@angular/common/http';
- import { Signin, ResetPassword } from '../model/user.model';
- import { Router } from '@angular/router';
- @Injectable()
- export class AuthService {
- private URL_Register = 'http://localhost:52718/account/register'; //Register User (Working)
- private URL_Signin = 'http://localhost:52718/account/Signin'; // Login User (Working)
- private URL_Token = 'http://localhost:52718/connect/token'; // Get Token Against Register User (Working)
- private URL_Data = 'http://localhost:52718/api/values'; // Get Token Against Register User (Working)
- private URL_Claims = 'http://localhost:52718/account/Get';
- private URL_PasswordRecovery = 'http://localhost:52718/account/ForgotPassword';
- private URL_ChangePassword = 'http://localhost:52718/account/ChangePassword';
- public isUserAuthenticated: boolean = false;
- private loggedIn = new BehaviorSubject<boolean>(false); // {1}
- get isLoggedIn() {
- return this.loggedIn.asObservable(); // {2}
- }
- constructor(
- private http: HttpClient,
- private router: Router) { }
- userAuthentication(credientials: Signin) {
- let reqHeader = new HttpHeaders({ 'Content-Type': 'application/json' });
- return this.http.post(this.URL_Signin, JSON.stringify(credientials), { headers: reqHeader }).subscribe((data: any) => {
- localStorage.setItem('userToken', data.token);
- this.loggedIn.next(true);
- this.router.navigate(['/dashboard']);
- },
- (err: HttpErrorResponse) => {
- console.log('Auth Service (Method userAuthentication): [HttpError] ' + err.message)
- });
- }
- fetchData() {
- return this.http.get(this.URL_Data);
- }
- getUserClaims() {
- return this.http.get(this.URL_Claims);
- }
- logout() {
- // remove user from local storage to log user out
- localStorage.removeItem('userToken');
- this.loggedIn.next(false);
- this.router.navigate(['/login']);
- }
- forgotPassword(ResetPassword: ResetPassword) {
- let reqHeader = new HttpHeaders({ 'Content-Type': 'application/json' });
- return this.http.post(this.URL_PasswordRecovery, JSON.stringify(ResetPassword), { headers: reqHeader }).subscribe((data: any) => { },
- (err: HttpErrorResponse) => {
- console.log('Auth Service (Method userAuthentication): [HttpError] ' + err)
- });;
- }
- changePassword(QueryParams: Object) {
- let reqHeader = new HttpHeaders({ 'Content-Type': 'application/json' });
- return this.http.put(this.URL_ChangePassword, JSON.stringify(QueryParams), { headers: reqHeader }).subscribe((data: any) => {
- localStorage.setItem('userToken', data.token);
- this.loggedIn.next(true);
- this.router.navigate(['/dashboard']);
- },
- (err: HttpErrorResponse) => {
- console.log('Auth Service (Method changePassword): [HttpError] ' + err)
- });;
- }
- }
- get isLoggedIn() {
- return this.loggedIn.asObservable(); // {2}
- }
- get isLoggedIn() {
- let currentUser = localStorage.getItem('userToken');
- if (!currentUser) { }
- this.loggedIn.next(true);
- return this.loggedIn.asObservable();
- }
Add Comment
Please, Sign In to add comment