@@ -24,27 +24,29 @@ const shouldMock =
2424 process . env . GITHUB_CLIENT_ID ?. startsWith ( 'MOCK_' ) ||
2525 process . env . NODE_ENV === 'test'
2626
27- type GitHubEmailsResponse = {
28- email : string
29- verified : boolean
30- primary : boolean
31- visibility : string | null
32- } [ ]
27+ const GitHubEmailSchema = z . object ( {
28+ email : z . string ( ) ,
29+ verified : z . boolean ( ) ,
30+ primary : z . boolean ( ) ,
31+ visibility : z . string ( ) . nullable ( ) ,
32+ } )
3333
34- type GitHubUserResponse = {
35- login : string
36- id : string
37- name : string | undefined
38- avatar_url : string | undefined
39- }
34+ const GitHubEmailsResponseSchema = z . array ( GitHubEmailSchema )
35+
36+ const GitHubUserResponseSchema = z . object ( {
37+ login : z . string ( ) ,
38+ id : z . string ( ) ,
39+ name : z . string ( ) . optional ( ) ,
40+ avatar_url : z . string ( ) . optional ( ) ,
41+ } )
4042
4143export class GitHubProvider implements AuthProvider {
4244 getAuthStrategy ( ) {
4345 return new GitHubStrategy (
4446 {
4547 clientId : process . env . GITHUB_CLIENT_ID ,
4648 clientSecret : process . env . GITHUB_CLIENT_SECRET ,
47- redirectURI : 'https://www.epicstack.dev/auth/github/callback' ,
49+ redirectURI : process . env . GITHUB_REDIRECT_URI ,
4850 } ,
4951 async ( { tokens } ) => {
5052 // we need to fetch the user and the emails separately, this is a change in remix-auth-github
@@ -56,7 +58,8 @@ export class GitHubProvider implements AuthProvider {
5658 'X-GitHub-Api-Version' : '2022-11-28' ,
5759 } ,
5860 } )
59- const user = ( await userResponse . json ( ) ) as GitHubUserResponse
61+ const rawUser = await userResponse . json ( )
62+ const user = GitHubUserResponseSchema . parse ( rawUser )
6063
6164 const emailsResponse = await fetch (
6265 'https://api.github.com/user/emails' ,
@@ -68,7 +71,8 @@ export class GitHubProvider implements AuthProvider {
6871 } ,
6972 } ,
7073 )
71- const emails = ( await emailsResponse . json ( ) ) as GitHubEmailsResponse
74+ const rawEmails = await emailsResponse . json ( )
75+ const emails = GitHubEmailsResponseSchema . parse ( rawEmails )
7276 const email = emails . find ( ( e ) => e . primary ) ?. email
7377 if ( ! email ) {
7478 throw new Error ( 'Email not found' )
0 commit comments