|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { authClient } from '@/app/lib/auth-client'; |
| 4 | +import { Button } from '@comp/ui/button'; |
| 5 | +import { Icons } from '@comp/ui/icons'; |
| 6 | +import { Loader2 } from 'lucide-react'; |
| 7 | +import { useState } from 'react'; |
| 8 | +import { toast } from 'sonner'; |
| 9 | + |
| 10 | +export function MicrosoftSignIn({ |
| 11 | + inviteCode, |
| 12 | + searchParams, |
| 13 | +}: { |
| 14 | + inviteCode?: string; |
| 15 | + searchParams?: URLSearchParams; |
| 16 | +}) { |
| 17 | + const [isLoading, setLoading] = useState(false); |
| 18 | + |
| 19 | + const handleSignIn = async () => { |
| 20 | + setLoading(true); |
| 21 | + |
| 22 | + try { |
| 23 | + // Build the callback URL with search params |
| 24 | + const baseURL = window.location.origin; |
| 25 | + const path = inviteCode ? `/invite/${inviteCode}` : '/'; |
| 26 | + const redirectTo = new URL(path, baseURL); |
| 27 | + |
| 28 | + // Append all search params if they exist |
| 29 | + if (searchParams) { |
| 30 | + searchParams.forEach((value, key) => { |
| 31 | + redirectTo.searchParams.append(key, value); |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + await authClient.signIn.social({ |
| 36 | + provider: 'microsoft', |
| 37 | + callbackURL: redirectTo.toString(), |
| 38 | + }); |
| 39 | + } catch (error) { |
| 40 | + setLoading(false); |
| 41 | + |
| 42 | + console.error('[Microsoft Sign-In] Authentication failed:', { |
| 43 | + error, |
| 44 | + message: error instanceof Error ? error.message : 'Unknown error', |
| 45 | + timestamp: new Date().toISOString(), |
| 46 | + }); |
| 47 | + |
| 48 | + // Show specific error messages based on error type |
| 49 | + if (error instanceof Error) { |
| 50 | + if (error.message.includes('redirect_uri_mismatch')) { |
| 51 | + toast.error('Configuration error', { |
| 52 | + description: 'Redirect URI mismatch. Please contact support.', |
| 53 | + }); |
| 54 | + } else if (error.message.includes('invalid_client')) { |
| 55 | + toast.error('Invalid credentials', { |
| 56 | + description: 'Microsoft OAuth credentials are invalid. Please contact support.', |
| 57 | + }); |
| 58 | + } else if (error.message.includes('account_not_linked')) { |
| 59 | + toast.error('Account linking failed', { |
| 60 | + description: 'Unable to link Microsoft account automatically. Please contact support.', |
| 61 | + }); |
| 62 | + console.warn( |
| 63 | + '[Microsoft Sign-In] account_not_linked error occurred despite auto-linking being enabled. Check account linking configuration.', |
| 64 | + ); |
| 65 | + } else if (error.message.includes('network') || error.message.includes('fetch')) { |
| 66 | + toast.error('Network error', { |
| 67 | + description: 'Please check your internet connection and try again.', |
| 68 | + }); |
| 69 | + } else { |
| 70 | + toast.error('Sign-in failed', { |
| 71 | + description: error.message || 'An unexpected error occurred. Please try again.', |
| 72 | + }); |
| 73 | + } |
| 74 | + } else { |
| 75 | + toast.error('Failed to sign in with Microsoft', { |
| 76 | + description: 'An unexpected error occurred. Please try again.', |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + return ( |
| 83 | + <Button |
| 84 | + onClick={handleSignIn} |
| 85 | + className="w-full h-11 font-medium" |
| 86 | + variant="outline" |
| 87 | + disabled={isLoading} |
| 88 | + > |
| 89 | + {isLoading ? ( |
| 90 | + <Loader2 className="h-4 w-4 animate-spin" /> |
| 91 | + ) : ( |
| 92 | + <> |
| 93 | + <Icons.Microsoft className="h-4 w-4" /> |
| 94 | + Continue with Microsoft |
| 95 | + </> |
| 96 | + )} |
| 97 | + </Button> |
| 98 | + ); |
| 99 | +} |
| 100 | + |
0 commit comments