A context-based hook for displaying GitHub OAuth login popup throughout the application.
src/components/app-login-popup.tsx
AppLoginPopupProvider - Must wrap your app or component tree to use this hook
function useLoginPopup(): {
show: () => void;
closeModal: () => void;
}None - Hook uses React Context
An object containing:
show: Function to display login popupcloseModal: Function to manually close the popup
import { AppLoginPopupProvider } from '@/components/app-login-popup';
function App() {
return (
<AppLoginPopupProvider>
<YourAppContent />
</AppLoginPopupProvider>
);
}import { useLoginPopup } from '@/components/app-login-popup';
function LoginButton() {
const { show } = useLoginPopup();
return (
<button onClick={show}>
Sign In
</button>
);
}function Header() {
const { show } = useLoginPopup();
return (
<header>
<nav>
<button onClick={show}>Login</button>
</nav>
</header>
);
}function ProtectedAction() {
const { show } = useLoginPopup();
const user = useCurrentUser(); // Assume this hook exists
const handleAction = () => {
if (!user) {
show(); // Show login popup if not authenticated
return;
}
// Perform protected action
performAction();
};
return (
<button onClick={handleAction}>
{user ? 'Perform Action' : 'Login to Continue'}
</button>
);
}function CommentForm() {
const { show } = useLoginPopup();
const [comment, setComment] = useState('');
const user = useCurrentUser();
const handleSubmit = (e) => {
e.preventDefault();
if (!user) {
show(); // Prompt login before commenting
return;
}
submitComment(comment);
};
return (
<form onSubmit={handleSubmit}>
<textarea
value={comment}
onChange={(e) => setComment(e.target.value)}
placeholder={user ? "Write a comment..." : "Login to comment"}
/>
<button type="submit">
{user ? 'Post Comment' : 'Login to Comment'}
</button>
</form>
);
}- Context-based: Uses React Context for global state management
- GitHub OAuth: Integrated with GitHub OAuth authentication
- URL preservation: Maintains current URL in
nextparameter for redirect after login - Internationalization: Uses translation hook for localized text
- Accessible: Built with AlertDialog component for accessibility
- Brand integration: Includes Techdiary logo and branding
- TypeScript support: Fully typed with proper interfaces
- Uses
useStatefor dialog open/close state - Uses
useCallbackfor memoized event handlers - Uses
useSearchParamsto preserve redirect URL - Built on top of shadcn/ui AlertDialog component
- Integrates with i18n translation system
- Links to
/api/auth/githubOAuth endpoint
- User clicks login trigger
- Login popup displays with GitHub OAuth option
- User clicks "Login with Github" link
- Redirects to
/api/auth/github?next={currentUrl} - GitHub OAuth handles authentication
- User redirected back to original page after login
- Gated content: Require login to access features
- Comment systems: Login required for commenting
- Bookmarking: Login required to save bookmarks
- User profiles: Access to user-specific features
- Article creation: Login required for content creation
- Reactions: Login required for article reactions
- Social features: Any user-specific functionality
// Example with auth state
function AuthenticatedFeature() {
const { show } = useLoginPopup();
const { user, loading } = useAuth(); // Your auth hook
if (loading) return <div>Loading...</div>;
const handleFeatureClick = () => {
if (!user) {
show();
return;
}
// Execute authenticated feature
executeFeature();
};
return (
<button onClick={handleFeatureClick}>
{user ? 'Use Feature' : 'Login Required'}
</button>
);
}The hook automatically preserves the current URL for post-login redirect:
- Current URL is captured via
useSearchParams - Passed as
nextparameter to OAuth endpoint - User redirected back after successful authentication
- Use for any feature that requires authentication
- Provide clear indication when login is required
- Handle loading states during authentication
- Consider UX flow for post-login actions
- Test with various redirect scenarios
- Ensure proper error handling for OAuth failures
The hook will throw an error if used outside of AppLoginPopupProvider:
Error: useLoginPopup must be used within a ModalProvider
- Uses shadcn/ui AlertDialog components
- Includes Techdiary branding and logo
- GitHub OAuth button with GitHub icon
- Responsive design for mobile and desktop
- Consistent with application theme
- Uses
useTranslationhook for localized text - Supports Bengali and English languages
- Login button text adapts to current language
- Description text is translatable
- OAuth flow handled server-side for security
- No sensitive data stored in client-side state
- Secure redirect handling via
nextparameter - GitHub OAuth provides secure authentication
- Lightweight provider with minimal overhead
- Dialog only rendered when needed
- Callbacks are memoized to prevent function recreation
- Search params only accessed when needed