Skip to content

Commit 3247bf3

Browse files
authored
Merge pull request #77 from privy-open-source/chore/simplify-oauth-redirect-uri-configuration
2 parents f40776a + fbaa8be commit 3247bf3

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
OAUTH_HOST=https://oauth.example.com/
22
OAUTH_CLIENT_ID=Abcde12346
33
OAUTH_CLIENT_SECRET=C1entS3cr3t
4-
OAUTH_REDIRECT_URI=https://localhost:3000/auth/callback
4+
OAUTH_REDIRECT_URI=/auth/callback
55
OAUTH_SCOPE=public read
66
OAUTH_LOGOUT_URI=https://oauth.example.com/logout
77
OAUTH_HOME=/dashboard

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ if (isAlmostExpired(15)) {
6565

6666
This module read enviroment variables directly.
6767

68-
| Env Name | Default | Description |
69-
|--------------------------|---------------|---------------------------------------------------------------------------------------|
70-
| OAUTH_HOST | - | **(Required)** Oauth server's host |
71-
| OAUTH_CLIENT_ID | - | **(Required)** Oauth Client ID |
72-
| OAUTH_CLIENT_SECRET | - | **(Required)** Oauth Client Secret |
73-
| OAUTH_REDIRECT_URI | - | **(Required)** Oauth Callback URI |
74-
| OAUTH_SCOPE | `public read` | Oauth scope |
75-
| OAUTH_LOGOUT_URI | - | Oauth Logout URI |
76-
| OAUTH_HOME | `/` | Redirect path after success login |
77-
| OAUTH_REGISTER | `false` | Add params register to Oauth Server |
78-
| OAUTH_REDIRECT_WHITELIST | - | Redirect path after success login whitelist, for multiple value, use `;` as delimeter |
68+
| Env Name | Default | Description |
69+
|--------------------------|------------------|---------------------------------------------------------------------------------------|
70+
| OAUTH_HOST | - | **(Required)** Oauth server's host |
71+
| OAUTH_CLIENT_ID | - | **(Required)** Oauth Client ID |
72+
| OAUTH_CLIENT_SECRET | - | **(Required)** Oauth Client Secret |
73+
| OAUTH_REDIRECT_URI | `/auth/callback` | Oauth Callback URI |
74+
| OAUTH_SCOPE | `public read` | Oauth scope |
75+
| OAUTH_LOGOUT_URI | - | Oauth Logout URI |
76+
| OAUTH_HOME | `/` | Redirect path after success login |
77+
| OAUTH_REGISTER | `false` | Add params register to Oauth Server |
78+
| OAUTH_REDIRECT_WHITELIST | - | Redirect path after success login whitelist, for multiple value, use `;` as delimeter |
7979

8080
👉 See [.env.example](/.env.example) for example
8181

src/core/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type H3Event, getRequestURL } from 'h3'
12
import { decodePath, parseURL } from 'ufo'
23

34
export function getHomeURL (profile: string, redirect?: string): string {
@@ -24,3 +25,15 @@ export function getHomeURL (profile: string, redirect?: string): string {
2425
export function getEnv (profile: string, name: string): string {
2526
return import.meta.env[`${profile.toUpperCase()}_${name.toUpperCase()}`]
2627
}
28+
29+
export function getRedirectUri (event: H3Event, profile: string): string {
30+
const redirectUrl = getEnv(profile, 'REDIRECT_URI') || '/auth/callback'
31+
const url = parseURL(redirectUrl)
32+
const requestUrl = getRequestURL(event)
33+
34+
const protocol = `${url.protocol ?? requestUrl.protocol}//`
35+
const host = `${url.host ?? requestUrl.host}`
36+
const path = `${url.pathname}`
37+
38+
return `${protocol}${host}${path}`
39+
}

src/runtime/callback.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { CookieSerializeOptions } from 'cookie-es'
1212
import {
1313
getEnv,
1414
getHomeURL,
15+
getRedirectUri,
1516
} from '../core/utils'
1617
import { getClient } from '../core/client'
1718

@@ -29,7 +30,7 @@ export default defineEventHandler(async (event) => {
2930
const homeURL = getHomeURL(profile, state.redirect)
3031
const access = await client.getToken({
3132
code : query.code as string,
32-
redirect_uri: getEnv(profile, 'REDIRECT_URI'),
33+
redirect_uri: getRedirectUri(event, profile),
3334
scope : getEnv(profile, 'SCOPE') || 'public read',
3435
})
3536

src/runtime/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from 'h3'
77
import { withQuery } from 'ufo'
88
import { useRuntimeConfig } from '#imports'
9-
import { getEnv } from '../core/utils'
9+
import { getEnv, getRedirectUri } from '../core/utils'
1010
import { getClient } from '../core/client'
1111

1212
export default defineEventHandler(async (event) => {
@@ -20,7 +20,7 @@ export default defineEventHandler(async (event) => {
2020

2121
const client = getClient(profile)
2222
const authorizeURL = client.authorizeURL({
23-
redirect_uri: getEnv(profile, 'REDIRECT_URI'),
23+
redirect_uri: getRedirectUri(event, profile),
2424
scope : getEnv(profile, 'SCOPE') || 'public read',
2525
state : query ? JSON.stringify(query) : '{}',
2626
})

src/runtime/logout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from 'h3'
88
import { withQuery } from 'ufo'
99
import { useRuntimeConfig } from '#imports'
10-
import { getEnv } from '../core/utils'
10+
import { getRedirectUri, getEnv } from '../core/utils'
1111

1212
export default defineEventHandler(async (event) => {
1313
const config = useRuntimeConfig()
@@ -20,7 +20,7 @@ export default defineEventHandler(async (event) => {
2020
const logoutUrl = withQuery(getEnv(profile, 'LOGOUT_URI'), {
2121
response_type: 'code',
2222
client_id : getEnv(profile, 'CLIENT_ID'),
23-
redirect_uri : getEnv(profile, 'REDIRECT_URI'),
23+
redirect_uri : getRedirectUri(event, profile),
2424
scope : getEnv(profile, 'SCOPE') || 'public read',
2525
state : query ? JSON.stringify(query) : '{}',
2626
})

0 commit comments

Comments
 (0)