浏览代码

Auth service integration

master
kj1352 3 年前
父节点
当前提交
0cbe00a608
共有 7 个文件被更改,包括 72 次插入7 次删除
  1. +2
    -0
      src/app/app.module.ts
  2. +1
    -0
      src/app/dashboard/table/table.component.ts
  3. +3
    -3
      src/app/login/login.component.html
  4. +15
    -3
      src/app/login/login.component.ts
  5. +16
    -0
      src/app/services/auth.service.spec.ts
  6. +30
    -0
      src/app/services/auth.service.ts
  7. +5
    -1
      src/app/services/partner-profile.service.ts

+ 2
- 0
src/app/app.module.ts 查看文件

@@ -16,6 +16,7 @@ import { SettingsComponent } from './dashboard/settings/settings.component';
import { PartnerProfileService } from './services/partner-profile.service'; import { PartnerProfileService } from './services/partner-profile.service';
import { PartnerDetailsComponent } from './dashboard/partner-details/partner-details.component'; import { PartnerDetailsComponent } from './dashboard/partner-details/partner-details.component';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { AuthService } from './services/auth.service';


@NgModule({ @NgModule({
declarations: [ declarations: [
@@ -40,6 +41,7 @@ import { FormsModule } from '@angular/forms';
], ],
providers: [ providers: [
PartnerProfileService, PartnerProfileService,
AuthService
], ],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })


+ 1
- 0
src/app/dashboard/table/table.component.ts 查看文件

@@ -19,6 +19,7 @@ export class TableComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
this.partnerProfileService.getPartnersData().then((data: any) => { this.partnerProfileService.getPartnersData().then((data: any) => {
this.userData = data; this.userData = data;
console.log(data);
}, (e) => console.log(e)); }, (e) => console.log(e));
} }




+ 3
- 3
src/app/login/login.component.html 查看文件

@@ -10,19 +10,19 @@
<h5> Log In </h5> <h5> Log In </h5>


<div class="input-holder"> <div class="input-holder">
<input type="text">
<input type="text" [(ngModel)]="credentails.username">
<label> Username </label> <label> Username </label>
</div> </div>
<div class="input-holder"> <div class="input-holder">
<input type="password">
<input type="password" [(ngModel)]="credentails.password">
<label> Password </label> <label> Password </label>
</div> </div>
<div class="action-buttons"> <div class="action-buttons">
<a href="#"> Forgot Password? </a> <a href="#"> Forgot Password? </a>
<button class="button" (click)="authenticateUser()">
<button class="button" (click)="login()">
Login Login
</button> </button>
</div> </div>


+ 15
- 3
src/app/login/login.component.ts 查看文件

@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { AuthService } from '../services/auth.service';


@Component({ @Component({
selector: 'app-login', selector: 'app-login',
@@ -7,16 +8,27 @@ import { Router } from '@angular/router';
styleUrls: ['./login.component.scss'] styleUrls: ['./login.component.scss']
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
credentails: {
username: string,
password: string,
} = {
username: 'admin@catalysts.org',
password: '1234'
};


constructor( constructor(
private router: Router
private router: Router,
private authService: AuthService
) { } ) { }


ngOnInit(): void { 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));
} }


} }

+ 16
- 0
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();
});
});

+ 30
- 0
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));
}
}

+ 5
- 1
src/app/services/partner-profile.service.ts 查看文件

@@ -2,6 +2,10 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { lastValueFrom } from 'rxjs'; import { lastValueFrom } from 'rxjs';


export const BASE_URL = 'http://localhost:8001';
// http://localhost:8001
// http://143.110.247.94

@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
@@ -12,7 +16,7 @@ export class PartnerProfileService {
) { } ) { }


async getPartnersData() { 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')));
} }


} }

正在加载...
取消
保存