mirror of
https://github.com/Nezumi-2711/foodify-frontend.git
synced 2026-09-22 20:01:17 +00:00
Deployment 25.4
- Fix some bug - Config Security
This commit is contained in:
@@ -2,6 +2,7 @@ import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core';
|
||||
import { OrderService } from '../../service/order.service';
|
||||
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
|
||||
import { Router } from '@angular/router';
|
||||
import { UserService } from '../../service/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-footer',
|
||||
@@ -10,8 +11,8 @@ import { Router } from '@angular/router';
|
||||
})
|
||||
export class FooterComponent implements OnInit {
|
||||
//Log-in
|
||||
loggedId: number = Number(localStorage.getItem('user-id'))
|
||||
loggedRole = localStorage.getItem('user-role');
|
||||
token = localStorage.getItem('jwt-token')
|
||||
loggedRole: string;
|
||||
shopId: number;
|
||||
|
||||
//Pagination Properties
|
||||
@@ -31,15 +32,18 @@ export class FooterComponent implements OnInit {
|
||||
@ViewChild('new_order') newOrderTemplate: TemplateRef<any>;
|
||||
|
||||
constructor(
|
||||
private userService: UserService,
|
||||
private orderService: OrderService,
|
||||
private modalService: BsModalService,
|
||||
private router: Router) { }
|
||||
|
||||
ngOnInit() {
|
||||
if (this.loggedRole == 'ROLE_SHOP') {
|
||||
this.shopId = Number(localStorage.getItem('shop-id'))
|
||||
this.countShopOrder();
|
||||
}
|
||||
this.userService.getUserByToken(this.token).subscribe((userInfo) => {
|
||||
if (userInfo.userRole == 'ROLE_SHOP') {
|
||||
this.shopId = userInfo.shopId;
|
||||
this.countShopOrder();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
countShopOrder() {
|
||||
|
||||
@@ -18,9 +18,10 @@ export class HeaderComponent implements OnInit {
|
||||
public user: User;
|
||||
|
||||
//Log-in
|
||||
token: string = localStorage.getItem("jwt-token");
|
||||
isShop: boolean = false;
|
||||
loggedId: number = Number(localStorage.getItem('user-id'))
|
||||
loggedRole = localStorage.getItem('user-role');
|
||||
loggedId: number;
|
||||
loggedRole: string;
|
||||
shopId: number;
|
||||
|
||||
@Output() rightSidebarEvent = new EventEmitter<boolean>();
|
||||
@@ -29,9 +30,7 @@ export class HeaderComponent implements OnInit {
|
||||
public navServices: NavService,
|
||||
private userService: UserService,
|
||||
private firebaseService: FirebaseService) {
|
||||
this.userService.getUserById(Number(localStorage.getItem('user-id'))).subscribe((user) => {
|
||||
this.user = user;
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
collapseSidebar() {
|
||||
@@ -49,10 +48,18 @@ export class HeaderComponent implements OnInit {
|
||||
|
||||
|
||||
ngOnInit() {
|
||||
if (this.loggedRole != 'ROLE_ADMIN') {
|
||||
this.isShop = true;
|
||||
this.shopId = Number(localStorage.getItem('shop-id'))
|
||||
}
|
||||
this.userService.getUserByToken(this.token).subscribe((userInfo) => {
|
||||
this.loggedId = userInfo.userId;
|
||||
this.loggedRole = userInfo.userRole;
|
||||
|
||||
if (this.loggedRole != 'ROLE_ADMIN') {
|
||||
this.isShop = true;
|
||||
this.shopId = userInfo.shopId;
|
||||
}
|
||||
this.userService.getUserById(userInfo.userId).subscribe((user) => {
|
||||
this.user = user;
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
logOut() {
|
||||
|
||||
@@ -15,34 +15,65 @@ export class SidebarComponent {
|
||||
public menuItems: Menu[];
|
||||
public url: any;
|
||||
public fileurl: any;
|
||||
token = localStorage.getItem('jwt-token')
|
||||
user: User;
|
||||
|
||||
constructor(private router: Router, public navServices: NavService, private userService: UserService) {
|
||||
this.userService.getUserById(Number(localStorage.getItem('user-id'))).subscribe((user) => {
|
||||
this.user = user;
|
||||
this.userService.getUserByToken(this.token).subscribe((userInfo) => {
|
||||
this.userService.getUserById(userInfo.userId).subscribe((user) => {
|
||||
this.user = user;
|
||||
})
|
||||
|
||||
if (userInfo.userRole == 'ROLE_ADMIN') {
|
||||
this.navServices.adminItems.subscribe(menuItems => {
|
||||
this.menuItems = menuItems
|
||||
this.router.events.subscribe((event) => {
|
||||
if (event instanceof NavigationEnd) {
|
||||
menuItems.filter(items => {
|
||||
if (items.path === event.url)
|
||||
this.setNavActive(items)
|
||||
if (!items.children) return false
|
||||
items.children.filter(subItems => {
|
||||
if (subItems.path === event.url)
|
||||
this.setNavActive(subItems)
|
||||
if (!subItems.children) return false
|
||||
subItems.children.filter(subSubItems => {
|
||||
if (subSubItems.path === event.url)
|
||||
this.setNavActive(subSubItems)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
else {
|
||||
this.navServices.shopItems.subscribe(menuItems => {
|
||||
this.menuItems = menuItems
|
||||
this.router.events.subscribe((event) => {
|
||||
if (event instanceof NavigationEnd) {
|
||||
menuItems.filter(items => {
|
||||
if (items.path === event.url)
|
||||
this.setNavActive(items)
|
||||
if (!items.children) return false
|
||||
items.children.filter(subItems => {
|
||||
if (subItems.path === event.url)
|
||||
this.setNavActive(subItems)
|
||||
if (!subItems.children) return false
|
||||
subItems.children.filter(subSubItems => {
|
||||
if (subSubItems.path === event.url)
|
||||
this.setNavActive(subSubItems)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
this.navServices.items.subscribe(menuItems => {
|
||||
this.menuItems = menuItems
|
||||
this.router.events.subscribe((event) => {
|
||||
if (event instanceof NavigationEnd) {
|
||||
menuItems.filter(items => {
|
||||
if (items.path === event.url)
|
||||
this.setNavActive(items)
|
||||
if (!items.children) return false
|
||||
items.children.filter(subItems => {
|
||||
if (subItems.path === event.url)
|
||||
this.setNavActive(subItems)
|
||||
if (!subItems.children) return false
|
||||
subItems.children.filter(subSubItems => {
|
||||
if (subSubItems.path === event.url)
|
||||
this.setNavActive(subSubItems)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Active Nave state
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, Router } from '@angular/router';
|
||||
import { FirebaseService } from '../service/firebase.service';
|
||||
import { UserService } from '../service/user.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AdminGuard implements CanActivate {
|
||||
token: string = localStorage.getItem('jwt-token');
|
||||
role: string;
|
||||
|
||||
constructor(
|
||||
private firebaseService: FirebaseService,
|
||||
private userService: UserService,
|
||||
private router: Router
|
||||
) { }
|
||||
) {
|
||||
this.userService.getUserByToken(this.token).subscribe((userInfo) => {
|
||||
this.role = userInfo.userRole;
|
||||
})
|
||||
}
|
||||
|
||||
canActivate(): boolean {
|
||||
if (this.firebaseService.isAdmin()) {
|
||||
if (this.role == 'ROLE_ADMIN') {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -10,6 +10,7 @@ import { environment } from 'src/environments/environment';
|
||||
import { StringBoolObject } from '../tables/string-bool-object';
|
||||
import { Observable } from 'rxjs';
|
||||
import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal';
|
||||
import { UserInfo } from '../tables/user';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -58,21 +59,14 @@ export class FirebaseService {
|
||||
this.userService.getUserByEmailOrPhoneNumber(email).subscribe((user) => {
|
||||
if (user.role.roleName == 'ROLE_ADMIN') {
|
||||
this.loggedIn = true;
|
||||
localStorage.setItem('user-role', user.role.roleName);
|
||||
localStorage.setItem('email', user.email);
|
||||
localStorage.setItem('user-id', user.id.toString());
|
||||
localStorage.setItem('is-logged', JSON.stringify(this.loggedIn));
|
||||
this.router.navigate(['/dashboard/default']);
|
||||
resolve(true); // trả về true nếu đăng nhập thành công
|
||||
} else if (user.role.roleName == 'ROLE_SHOP') {
|
||||
localStorage.setItem('user-role', user.role.roleName);
|
||||
localStorage.setItem('user-email', user.email);
|
||||
localStorage.setItem('user-id', user.id.toString());
|
||||
this.shopService.getShopByUserId(user.id).subscribe((shop) => {
|
||||
if (shop.isEnabled) {
|
||||
this.loggedIn = true;
|
||||
localStorage.setItem('is-logged', JSON.stringify(this.loggedIn))
|
||||
localStorage.setItem('shop-id', shop.id.toString());
|
||||
this.router.navigate(['/dashboard/default']);
|
||||
resolve(true); // trả về true nếu đăng nhập thành công
|
||||
} else {
|
||||
@@ -125,11 +119,14 @@ export class FirebaseService {
|
||||
}
|
||||
}
|
||||
|
||||
isAdmin() {
|
||||
const role = localStorage.getItem('user-role');
|
||||
if (role == 'ROLE_ADMIN') {
|
||||
return true;
|
||||
}
|
||||
isAdmin(): boolean {
|
||||
this.userService.getUserByToken(localStorage.getItem("jwt-token")).subscribe((userInfo) => {
|
||||
const role = userInfo.userRole;
|
||||
if (role == 'ROLE_ADMIN') {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
})
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { HostListener, Inject, Injectable } from '@angular/core';
|
||||
import { BehaviorSubject } from 'rxjs';
|
||||
import { WINDOW } from "./windows.service";
|
||||
import { UserService } from './user.service';
|
||||
|
||||
// Menu
|
||||
export interface Menu {
|
||||
@@ -23,10 +24,13 @@ export class NavService {
|
||||
|
||||
public screenWidth: any
|
||||
public collapseSidebar: boolean = false
|
||||
public roleName: string = localStorage.getItem('user-role');
|
||||
public items: BehaviorSubject<Menu[]>;
|
||||
public token = localStorage.getItem('jwt-token');
|
||||
public roleName: string;
|
||||
public adminItems: BehaviorSubject<Menu[]>;
|
||||
public shopItems: BehaviorSubject<Menu[]>;
|
||||
|
||||
constructor(@Inject(WINDOW) private window) {
|
||||
constructor(@Inject(WINDOW) private window,
|
||||
private userService: UserService) {
|
||||
this.onResize();
|
||||
if (this.screenWidth < 991) {
|
||||
this.collapseSidebar = true
|
||||
@@ -35,12 +39,8 @@ export class NavService {
|
||||
}
|
||||
|
||||
private updateMenuItems() {
|
||||
if (this.roleName == 'ROLE_ADMIN') {
|
||||
this.items = new BehaviorSubject<Menu[]>(this.MENUITEMS);
|
||||
}
|
||||
else {
|
||||
this.items = new BehaviorSubject<Menu[]>(this.SHOP_ITEMS);
|
||||
}
|
||||
this.adminItems = new BehaviorSubject<Menu[]>(this.MENUITEMS);
|
||||
this.shopItems = new BehaviorSubject<Menu[]>(this.SHOP_ITEMS);
|
||||
}
|
||||
|
||||
// Windows width
|
||||
@@ -88,6 +88,7 @@ export class NavService {
|
||||
{
|
||||
title: 'Đơn hàng', icon: 'dollar-sign', type: 'sub', active: false, children: [
|
||||
{ path: '/sales/orders', title: 'Danh sách', type: 'link' },
|
||||
{ path: '/sales/scam-orders', title: 'Bị báo cáo', type: 'link' }
|
||||
// { path: '/sales/transactions', title: 'Giao dịch', type: 'link' },
|
||||
]
|
||||
},
|
||||
@@ -168,6 +169,13 @@ export class NavService {
|
||||
badgeType: 'primary',
|
||||
active: false
|
||||
},
|
||||
{
|
||||
title: 'Đơn hàng', path: '/sales/orders', icon: 'dollar-sign', type: 'link', active: false,
|
||||
// children: [
|
||||
// // { path: '/sales/orders', title: 'Danh sách', type: 'link' },
|
||||
// // { path: '/sales/transactions', title: 'Giao dịch', type: 'link' },
|
||||
// ]
|
||||
},
|
||||
{
|
||||
title: 'Sản phẩm', icon: 'box', type: 'sub', active: false, children: [
|
||||
{ path: '/products/add-product', title: 'Thêm sản phẩm', type: 'link' },
|
||||
@@ -194,12 +202,7 @@ export class NavService {
|
||||
// {path: '/products/digital/digital-add-product', title: 'Add Product', type: 'link'},
|
||||
// ]
|
||||
// },
|
||||
{
|
||||
title: 'Đơn hàng', icon: 'dollar-sign', type: 'sub', active: false, children: [
|
||||
{ path: '/sales/orders', title: 'Danh sách', type: 'link' },
|
||||
{ path: '/sales/transactions', title: 'Giao dịch', type: 'link' },
|
||||
]
|
||||
},
|
||||
|
||||
// {
|
||||
// title: 'Coupons', icon: 'tag', type: 'sub', active: false, children: [
|
||||
// {path: '/coupons/list-coupons', title: 'List Coupons', type: 'link'},
|
||||
|
||||
@@ -4,7 +4,8 @@ import { Observable } from 'rxjs-compat';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { Address } from '../tables/address';
|
||||
import { StringBoolObject } from '../tables/string-bool-object';
|
||||
import { User } from '../tables/user';
|
||||
import { User, UserInfo } from '../tables/user';
|
||||
import { userInfo } from 'os';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -84,4 +85,18 @@ export class UserService {
|
||||
return this.httpClient.get<number>(this.baseUrl + `/count?day=${day}`);
|
||||
}
|
||||
|
||||
//Get User By Token
|
||||
getUserByToken(token: string) {
|
||||
return this.httpClient.get<UserInfo>(this.baseUrl + `/info?token=${token}`);
|
||||
}
|
||||
|
||||
//User Info
|
||||
getUserInfo(): UserInfo {
|
||||
const token = localStorage.getItem("jwt-token");
|
||||
this.httpClient.get<UserInfo>(this.baseUrl + `/info?token=${token}`).subscribe((userInfo) => {
|
||||
console.log(userInfo)
|
||||
return userInfo;
|
||||
})
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,11 @@ export class User {
|
||||
//Response
|
||||
addresses: Address[]
|
||||
role: Role;
|
||||
}
|
||||
|
||||
export class UserInfo {
|
||||
userId: number;
|
||||
shopId: number;
|
||||
userRole: string;
|
||||
userEmail: string;
|
||||
}
|
||||
Reference in New Issue
Block a user