Files
pos-system/apps/web-client/src/lib/auth/oauth.ts
Ho Ngoc Hai c088de53c3 Update dependencies and enhance Tailwind CSS configuration for web applications
- Added new dependencies including clsx, lucide-react, recharts, and various Radix UI components to improve UI functionality.
- Upgraded Tailwind CSS to version 4.0.0 and updated configuration to utilize CSS variables for theming and responsive design.
- Introduced global styles and improved accessibility features in the layout and components.
- Removed outdated login page and refactored authentication store for better state management.
- Enhanced API service with additional authentication methods and improved error handling.

These changes aim to modernize the web applications and improve user experience through better design and functionality.
2026-01-02 09:41:40 +07:00

114 lines
3.4 KiB
TypeScript

/**
* EN: OAuth utility functions for social authentication
* VI: Các hàm tiện ích OAuth cho xác thực mạng xã hội
*/
/**
* EN: Get API base URL from environment
* VI: Lấy API base URL từ environment
*/
const getApiBaseUrl = (): string => {
return process.env.NEXT_PUBLIC_API_URL || 'http://localhost/api/v1';
};
/**
* EN: OAuth provider types
* VI: Các loại provider OAuth
*/
export type OAuthProvider = 'google' | 'facebook' | 'github';
/**
* EN: Initiate OAuth flow by redirecting to backend OAuth endpoint
* VI: Bắt đầu luồng OAuth bằng cách chuyển hướng đến endpoint OAuth của backend
*
* @param provider - OAuth provider (google, facebook, github)
* @param redirectUrl - Optional redirect URL after successful authentication
*/
export const initiateOAuth = (provider: OAuthProvider, redirectUrl?: string): void => {
// EN: Store redirect URL in sessionStorage for use after callback
// VI: Lưu redirect URL vào sessionStorage để sử dụng sau callback
if (redirectUrl && typeof window !== 'undefined') {
sessionStorage.setItem('oauth_redirect', redirectUrl);
}
// EN: Build OAuth initiation URL
// VI: Xây dựng URL bắt đầu OAuth
const apiBaseUrl = getApiBaseUrl();
const oauthUrl = `${apiBaseUrl}/auth/social/${provider}`;
// EN: Redirect to backend OAuth endpoint
// VI: Chuyển hướng đến endpoint OAuth của backend
if (typeof window !== 'undefined') {
window.location.href = oauthUrl;
}
};
/**
* EN: Handle OAuth callback by extracting token from URL
* VI: Xử lý OAuth callback bằng cách trích xuất token từ URL
*
* @param token - Access token from OAuth callback
* @returns Access token string
*/
export const handleOAuthCallback = (token: string): string => {
return token;
};
/**
* EN: Handle OAuth error by extracting error message from URL
* VI: Xử lý lỗi OAuth bằng cách trích xuất thông báo lỗi từ URL
*
* @param errorMessage - Error message from OAuth callback
* @returns Error message string
*/
export const handleOAuthError = (errorMessage: string): string => {
return decodeURIComponent(errorMessage);
};
/**
* EN: Get stored redirect URL from sessionStorage
* VI: Lấy redirect URL đã lưu từ sessionStorage
*
* @returns Redirect URL or null
*/
export const getStoredRedirectUrl = (): string | null => {
if (typeof window === 'undefined') {
return null;
}
const redirectUrl = sessionStorage.getItem('oauth_redirect');
if (redirectUrl) {
sessionStorage.removeItem('oauth_redirect');
}
return redirectUrl;
};
/**
* EN: Google OAuth initiation helper
* VI: Helper để bắt đầu OAuth Google
*
* @param redirectUrl - Optional redirect URL after successful authentication
*/
export const signInWithGoogle = (redirectUrl?: string): void => {
initiateOAuth('google', redirectUrl);
};
/**
* EN: Facebook OAuth initiation helper
* VI: Helper để bắt đầu OAuth Facebook
*
* @param redirectUrl - Optional redirect URL after successful authentication
*/
export const signInWithFacebook = (redirectUrl?: string): void => {
initiateOAuth('facebook', redirectUrl);
};
/**
* EN: GitHub OAuth initiation helper
* VI: Helper để bắt đầu OAuth GitHub
*
* @param redirectUrl - Optional redirect URL after successful authentication
*/
export const signInWithGitHub = (redirectUrl?: string): void => {
initiateOAuth('github', redirectUrl);
};