Skip to content

Commit c377877

Browse files
committed
adding files
1 parent 2f4bda5 commit c377877

3 files changed

Lines changed: 53 additions & 34 deletions

File tree

src/app/login/login.component.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ export class LoginComponent implements OnInit {
2020
}
2121

2222
checkLogin() {
23-
if (this.loginservice.authenticate(this.username, this.password)
24-
) {
25-
this.router.navigate([''])
26-
this.invalidLogin = false
27-
} else
28-
this.invalidLogin = true
23+
(this.loginservice.authenticate(this.username, this.password).subscribe(
24+
data => {
25+
this.router.navigate([''])
26+
this.invalidLogin = false
27+
},
28+
error => {
29+
this.invalidLogin = true
30+
31+
}
32+
)
33+
);
34+
2935
}
3036

3137
}

src/app/service/authentication.service.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
import { Injectable } from '@angular/core';
2+
import { HttpClient, HttpHeaders } from '@angular/common/http';
3+
import { map } from 'rxjs/operators';
4+
5+
export class User{
6+
constructor(
7+
public status:string,
8+
) {}
9+
10+
}
211

312
@Injectable({
413
providedIn: 'root'
514
})
615
export class AuthenticationService {
716

8-
constructor() { }
17+
constructor(
18+
private httpClient:HttpClient
19+
) {
20+
}
921

10-
authenticate(username, password) {
11-
if (username === "javainuse" && password === "password") {
12-
sessionStorage.setItem('username', username)
13-
return true;
14-
} else {
15-
return false;
22+
authenticate(username, password) {
23+
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
24+
return this.httpClient.get<User>('http://localhost:8080/employees/validateLogin',{headers}).pipe(
25+
map(
26+
userData => {
27+
sessionStorage.setItem('username',username);
28+
return userData;
29+
}
30+
)
31+
32+
);
1633
}
17-
}
34+
1835

1936
isUserLoggedIn() {
2037
let user = sessionStorage.getItem('username')

src/app/service/httpclient.service.ts

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,27 @@ export class HttpClientService {
2424

2525
getEmployees()
2626
{
27-
let basicString=this.getHeaders();
28-
29-
let headers=new HttpHeaders(
30-
{Authorization:basicString}
31-
);
32-
console.log("test call");
33-
return this.httpClient.get<Employee[]>('http://localhost:8080/employees',{headers});
27+
let username='javainuse'
28+
let password='password'
29+
30+
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
31+
32+
return this.httpClient.get<Employee[]>('http://localhost:8080/employees',{headers});
3433
}
3534

3635
public deleteEmployee(employee) {
37-
return this.httpClient.delete<Employee>("http://localhost:8080/employees" + "/"+ employee.empId);
36+
let username='javainuse'
37+
let password='password'
38+
39+
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
40+
return this.httpClient.delete<Employee>("http://localhost:8080/employees" + "/"+ employee.empId,{headers});
3841
}
3942

4043
public createEmployee(employee) {
41-
return this.httpClient.post<Employee>("http://localhost:8080/employees", employee);
44+
let username='javainuse'
45+
let password='password'
46+
47+
const headers = new HttpHeaders({ Authorization: 'Basic ' + btoa(username + ':' + password) });
48+
return this.httpClient.post<Employee>("http://localhost:8080/employees", employee,{headers});
4249
}
43-
44-
45-
46-
getHeaders(){
47-
let username='admin'
48-
let password='password'
49-
50-
let basicString='Basic '+window.btoa(username + ':' + password)
51-
return basicString;
52-
}
53-
5450
}

0 commit comments

Comments
 (0)