[8423429] | 1 | import {Injectable} from '@angular/core';
|
---|
| 2 | import {HttpClient, HttpParams} from "@angular/common/http";
|
---|
| 3 | import {ActiveUserInterface} from "./ActiveUserInterface";
|
---|
| 4 | import {Router} from "@angular/router";
|
---|
| 5 |
|
---|
| 6 | @Injectable({
|
---|
| 7 | providedIn: 'root'
|
---|
| 8 | })
|
---|
| 9 | export class LoginService {
|
---|
| 10 |
|
---|
| 11 | url = "http://localhost:8080/api/login"
|
---|
| 12 | activeUser: ActiveUserInterface | undefined;
|
---|
| 13 | currentCourse: number | undefined;
|
---|
| 14 |
|
---|
| 15 | constructor(private http: HttpClient, private router: Router) {
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | loginUser(username: string, password: string) {
|
---|
| 19 | const params = new HttpParams()
|
---|
| 20 | .append('username', username)
|
---|
| 21 | .append('password', password);
|
---|
| 22 | console.log("params", params)
|
---|
| 23 | this.http.get<ActiveUserInterface>(this.url, {params:params})
|
---|
| 24 | .subscribe(resp => {
|
---|
| 25 | console.log("Response login", resp);
|
---|
| 26 | this.activeUser = resp;
|
---|
| 27 | console.log("active user", this.activeUser)
|
---|
| 28 | if (this.activeUser.userType==="ADMIN")
|
---|
| 29 | this.router.navigate(['/admin-panel'])
|
---|
| 30 | else
|
---|
| 31 | this.router.navigate(['/courses']);
|
---|
| 32 | })
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | setCurrentCourse(id: number) : void{
|
---|
| 36 | this.currentCourse = id;
|
---|
| 37 | console.log("Current course is ", id);
|
---|
| 38 | }
|
---|
| 39 | }
|
---|