From 0cbe00a60899191bdd2492c88bfb66638234d924 Mon Sep 17 00:00:00 2001 From: kj1352 Date: Wed, 23 Feb 2022 13:48:16 +0530 Subject: [PATCH] Auth service integration --- src/app/app.module.ts | 2 ++ src/app/dashboard/table/table.component.ts | 1 + src/app/login/login.component.html | 6 ++--- src/app/login/login.component.ts | 18 ++++++++++--- src/app/services/auth.service.spec.ts | 16 +++++++++++ src/app/services/auth.service.ts | 30 +++++++++++++++++++++ src/app/services/partner-profile.service.ts | 6 ++++- 7 files changed, 72 insertions(+), 7 deletions(-) create mode 100644 src/app/services/auth.service.spec.ts create mode 100644 src/app/services/auth.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index aba762c..bddd953 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -16,6 +16,7 @@ import { SettingsComponent } from './dashboard/settings/settings.component'; import { PartnerProfileService } from './services/partner-profile.service'; import { PartnerDetailsComponent } from './dashboard/partner-details/partner-details.component'; import { FormsModule } from '@angular/forms'; +import { AuthService } from './services/auth.service'; @NgModule({ declarations: [ @@ -40,6 +41,7 @@ import { FormsModule } from '@angular/forms'; ], providers: [ PartnerProfileService, + AuthService ], bootstrap: [AppComponent] }) diff --git a/src/app/dashboard/table/table.component.ts b/src/app/dashboard/table/table.component.ts index 7474c2b..5a38f0e 100644 --- a/src/app/dashboard/table/table.component.ts +++ b/src/app/dashboard/table/table.component.ts @@ -19,6 +19,7 @@ export class TableComponent implements OnInit { ngOnInit(): void { this.partnerProfileService.getPartnersData().then((data: any) => { this.userData = data; + console.log(data); }, (e) => console.log(e)); } diff --git a/src/app/login/login.component.html b/src/app/login/login.component.html index 45e933a..d01d6ce 100644 --- a/src/app/login/login.component.html +++ b/src/app/login/login.component.html @@ -10,19 +10,19 @@
Log In
- +
- +
Forgot Password? -
diff --git a/src/app/login/login.component.ts b/src/app/login/login.component.ts index a3de9ae..41407f1 100644 --- a/src/app/login/login.component.ts +++ b/src/app/login/login.component.ts @@ -1,5 +1,6 @@ import { Component, OnInit } from '@angular/core'; import { Router } from '@angular/router'; +import { AuthService } from '../services/auth.service'; @Component({ selector: 'app-login', @@ -7,16 +8,27 @@ import { Router } from '@angular/router'; styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit { + credentails: { + username: string, + password: string, + } = { + username: 'admin@catalysts.org', + password: '1234' + }; constructor( - private router: Router + private router: Router, + private authService: AuthService ) { } ngOnInit(): void { } - authenticateUser() { - this.router.navigate(['/dashboard/analytics']); + login() { + this.authService.authenticateUser(this.credentails).then((data: any) => { + localStorage.setItem('token', data.token); + this.router.navigate(['/dashboard/analytics']); + }, (e) => console.log(e)); } } diff --git a/src/app/services/auth.service.spec.ts b/src/app/services/auth.service.spec.ts new file mode 100644 index 0000000..f1251ca --- /dev/null +++ b/src/app/services/auth.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { AuthService } from './auth.service'; + +describe('AuthService', () => { + let service: AuthService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(AuthService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts new file mode 100644 index 0000000..4ba6e43 --- /dev/null +++ b/src/app/services/auth.service.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@angular/core'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; +import { lastValueFrom } from 'rxjs'; +import { BASE_URL } from './partner-profile.service'; + +@Injectable({ + providedIn: 'root' +}) +export class AuthService { + + constructor( + private http: HttpClient + ) { } + + async authenticateUser(credentials: { + username: string, + password: string, + }) { + const body = `&username=${encodeURIComponent(credentials.username)}&password=${encodeURIComponent(credentials.password)}`; + const headers = new HttpHeaders({ + Accept: 'text/html, application/xhtml+xml, */*', + 'Content-Type': 'application/x-www-form-urlencoded', + }); + const httpOptions = { + headers, + responseType: 'text' as 'json', + }; + return lastValueFrom(this.http.post(BASE_URL + '/api-auth/', body, httpOptions)); + } +} diff --git a/src/app/services/partner-profile.service.ts b/src/app/services/partner-profile.service.ts index 4c6b26a..2e4d5c8 100644 --- a/src/app/services/partner-profile.service.ts +++ b/src/app/services/partner-profile.service.ts @@ -2,6 +2,10 @@ import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { lastValueFrom } from 'rxjs'; +export const BASE_URL = 'http://localhost:8001'; +// http://localhost:8001 +// http://143.110.247.94 + @Injectable({ providedIn: 'root' }) @@ -12,7 +16,7 @@ export class PartnerProfileService { ) { } async getPartnersData() { - return lastValueFrom(this.http.get('http://143.110.247.94/user-data/?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MjAyY2I3YmQwMWMxMzRhYjdjZmViOGUiLCJleHAiOjE2NDk0NjI0MDAsImlhdCI6MTY0NDM1MTEwMH0.MghrYB51zg5Qk2fk1yx9NO1nPLpdhwMK69XIuFrmrAY')); + return lastValueFrom(this.http.get(BASE_URL + '/user-data/?token=' + localStorage.getItem('token'))); } }