Skip to content

Commit

Permalink
SWD-48: allow guest use app with limited features
Browse files Browse the repository at this point in the history
  • Loading branch information
Nguyễn Thục Ngân authored and thongdanghoang committed Jun 19, 2024
1 parent 9e5ef21 commit 762ac94
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function bootstrap(): Promise<void> {
app.enableCors({
origin: 'http://localhost:5173', // replace with your client's origin
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Content-Type, Accept, Authorization',
allowedHeaders: 'Content-Type, Accept, Authorization, Cache-Control',
credentials: true
});
const config = new DocumentBuilder()
Expand Down
12 changes: 6 additions & 6 deletions backend/src/modules/product/entities/product.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import {
} from 'typeorm';

export enum ProductStatus {
REVIEWING,
PUBLISHED,
EXCHANGING,
EXCHANGED,
BANNED,
REMOVED
REVIEWING = 'REVIEWING',
PUBLISHED = 'PUBLISHED',
EXCHANGING = 'EXCHANGING',
EXCHANGED = 'EXCHANGED',
BANNED = 'BANNED',
REMOVED = 'REMOVED'
}

@Entity('products')
Expand Down
6 changes: 3 additions & 3 deletions backend/src/modules/transactions/entities/ExchangeEntity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
} from 'typeorm';

export enum ExchangeStatus {
PENDING,
ACCEPTED,
REJECTED
PENDING = 'PENDING',
ACCEPTED = 'ACCEPTED',
REJECTED = 'REJECTED'
}

@Entity('exchange')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../../user/notification.entity';

@Controller('exchanges')
@UseGuards(JwtAuthGuard)
export class ExchangeRequestController {
constructor(
private readonly productService: ProductService,
Expand All @@ -24,7 +25,6 @@ export class ExchangeRequestController {
) {}

@Post('/accept/:exchangeId')
@UseGuards(JwtAuthGuard)
async acceptExchangeRequest(
@Param('exchangeId') exchangeId: number
): Promise<ExchangeEntity> {
Expand All @@ -49,7 +49,6 @@ export class ExchangeRequestController {
}

@Post('/reject/:exchangeId')
@UseGuards(JwtAuthGuard)
async rejectExchangeRequest(
@Param('exchangeId') exchangeId: number
): Promise<ExchangeEntity> {
Expand All @@ -74,7 +73,6 @@ export class ExchangeRequestController {
}

@Post('/request')
@UseGuards(JwtAuthGuard)
async create(
@Body() exchangeRequestBody: ExchangeRequestBodyDto
): Promise<ExchangeEntity> {
Expand All @@ -97,10 +95,6 @@ export class ExchangeRequestController {
productExchanged,
exchangeRequest
);
void this.changeStatusOfProductToExchanging(
exchangeRequestBody.productId,
exchangeRequestBody.productToExchangeId
);
return exchangeRequest;
});
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/modules/user/notification.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import {UserEntity} from './user.entity';

export enum NotificationType {
EXCHANGE_REQUEST
EXCHANGE_REQUEST = 'EXCHANGE_REQUEST'
}

@Entity('notifications')
Expand Down
4 changes: 2 additions & 2 deletions backend/src/modules/user/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {PartialType} from '@nestjs/swagger';
import {NotificationEntity} from './notification.entity';

export enum UserStatus {
ACTIVE,
BANNED
ACTIVE = 'ACTIVE',
BANNED = 'BANNED'
}

@Entity('users')
Expand Down
14 changes: 8 additions & 6 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import './index.css';
import '@assets/styles/styles.scss';
import {AuthProviderProps} from 'oidc-react/build/src/AuthContextInterface';
import {AuthProvider} from 'oidc-react';
import React from 'react';

const oidcConfig: AuthProviderProps = {
authority: import.meta.env.VITE_APP_AUTHORITY as string,
clientId: import.meta.env.VITE_APP_CLIENT_ID as string,
responseType: 'code',
redirectUri: import.meta.env.VITE_APP_REDIRECT_URI as string
redirectUri: import.meta.env.VITE_APP_REDIRECT_URI as string,
autoSignIn: false
};

ReactDOM.createRoot(document.getElementById('root')!).render(
// <React.StrictMode>
<AuthProvider {...oidcConfig}>
<App />
</AuthProvider>
// </React.StrictMode>
<React.StrictMode>
<AuthProvider {...oidcConfig}>
<App />
</AuthProvider>
</React.StrictMode>
);
12 changes: 6 additions & 6 deletions frontend/src/modules/homepage/model/productDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export interface ProductDTO {
}

export enum ProductStatus {
REVIEWING,
PUBLISHED,
EXCHANGING,
EXCHANGED,
BANNED,
REMOVED
REVIEWING = 'REVIEWING',
PUBLISHED = 'PUBLISHED',
EXCHANGING = 'EXCHANGING',
EXCHANGED = 'EXCHANGED',
BANNED = 'BANNED',
REMOVED = 'REMOVED'
}

export const getProductStatusDisplay = (
Expand Down
9 changes: 7 additions & 2 deletions frontend/src/modules/shared/components/header/AppHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import {formatDistanceToNow} from 'date-fns';
import {vi} from 'date-fns/locale';
import {ExchangeResponseModal} from '../../../transactions/components/exchange-response-modal/ExchangeResponseModal.tsx';
import {useModal} from '../modal/useModal.tsx';
import {useApplicationService} from '../../services/application.service.ts';

export default function AppHeader({
currentUser
}: {
currentUser: UserDto | null;
}): ReactElement {
const applicationService = useApplicationService();
const navigate = useNavigate();
const [showNotifications, setShowNotifications] = useState(false);
const modalContext = useModal();
Expand Down Expand Up @@ -183,13 +185,16 @@ export default function AppHeader({
</div>
<div
className="user-info d-flex flex-row align-items-center gap-2 btn"
data-bs-toggle="offcanvas"
data-bs-toggle={currentUser ? 'offcanvas' : undefined}
data-bs-target="#popup-profile"
aria-controls="popup-profile"
onClick={() => !currentUser && applicationService.signIn()}
>
<i className="user-avatar fs-5 bi bi-person-circle"></i>
<div className="user-full-name regular-14">
{currentUser?.firstName} {currentUser?.lastName}
{currentUser
? `${currentUser.firstName} ${currentUser.lastName}`
: 'Login'}
</div>
</div>
<ProfileOffCanvas currentUser={currentUser} />
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/modules/shared/models/userDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export interface NotificationDto {
}

export enum NotificationType {
EXCHANGE_REQUEST
EXCHANGE_REQUEST = 'EXCHANGE_REQUEST'
}
24 changes: 13 additions & 11 deletions frontend/src/modules/shared/services/application.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ class ApplicationService {
return response.data;
}

public signIn(): void {
void this.auth?.signIn();
}

public signOutRedirect(): void {
if (this.auth) {
this.auth
.signOutRedirect()
.then(() => {
alert('Successfully logged out');
})
.catch(error => {
alert(error);
});
}
this.auth
?.signOutRedirect()
.then(() => {
alert('Successfully logged out');
})
.catch(error => {
alert(error);
});
}

public isAuthenticated(): boolean {
Expand All @@ -48,12 +50,12 @@ class ApplicationService {
public createApiClient(): AxiosInstance {
const apiClient: AxiosInstance = axios.create();
const accessToken: string | undefined = this.auth?.userData?.access_token;

apiClient.interceptors.request.use(
config => {
if (accessToken) {
config.headers.Authorization = `Bearer ${accessToken}`;
}
config.headers['Cache-Control'] = 'no-cache';
this.startLoading();
return config;
},
Expand Down

0 comments on commit 762ac94

Please sign in to comment.