Skip to content

Commit

Permalink
fix: oauth2 login
Browse files Browse the repository at this point in the history
  • Loading branch information
Benehiko committed Aug 28, 2023
1 parent 2b2bc87 commit af79bf5
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
18 changes: 15 additions & 3 deletions examples/nextjs-spa/src/pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const Login: NextPageWithLayout = () => {
const returnTo = String(router.query.return_to || "")
const flowId = String(router.query.flow || "")

// login_challenge is used for OAuth2 and OpenID Connect logins
// when this NextJS application is used as an OAuth provider for Single Sign on (SSO).
const loginChallenge = String(router.query.login_challenge || "")

// Refresh means we want to refresh the session. This is needed, for example, when we want to update the password
// of a user.
const refresh = Boolean(router.query.refresh)
Expand Down Expand Up @@ -53,13 +57,14 @@ const Login: NextPageWithLayout = () => {
)

const createFlow = useCallback(
(refresh: boolean, aal: string, returnTo: string) =>
(refresh: boolean, aal: string, returnTo: string, loginChallenge?: string) =>
ory
.createBrowserLoginFlow({
refresh: refresh,
// Check for two-factor authentication
aal: aal,
returnTo: returnTo,
loginChallenge: loginChallenge,
})
.then(({ data }) => {
setFlow(data)
Expand All @@ -76,13 +81,13 @@ const Login: NextPageWithLayout = () => {

if (flowId) {
getFlow(flowId).catch(() => {
createFlow(refresh, aal, returnTo)
createFlow(refresh, aal, returnTo, loginChallenge)
})
return
}

// Otherwise we initialize it
createFlow(refresh, aal, returnTo)
createFlow(refresh, aal, returnTo, loginChallenge)
}, [router.isReady])

const submitFlow = (values: UpdateLoginFlowBody) =>
Expand Down Expand Up @@ -119,6 +124,13 @@ const Login: NextPageWithLayout = () => {
includeScripts={true}
// we submit the form data to Ory
onSubmit={({ body }) => submitFlow(body as UpdateLoginFlowBody)}

// OAauth2 and OpenID Connect
{...(flow.oauth2_login_request && {
subtitle: `To authenticate ${flow.oauth2_login_request.client?.client_name ||
flow.oauth2_login_request.client?.client_id
}`
})}
/>
) : (
<div>Loading...</div>
Expand Down
105 changes: 105 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af79bf5

Please sign in to comment.