Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to set redirect_uri when logging-in thru GitHub (uses OST_GITHUB_CALLBACK_URL) #162

Merged
merged 5 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rare-onions-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"outstatic": patch
---

Allow to set redirect_uri when logging-in thru GitHub (uses OST_GITHUB_CALLBACK_URL)
9 changes: 8 additions & 1 deletion packages/outstatic/src/app/api/auth/callback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextRequest, NextResponse } from 'next/server'
import { createEdgeRouter } from 'next-connect'
import nextSession from 'next-session'
import { Session } from 'next-session/lib/types'
import { NextRequest, NextResponse } from 'next/server'
import { setLoginSession } from '../../../utils/auth/auth'
import { MAX_AGE } from '../../../utils/auth/auth-cookies'

Expand Down Expand Up @@ -100,6 +100,13 @@ router
}
})
.get(async (req) => {
const error = req?.nextUrl.searchParams?.get('error')

// check for GitHub errors
if (error) {
return NextResponse.json({ error }, { status: 403 })
}

const code = req?.nextUrl.searchParams?.get('code') as string
const access_token = await getAccessToken(code)
req.session.token = access_token
Expand Down
3 changes: 3 additions & 0 deletions packages/outstatic/src/app/api/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default async function GET() {
url.searchParams.append('client_id', process.env.OST_GITHUB_ID ?? '')
url.searchParams.append('scope', scopes.join(','))
url.searchParams.append('response_type', 'code')
if (process.env?.OST_GITHUB_CALLBACK_URL) {
url.searchParams.append('redirect_uri', process.env.OST_GITHUB_CALLBACK_URL)
}

redirect(url.toString())
}
16 changes: 16 additions & 0 deletions packages/outstatic/src/utils/errors/loginErrors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ const loginErrors = {
</a>
.
</>
),
redirect_uri_mismatch: (
<>
The redirect_uri MUST match the registered callback URL for this
application. <br />
<br />
For more information:{' '}
<a
className="underline"
target="_blank"
href="https://docs.github.com/apps/managing-oauth-apps/troubleshooting-authorization-request-errors/#redirect-uri-mismatch"
>
GitHub Apps troubleshooting
</a>
.
</>
)
}

Expand Down
3 changes: 2 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"OST_REPO_OWNER",
"OST_TOKEN_SECRET",
"VERCEL_GIT_REPO_SLUG",
"OPENAI_API_KEY"
"OPENAI_API_KEY",
"OST_GITHUB_CALLBACK_URL"
]
}
}
Expand Down