@@ -24,27 +24,29 @@ const shouldMock =
24
24
process . env . GITHUB_CLIENT_ID ?. startsWith ( 'MOCK_' ) ||
25
25
process . env . NODE_ENV === 'test'
26
26
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
+ } )
33
33
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
+ } )
40
42
41
43
export class GitHubProvider implements AuthProvider {
42
44
getAuthStrategy ( ) {
43
45
return new GitHubStrategy (
44
46
{
45
47
clientId : process . env . GITHUB_CLIENT_ID ,
46
48
clientSecret : process . env . GITHUB_CLIENT_SECRET ,
47
- redirectURI : 'https://www.epicstack.dev/auth/github/callback' ,
49
+ redirectURI : process . env . GITHUB_REDIRECT_URI ,
48
50
} ,
49
51
async ( { tokens } ) => {
50
52
// 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 {
56
58
'X-GitHub-Api-Version' : '2022-11-28' ,
57
59
} ,
58
60
} )
59
- const user = ( await userResponse . json ( ) ) as GitHubUserResponse
61
+ const rawUser = await userResponse . json ( )
62
+ const user = GitHubUserResponseSchema . parse ( rawUser )
60
63
61
64
const emailsResponse = await fetch (
62
65
'https://api.github.com/user/emails' ,
@@ -68,7 +71,8 @@ export class GitHubProvider implements AuthProvider {
68
71
} ,
69
72
} ,
70
73
)
71
- const emails = ( await emailsResponse . json ( ) ) as GitHubEmailsResponse
74
+ const rawEmails = await emailsResponse . json ( )
75
+ const emails = GitHubEmailsResponseSchema . parse ( rawEmails )
72
76
const email = emails . find ( ( e ) => e . primary ) ?. email
73
77
if ( ! email ) {
74
78
throw new Error ( 'Email not found' )
0 commit comments