Skip to content

Commit

Permalink
account: Fix login error logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kschiffer committed Jul 2, 2023
1 parent 01a3fcc commit 826cba1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/webui/account/views/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const AccountApp = ({ history }) => {
defaultTitle={`${siteTitle ? `${siteTitle} - ` : ''}${siteName}`}
/>
<Routes>
<Route path="/authorize" Component={Authorize} />
<Route path="/authorize/*" Component={Authorize} />
<Route path="*" Component={Boolean(user) ? Landing : Front} />
</Routes>
</React.Fragment>
Expand Down
2 changes: 1 addition & 1 deletion pkg/webui/lib/components/full-view-error/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const FullViewErrorInner = ({ error, safe }) => {
} else if (isOAuthCallback) {
errorTitle = errorMessages.loginFailed
errorMessage = errorMessages.loginFailedDescription
if (isOAuthClientRefusedError(error)) {
if (isOAuthClientRefusedError(error) || error.error === 'access_denied') {
errorMessage = errorMessages.loginFailedAbortDescription
} else if (isOAuthInvalidStateError(error)) {
// Usually in case of state errors, the state has expired or otherwise
Expand Down
13 changes: 10 additions & 3 deletions pkg/webui/lib/components/with-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

import React from 'react'
import { useLocation, Navigate } from 'react-router-dom'
import { useLocation, Navigate, useSearchParams } from 'react-router-dom'
import { defineMessages } from 'react-intl'

import Spinner from '@ttn-lw/components/spinner'
Expand Down Expand Up @@ -47,6 +47,7 @@ const m = defineMessages({
// `Auth` is a component that wraps a tree that requires the user to be authenticated.
const Auth = ({ user, fetching, userError, errorComponent, children, rights, isAdmin }) => {
const location = useLocation()
const [searchParams] = useSearchParams()
if (fetching) {
return (
<Spinner center>
Expand All @@ -56,8 +57,14 @@ const Auth = ({ user, fetching, userError, errorComponent, children, rights, isA
}

let error
const errorParam = searchParams.get('error')
const errorDesriptionParam = searchParams.get('error_description')
const isCallback = location.pathname.endsWith('/oauth/callback')
const hasCallbackError = isCallback && Boolean(errorParam)

if (userError) {
if (hasCallbackError) {
error = { error: errorParam, error_description: errorDesriptionParam }
} else if (userError) {
error = userError
} else if (
// Check whether the user has at least basic rights, without which it
Expand All @@ -82,7 +89,7 @@ const Auth = ({ user, fetching, userError, errorComponent, children, rights, isA

if (error) {
// Redirect to root to prevent side effects.
if (location.pathname !== '/') {
if (!hasCallbackError && location.pathname !== '/') {
return <Navigate to="" replace />
}

Expand Down

0 comments on commit 826cba1

Please sign in to comment.