11'use server'
22
33import { env } from '@codebuff/common/env'
4+ import { headers } from 'next/headers'
45
6+ import {
7+ getCliAuthCodeHashPrefix ,
8+ isAuthCodeExpired ,
9+ isCliAuthCodeCandidate ,
10+ parseAuthCode ,
11+ } from '@/app/onboard/_helpers'
512import { BackgroundBeams } from '@/components/background-beams'
613import { HeroGrid } from '@/components/hero-grid'
714import { LoginCard } from '@/components/login/login-card'
@@ -12,17 +19,67 @@ import {
1219 CardDescription ,
1320 CardContent ,
1421} from '@/components/ui/card'
15- import { isAuthCodeExpired , parseAuthCode } from '@/app/onboard/_helpers '
22+ import { logger } from '@/util/logger '
1623
1724export default async function LoginPage ( {
1825 searchParams,
1926} : {
2027 searchParams ?: Promise < { [ key : string ] : string | string [ ] | undefined } >
2128} ) {
2229 const resolvedSearchParams = searchParams ? await searchParams : { }
23- const authCode = resolvedSearchParams ?. auth_code as string | undefined
30+ const rawAuthCode = resolvedSearchParams ?. auth_code
31+ const authCode = Array . isArray ( rawAuthCode ) ? rawAuthCode [ 0 ] : rawAuthCode
32+ const searchParamKeys = Object . keys ( resolvedSearchParams ) . sort ( )
2433
2534 if ( authCode ) {
35+ if ( ! isCliAuthCodeCandidate ( authCode ) ) {
36+ const headerStore = await headers ( )
37+ logger . warn (
38+ {
39+ authCodeLength : authCode . length ,
40+ authCodeTrimmedLength : authCode . trim ( ) . length ,
41+ authCodeHashPrefix : getCliAuthCodeHashPrefix ( authCode ) ,
42+ authCodeParamCount : Array . isArray ( rawAuthCode )
43+ ? rawAuthCode . length
44+ : 1 ,
45+ searchParamKeys,
46+ searchParamCount : searchParamKeys . length ,
47+ hasCallbackUrlParam : searchParamKeys . includes ( 'callbackUrl' ) ,
48+ hasCodeParam : searchParamKeys . includes ( 'code' ) ,
49+ hasRedirectParam : searchParamKeys . includes ( 'redirect' ) ,
50+ dotCount : authCode . match ( / \. / g) ?. length ?? 0 ,
51+ hyphenCount : authCode . match ( / - / g) ?. length ?? 0 ,
52+ requestHost : headerStore . get ( 'host' ) ?? '' ,
53+ forwardedHost : headerStore . get ( 'x-forwarded-host' ) ?? '' ,
54+ forwardedProto : headerStore . get ( 'x-forwarded-proto' ) ?? '' ,
55+ originHeader : headerStore . get ( 'origin' ) ?? '' ,
56+ referer : headerStore . get ( 'referer' ) ?? '' ,
57+ userAgent : headerStore . get ( 'user-agent' ) ?? '' ,
58+ referrerParam :
59+ typeof resolvedSearchParams . referrer === 'string'
60+ ? resolvedSearchParams . referrer
61+ : '' ,
62+ utmSource :
63+ typeof resolvedSearchParams . utm_source === 'string'
64+ ? resolvedSearchParams . utm_source
65+ : '' ,
66+ utmMedium :
67+ typeof resolvedSearchParams . utm_medium === 'string'
68+ ? resolvedSearchParams . utm_medium
69+ : '' ,
70+ utmCampaign :
71+ typeof resolvedSearchParams . utm_campaign === 'string'
72+ ? resolvedSearchParams . utm_campaign
73+ : '' ,
74+ utmContent :
75+ typeof resolvedSearchParams . utm_content === 'string'
76+ ? resolvedSearchParams . utm_content
77+ : '' ,
78+ } ,
79+ 'Freebuff login received non-CLI-shaped auth_code' ,
80+ )
81+ }
82+
2683 const { expiresAt } = parseAuthCode ( authCode )
2784
2885 if ( expiresAt && isAuthCodeExpired ( expiresAt ) ) {
0 commit comments