Skip to content

Commit ba19e66

Browse files
authored
fix(oauth-providers): Fix/1340 (#1364)
* fix(oauth-providers): close #1340 * chore(oauth-providers): run changeset * test(oauth-providers): add test Fixes #1340
1 parent 3f66cd9 commit ba19e66

6 files changed

Lines changed: 25 additions & 0 deletions

File tree

.changeset/bright-parents-fry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hono/oauth-providers': patch
3+
---
4+
5+
handle refersh_token on googleAuth

packages/oauth-providers/mocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ export const dummyToken = {
242242
access_token: '15d42a4d-1948-4de4-ba78-b8a893feaf45',
243243
expires_in: 60000,
244244
scope: 'openid email profile',
245+
refresh_token: '1//04dX-1234567890abcdef-567890abcdef1234',
246+
refresh_token_expires_in: 3600,
245247
}
246248

247249
export const googleUser = {

packages/oauth-providers/src/index.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,13 @@ describe('OAuth Middleware', () => {
107107
const user = c.get('user-google')
108108
const token = c.get('token')
109109
const grantedScopes = c.get('granted-scopes')
110+
const refreshToken = c.get('refresh-token')
110111

111112
return c.json({
112113
user,
113114
token,
114115
grantedScopes,
116+
refreshToken,
115117
})
116118
})
117119

@@ -538,6 +540,7 @@ describe('OAuth Middleware', () => {
538540
const res = await app.request(`/google?code=${dummyCode}`)
539541
const response = (await res.json()) as {
540542
token: Token
543+
refreshToken: Token
541544
user: GoogleUser
542545
grantedScopes: string[]
543546
}
@@ -550,6 +553,10 @@ describe('OAuth Middleware', () => {
550553
token: dummyToken.access_token,
551554
expires_in: dummyToken.expires_in,
552555
})
556+
expect(response.refreshToken).toEqual({
557+
token: dummyToken.refresh_token,
558+
expires_in: dummyToken.refresh_token_expires_in,
559+
})
553560
})
554561
})
555562

packages/oauth-providers/src/providers/google/authFlow.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class AuthFlow {
2323
redirect_uri: string
2424
code: string | undefined
2525
token: Token | undefined
26+
refresh_token: Token | undefined
2627
scope: string[]
2728
state: string | undefined
2829
login_hint: string | undefined
@@ -110,6 +111,13 @@ export class AuthFlow {
110111

111112
this.granted_scopes = response.scope.split(' ')
112113
}
114+
115+
if ('refresh_token' in response) {
116+
this.refresh_token = {
117+
token: response.refresh_token,
118+
expires_in: response.refresh_token_expires_in,
119+
}
120+
}
113121
}
114122

115123
async getUserData(): Promise<void> {

packages/oauth-providers/src/providers/google/googleAuth.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function googleAuth(options: {
6161
c.set('token', auth.token)
6262
c.set('user-google', auth.user)
6363
c.set('granted-scopes', auth.granted_scopes)
64+
c.set('refresh-token', auth.refresh_token)
6465

6566
await next()
6667
}

packages/oauth-providers/src/providers/google/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type GoogleErrorResponse = {
1010
export type GoogleTokenResponse = {
1111
access_token: string
1212
expires_in: number
13+
refresh_token: string
14+
refresh_token_expires_in: number
1315
scope: string
1416
token_type: string
1517
id_token: string

0 commit comments

Comments
 (0)