| 
 | 1 | +import { SetCookie } from '@mjackson/headers'  | 
1 | 2 | import { createId as cuid } from '@paralleldrive/cuid2'  | 
2 | 3 | import { redirect } from 'react-router'  | 
3 | 4 | import { GitHubStrategy } from 'remix-auth-github'  | 
@@ -28,24 +29,33 @@ export class GitHubProvider implements AuthProvider {  | 
28 | 29 | 	getAuthStrategy() {  | 
29 | 30 | 		return new GitHubStrategy(  | 
30 | 31 | 			{  | 
31 |  | -				clientID: process.env.GITHUB_CLIENT_ID,  | 
 | 32 | +				clientId: process.env.GITHUB_CLIENT_ID,  | 
32 | 33 | 				clientSecret: process.env.GITHUB_CLIENT_SECRET,  | 
33 |  | -				callbackURL: '/auth/github/callback',  | 
 | 34 | +				redirectURI: '/auth/github/callback',  | 
34 | 35 | 			},  | 
35 |  | -			async ({ profile }) => {  | 
36 |  | -				const email = profile.emails[0]?.value.trim().toLowerCase()  | 
 | 36 | +			async ({ tokens }) => {  | 
 | 37 | +				const response = await fetch('https://api.github.com/user', {  | 
 | 38 | +					headers: {  | 
 | 39 | +						Accept: 'application/vnd.github+json',  | 
 | 40 | +						Authorization: `Bearer ${tokens.accessToken()}`,  | 
 | 41 | +						'X-GitHub-Api-Version': '2022-11-28',  | 
 | 42 | +					},  | 
 | 43 | +				})  | 
 | 44 | +				const profile = (await response.json()) as any  | 
 | 45 | +				const email = profile.emails[0]?.trim().toLowerCase()  | 
37 | 46 | 				if (!email) {  | 
38 | 47 | 					throw new Error('Email not found')  | 
39 | 48 | 				}  | 
40 |  | -				const username = profile.displayName  | 
41 |  | -				const imageUrl = profile.photos[0]?.value  | 
42 |  | -				return {  | 
43 |  | -					email,  | 
 | 49 | +				// const username = profile.displayName  | 
 | 50 | +				// const imageUrl = profile.photos[0]?.value  | 
 | 51 | +				const returnValue = {  | 
44 | 52 | 					id: profile.id,  | 
45 |  | -					username,  | 
46 |  | -					name: profile.name.givenName,  | 
47 |  | -					imageUrl,  | 
 | 53 | +					email,  | 
 | 54 | +					// username,  | 
 | 55 | +					// name: profile.name,  | 
 | 56 | +					// imageUrl,  | 
48 | 57 | 				}  | 
 | 58 | +				return returnValue  | 
49 | 59 | 			},  | 
50 | 60 | 		)  | 
51 | 61 | 	}  | 
@@ -85,21 +95,24 @@ export class GitHubProvider implements AuthProvider {  | 
85 | 95 | 	async handleMockAction(request: Request) {  | 
86 | 96 | 		if (!shouldMock) return  | 
87 | 97 | 
 
  | 
88 |  | -		const connectionSession = await connectionSessionStorage.getSession(  | 
89 |  | -			request.headers.get('cookie'),  | 
90 |  | -		)  | 
91 | 98 | 		const state = cuid()  | 
92 |  | -		connectionSession.set('oauth2:state', state)  | 
93 |  | - | 
94 | 99 | 		// allows us to inject a code when running e2e tests,  | 
95 | 100 | 		// but falls back to a pre-defined 🐨 constant  | 
96 | 101 | 		const code =  | 
97 | 102 | 			request.headers.get(MOCK_CODE_GITHUB_HEADER) || MOCK_CODE_GITHUB  | 
98 | 103 | 		const searchParams = new URLSearchParams({ code, state })  | 
 | 104 | +		let cookie = new SetCookie({  | 
 | 105 | +			name: 'github',  | 
 | 106 | +			value: searchParams.toString(),  | 
 | 107 | +			path: '/',  | 
 | 108 | +			sameSite: 'Lax',  | 
 | 109 | +			httpOnly: true,  | 
 | 110 | +			maxAge: 60 * 10,  | 
 | 111 | +			secure: process.env.NODE_ENV === 'production' || undefined,  | 
 | 112 | +		})  | 
99 | 113 | 		throw redirect(`/auth/github/callback?${searchParams}`, {  | 
100 | 114 | 			headers: {  | 
101 |  | -				'set-cookie':  | 
102 |  | -					await connectionSessionStorage.commitSession(connectionSession),  | 
 | 115 | +				'Set-Cookie': cookie.toString(),  | 
103 | 116 | 			},  | 
104 | 117 | 		})  | 
105 | 118 | 	}  | 
 | 
0 commit comments