Skip to content

Commit 92546b5

Browse files
committed
refactor(github package): update PopupWindow dimensions and enhance useGitHubLogin options handling
- Adjusted PopupWindow dimensions for better user experience. - Refactored useGitHubLogin to extract individual options and utilize refs for callbacks.
1 parent f8ea800 commit 92546b5

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

packages/@react-oauth/github/src/PopupWindow.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ export function openPopupWindow(
7171
let popupWindow: Window | null = null;
7272
let pollingIntervalId: number | null = null;
7373

74-
// Normalize options with defaults
74+
// Normalize options with defaults and center the popup
7575
const normalizedOptions: Required<PopupWindowOptions> = {
76-
height: 1000,
77-
width: 600,
78-
left: window.screen.width / 2 - 300,
79-
top: window.screen.height / 2 - 500,
76+
height: 700,
77+
width: 500,
78+
left: window.screen.width / 2 - 250,
79+
top: window.screen.height / 2 - 350,
8080
...options,
8181
};
8282

packages/@react-oauth/github/src/useGitHubLogin.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,39 @@ export function useGitHubLogin(
9393
const popupRef = useRef<PopupWindowPromise | null>(null);
9494
const [isLoading, setIsLoading] = useState(false);
9595

96+
// Extract individual options to avoid dependency on the whole options object
97+
const {
98+
clientId,
99+
redirectUri = '',
100+
scope = 'user:email',
101+
popupOptions = {},
102+
allowSignup = true,
103+
onSuccess,
104+
onError,
105+
onRequest,
106+
} = options;
107+
108+
// Use refs for callbacks to avoid requiring users to memoize them
109+
const onSuccessRef = useRef(onSuccess);
110+
onSuccessRef.current = onSuccess;
111+
112+
const onErrorRef = useRef(onError);
113+
onErrorRef.current = onError;
114+
115+
const onRequestRef = useRef(onRequest);
116+
onRequestRef.current = onRequest;
117+
96118
// Generate state if not provided (for CSRF protection)
97119
const state = useMemo(
98120
() => options.state || generateState(),
99121
[options.state],
100122
);
101123

102124
const initiateGitHubLogin = useCallback(() => {
103-
const {
104-
clientId,
105-
redirectUri = '',
106-
scope = 'user:email',
107-
popupOptions = {},
108-
allowSignup = true,
109-
onSuccess,
110-
onError,
111-
onRequest,
112-
} = options;
113-
114125
// Validate required props
115126
if (!clientId) {
116127
const error = new Error('clientId is required');
117-
onError(error);
128+
onErrorRef.current(error);
118129
return;
119130
}
120131

@@ -138,7 +149,7 @@ export function useGitHubLogin(
138149

139150
try {
140151
// Call onRequest callback if provided
141-
onRequest?.();
152+
onRequestRef.current?.();
142153

143154
// Open popup window
144155
const popup = openPopupWindow(
@@ -156,25 +167,37 @@ export function useGitHubLogin(
156167

157168
// Verify state matches (CSRF protection)
158169
if (data.state && data.state !== state) {
159-
onError(OAuthError.stateMismatch());
170+
onErrorRef.current(OAuthError.stateMismatch());
160171
return;
161172
}
162173

163174
if (!data.code) {
164-
onError(OAuthError.missingCode());
175+
onErrorRef.current(OAuthError.missingCode());
165176
return;
166177
}
167-
onSuccess(data);
178+
onSuccessRef.current(data);
168179
})
169180
.catch((error: Error) => {
170181
setIsLoading(false);
171-
onError(error instanceof Error ? error : new Error(String(error)));
182+
onErrorRef.current(
183+
error instanceof Error ? error : new Error(String(error)),
184+
);
172185
});
173186
} catch (error) {
174187
setIsLoading(false);
175-
onError(error instanceof Error ? error : new Error(String(error)));
188+
onErrorRef.current(
189+
error instanceof Error ? error : new Error(String(error)),
190+
);
176191
}
177-
}, [options, state, isLoading]);
192+
}, [
193+
clientId,
194+
redirectUri,
195+
scope,
196+
popupOptions,
197+
allowSignup,
198+
state,
199+
isLoading,
200+
]);
178201

179202
return {
180203
initiateGitHubLogin,

0 commit comments

Comments
 (0)