-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(service): added user and admin services
* add auth guard and fix login * add auth guard and fix login * feat(auth-service): fetch permissions * fix(login): fix bug in login and permissions * fix(login): fix bug in auth guard * feat(service): add user service - not completed * feat(admin-service): add services for manage users * fix(service): fix some missed imports * fix(test): fix bug in test
- Loading branch information
1 parent
9d267b2
commit eb30611
Showing
13 changed files
with
266 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { CanActivateFn } from '@angular/router'; | ||
|
||
import { authGuard } from './auth.guard'; | ||
|
||
describe('authGuard', () => { | ||
const executeGuard: CanActivateFn = (...guardParameters) => | ||
TestBed.runInInjectionContext(() => authGuard(...guardParameters)); | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(executeGuard).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, CanActivateFn, Router } from '@angular/router'; | ||
import { catchError, Observable } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
import { AuthService } from '../services/auth/auth.service'; | ||
|
||
export const authGuard: CanActivateFn = (route, state) => { | ||
if (route && state) return true; | ||
return true; | ||
}; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private authService: AuthService, | ||
private router: Router, | ||
) {} | ||
|
||
canActivate(): Observable<boolean> { | ||
return this.authService.getPermissions().pipe( | ||
map((permissions) => { | ||
if (permissions.permission !== '') { | ||
return true; | ||
} else { | ||
this.router.navigate(['/login']); | ||
return false; | ||
} | ||
}), | ||
catchError(() => { | ||
this.router.navigate(['/login']); | ||
return [false]; | ||
}), | ||
); | ||
// return this.authService.isLoggedIn$.pipe( | ||
// map((isLoggedIn) => { | ||
// if (!isLoggedIn) { | ||
// this.router.navigate(['/login']); | ||
// return false; | ||
// } | ||
// return true; | ||
// }), | ||
// ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export interface User { | ||
firstName: string; | ||
lastName: string; | ||
imageURL: string; | ||
} | ||
|
||
export interface LoginRequest { | ||
username: string; | ||
password: string; | ||
rememberMe: boolean; | ||
} | ||
|
||
export interface LoginResponse { | ||
firstName: string; | ||
lastName: string; | ||
imageURL: string | null; | ||
} | ||
|
||
export interface UserPermissions { | ||
username: string; | ||
firstName: string; | ||
lastName: string; | ||
permission: string; | ||
} | ||
|
||
export interface RegisterRequest { | ||
username: string; | ||
password: string; | ||
confirmPassword: string; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string; | ||
roleName: string; | ||
} | ||
|
||
export interface RegisterResponse { | ||
username: string; | ||
password: string; | ||
confirmPassword: string; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string; | ||
roleName: string; | ||
} | ||
|
||
export interface UpdateUserRequest { | ||
username: string; | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
phoneNumber: string; | ||
roleName: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { AdminService } from './admin.service'; | ||
|
||
describe('AdminService', () => { | ||
let service: AdminService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(AdminService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { RegisterRequest, UpdateUserRequest } from '../../models/User'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AdminService { | ||
private apiUrl = 'https://localhost:44322/api/Admin'; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
createUser(request: RegisterRequest) { | ||
return this.http.post(`${this.apiUrl}/register`, request); | ||
} | ||
|
||
getUsers(limit = 10, page = 1) { | ||
return this.http.get( | ||
`${this.apiUrl}/GetUsersPagination?limit=${limit}&page=${page}`, | ||
); | ||
} | ||
|
||
deleteUser(id: string) { | ||
return this.http.delete(`${this.apiUrl}/DeleteUser?id=${id}`); | ||
} | ||
|
||
updateUser(id: string, request: UpdateUserRequest) { | ||
return this.http.put(`${this.apiUrl}/UpdateUser?id=${id}`, request); | ||
} | ||
|
||
firstAdmin() { | ||
return this.http.get(`${this.apiUrl}/firstAdmin`); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { BehaviorSubject, Observable, Subject, tap } from 'rxjs'; | ||
import { LoginRequest, LoginResponse } from './auth.model'; | ||
import { | ||
LoginRequest, | ||
LoginResponse, | ||
UserPermissions, | ||
} from '../../models/User'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class AuthService { | ||
private apiUrl = 'http://localhost:5000/api/User'; | ||
private apiUrl = 'https://localhost:44322/api/User'; | ||
|
||
private userData = new Subject<unknown>(); | ||
private userData = new Subject<LoginResponse>(); | ||
private isLoggedIn = new BehaviorSubject<boolean>(false); | ||
private permissions = new Subject<UserPermissions>(); | ||
|
||
isLoggedIn$ = this.isLoggedIn.asObservable(); | ||
userData$ = this.userData.asObservable(); | ||
permissions$ = this.permissions.asObservable(); | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
login(loginRequest: LoginRequest): Observable<LoginResponse> { | ||
return this.http | ||
.post<LoginResponse>(this.apiUrl + '/login', loginRequest) | ||
.post<LoginResponse>(this.apiUrl + '/login', loginRequest, { | ||
withCredentials: true, | ||
}) | ||
.pipe( | ||
tap((response) => { | ||
this.userData.next(response); | ||
this.isLoggedIn.next(true); | ||
}) | ||
}), | ||
); | ||
} | ||
|
||
getUserData() { | ||
// return this.userData$; | ||
getPermissions() { | ||
return this.http | ||
.get<UserPermissions>(this.apiUrl + '/permissions', { | ||
withCredentials: true, | ||
}) | ||
.pipe( | ||
tap((response) => { | ||
if (response.username == null) this.isLoggedIn.next(false); | ||
else { | ||
this.isLoggedIn.next(true); | ||
this.permissions.next(response); | ||
console.log(response); | ||
} | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { UserService } from './user.service'; | ||
|
||
describe('UserService', () => { | ||
let service: UserService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(UserService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class UserService { | ||
private apiUrl = 'https://localhost:44322/api/User'; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
// getUsers(): Observable<User[]> { | ||
// return this.http.get<User[]>(`${this.apiUrl}`, { withCredentials: true }); | ||
// } | ||
// | ||
// getUserById(id: number): Observable<User> { | ||
// return this.http.get<User>(`${this.apiUrl}/${id}`, { | ||
// withCredentials: true, | ||
// }); | ||
// } | ||
// | ||
// updateUser(id: number, user: User): Observable<User> { | ||
// return this.http.put<User>(`${this.apiUrl}/${id}`, user, { | ||
// withCredentials: true, | ||
// }); | ||
// } | ||
// | ||
// deleteUser(id: number): Observable<void> { | ||
// return this.http.delete<void>(`${this.apiUrl}/${id}`, { | ||
// withCredentials: true, | ||
// }); | ||
// } | ||
} |