src/app/auth.service.ts
Properties |
|
Methods |
constructor(http: HttpClient)
|
||||||
Defined in src/app/auth.service.ts:19
|
||||||
Parameters :
|
clearCartItem |
clearCartItem(cartId: string, itemId: string)
|
Defined in src/app/auth.service.ts:100
|
Returns :
Observable<any>
|
clearToken |
clearToken()
|
Defined in src/app/auth.service.ts:112
|
Returns :
void
|
getCartId |
getCartId()
|
Defined in src/app/auth.service.ts:27
|
Returns :
string | null
|
getCartItems |
getCartItems()
|
Defined in src/app/auth.service.ts:90
|
Returns :
Observable<{}>
|
getRestaurants |
getRestaurants()
|
Defined in src/app/auth.service.ts:82
|
Returns :
Observable<any>
|
getToken |
getToken()
|
Defined in src/app/auth.service.ts:78
|
Returns :
string | null
|
getUserId |
getUserId()
|
Defined in src/app/auth.service.ts:45
|
Returns :
string | null
|
getUsers |
getUsers()
|
Defined in src/app/auth.service.ts:86
|
Returns :
Observable<any>
|
isLoggedIn |
isLoggedIn()
|
Defined in src/app/auth.service.ts:50
|
Returns :
boolean
|
login |
login(username: string, password: string, role: string)
|
Defined in src/app/auth.service.ts:55
|
Returns :
Observable<any>
|
logout |
logout()
|
Defined in src/app/auth.service.ts:67
|
Returns :
void
|
setToken | ||||||
setToken(token: string)
|
||||||
Defined in src/app/auth.service.ts:106
|
||||||
Parameters :
Returns :
void
|
setUserId | ||||||
setUserId(userId: string)
|
||||||
Defined in src/app/auth.service.ts:37
|
||||||
Parameters :
Returns :
void
|
subscribeToUserIdChanges |
subscribeToUserIdChanges()
|
Defined in src/app/auth.service.ts:33
|
Returns :
Observable<string | null>
|
updateCart | ||||||
updateCart(cart: Cart)
|
||||||
Defined in src/app/auth.service.ts:94
|
||||||
Parameters :
Returns :
Observable<any>
|
Private _isLoggedIn |
Default value : false
|
Defined in src/app/auth.service.ts:13
|
Private apiUrl |
Type : string
|
Default value : 'http://localhost:3000'
|
Defined in src/app/auth.service.ts:11
|
authChange |
Type : EventEmitter<boolean>
|
Default value : new EventEmitter<boolean>()
|
Defined in src/app/auth.service.ts:19
|
Private token |
Type : string | null
|
Defined in src/app/auth.service.ts:12
|
Private userId |
Type : string | null
|
Defined in src/app/auth.service.ts:14
|
Private userIdSubject |
Type : Subject<string | null>
|
Default value : new Subject<string | null>()
|
Defined in src/app/auth.service.ts:16
|
import { HttpClient } from "@angular/common/http";
import { EventEmitter, Injectable } from "@angular/core";
import { Observable, Subject, tap } from "rxjs";
import { Cart, CartItem } from "./Cart";
import { MenuItem } from "./MenuItem";
@Injectable({
providedIn: 'root'
})
export class AuthService {
private apiUrl = 'http://localhost:3000';
private token: string | null;
private _isLoggedIn = false;
private userId: string | null;
private userIdSubject: Subject<string | null> = new Subject<string | null>();
// authChange: Subject<boolean> = new Subject<boolean>();
authChange: EventEmitter<boolean> = new EventEmitter<boolean>(); // Add EventEmitter
constructor(private http: HttpClient) {
this.token = localStorage.getItem('token');
this._isLoggedIn = this.token !== null;
this.userId = localStorage.getItem('userId');
}
getCartId(): string | null {
return localStorage.getItem('cartId');
}
// Method to subscribe to userId changes
subscribeToUserIdChanges(): Observable<string | null> {
return this.userIdSubject.asObservable();
}
setUserId(userId: string): void {
this.userId = userId;
localStorage.setItem('userId', userId);
}
getUserId(): string | null {
return this.userId;
}
isLoggedIn(): boolean {
// Check if the user ID is set to determine the logged-in state
return !!this.userId;
}
login(username: string, password: string, role: string): Observable<any> {
this.authChange.emit(true); // Emit event when user logs in
return this.http.post<{ token: string, userId: string }>(`${this.apiUrl}/login`, { username, password, role })
.pipe(tap(res => {
this.setToken(res.token);
this.setUserId(res.userId);
this.authChange.next(true);
}));
}
logout(): void {
this.authChange.emit(false); // Emit event when user logs out
this.token = null;
localStorage.removeItem('token');
this._isLoggedIn = false;
localStorage.removeItem('userId');
this.userId = null;
this.authChange.next(false);
}
getToken(): string | null {
return this.token;
}
getRestaurants(): Observable<any> {
return this.http.get(`${this.apiUrl}/restaurants`, { headers: { Authorization: `Bearer ${this.token}` } });
}
getUsers(): Observable<any> {
return this.http.get(`${this.apiUrl}/users`, { headers: { Authorization: `Bearer ${this.token}` } });
}
getCartItems(){
return this.http.get<CartItem[]>(`${this.apiUrl}/cart/${this.userId}`, { headers: { Authorization: `Bearer ${this.token}` } }) as Observable<CartItem[]>;
}
updateCart(cart: Cart): Observable<any> {
return this.http.delete<Cart>(`http://localhost:3000/cart/${cart._id}`);
}
clearCartItem(cartId: string, itemId: string): Observable<any> {
return this.http.delete(`${this.apiUrl}/cart/${cartId}/item/${itemId}`);
}
setToken(token: string) {
this.token = token;
localStorage.setItem('token', token);
this._isLoggedIn = true;
}
clearToken(): void {
this.token = null;
localStorage.removeItem('token');
this._isLoggedIn = false;
}
}