@@ -5,40 +5,44 @@ import { findOrCreateUser } from "~/models/user.server";
55import type { AuthUser } from "./authUser" ;
66import { postAuthentication } from "./postAuth.server" ;
77
8- const gitHubStrategy = new GitHubStrategy (
9- {
10- clientID : env . GITHUB_CLIENT_ID ?? "" ,
11- clientSecret : env . GITHUB_SECRET ?? "" ,
12- callbackURL : `${ env . LOGIN_ORIGIN } /auth/github/callback` ,
13- } ,
14- async ( { accessToken, extraParams, profile } ) => {
15- const emails = profile . emails ;
8+ export function addGitHubStrategy (
9+ authenticator : Authenticator < AuthUser > ,
10+ clientID : string ,
11+ clientSecret : string
12+ ) {
13+ const gitHubStrategy = new GitHubStrategy (
14+ {
15+ clientID,
16+ clientSecret,
17+ callbackURL : `${ env . LOGIN_ORIGIN } /auth/github/callback` ,
18+ } ,
19+ async ( { accessToken, extraParams, profile } ) => {
20+ const emails = profile . emails ;
1621
17- if ( ! emails ) {
18- throw new Error ( "GitHub login requires an email address" ) ;
19- }
22+ if ( ! emails ) {
23+ throw new Error ( "GitHub login requires an email address" ) ;
24+ }
2025
21- try {
22- const { user, isNewUser } = await findOrCreateUser ( {
23- email : emails [ 0 ] . value ,
24- authenticationMethod : "GITHUB" ,
25- accessToken,
26- authenticationProfile : profile ,
27- authenticationExtraParams : extraParams ,
28- } ) ;
26+ try {
27+ const { user, isNewUser } = await findOrCreateUser ( {
28+ email : emails [ 0 ] . value ,
29+ authenticationMethod : "GITHUB" ,
30+ accessToken,
31+ authenticationProfile : profile ,
32+ authenticationExtraParams : extraParams ,
33+ } ) ;
2934
30- await postAuthentication ( { user, isNewUser, loginMethod : "GITHUB" } ) ;
35+ await postAuthentication ( { user, isNewUser, loginMethod : "GITHUB" } ) ;
3136
32- return {
33- userId : user . id ,
34- } ;
35- } catch ( error ) {
36- console . error ( error ) ;
37- throw error ;
37+ return {
38+ userId : user . id ,
39+ } ;
40+ } catch ( error ) {
41+ console . error ( error ) ;
42+ throw error ;
43+ }
3844 }
39- }
40- ) ;
45+ ) ;
4146
42- export function addGitHubStrategy ( authenticator : Authenticator < AuthUser > ) {
4347 authenticator . use ( gitHubStrategy ) ;
4448}
0 commit comments