diff --git a/ui/src/Logout.tsx b/ui/src/Logout.tsx index 0c0bb5fe59f..1605b10cacb 100644 --- a/ui/src/Logout.tsx +++ b/ui/src/Logout.tsx @@ -1,9 +1,11 @@ // Libraries -import {PureComponent} from 'react' +import {FC, useEffect} from 'react' import {withRouter, WithRouterProps} from 'react-router' +import auth0js from 'auth0-js' // APIs import {postSignout} from 'src/client' +import {getAuth0Config} from 'src/authorizations/apis' // Constants import {CLOUD, CLOUD_URL, CLOUD_LOGOUT_PATH} from 'src/shared/constants' @@ -11,19 +13,20 @@ import {CLOUD, CLOUD_URL, CLOUD_LOGOUT_PATH} from 'src/shared/constants' // Components import {ErrorHandling} from 'src/shared/decorators/errors' -type Props = WithRouterProps - -@ErrorHandling -export class Logout extends PureComponent { - public componentDidMount() { - this.handleSignOut() - } - - public render() { - return null - } - - private handleSignOut = async () => { +// Utils +import {isFlagEnabled} from 'src/shared/utils/featureFlag' + +const Logout: FC = ({router}) => { + const handleSignOut = async () => { + if (CLOUD && isFlagEnabled('regionBasedLoginPage')) { + const config = await getAuth0Config() + const auth0 = new auth0js.WebAuth({ + domain: config.domain, + clientID: config.clientID, + }) + auth0.logout({}) + return + } if (CLOUD) { window.location.href = `${CLOUD_URL}${CLOUD_LOGOUT_PATH}` return @@ -34,9 +37,14 @@ export class Logout extends PureComponent { throw new Error(resp.data.message) } - this.props.router.push(`/signin`) + router.push(`/signin`) } } + + useEffect(() => { + handleSignOut() + }, []) + return null } -export default withRouter(Logout) +export default ErrorHandling(withRouter(Logout)) diff --git a/ui/src/authorizations/apis/index.ts b/ui/src/authorizations/apis/index.ts index fff18cf9fd3..3154f43bf6e 100644 --- a/ui/src/authorizations/apis/index.ts +++ b/ui/src/authorizations/apis/index.ts @@ -1,5 +1,5 @@ import AJAX from 'src/utils/ajax' -import {Authorization} from 'src/types' +import {Authorization, Auth0Config} from 'src/types' export const createAuthorization = async ( authorization @@ -17,3 +17,14 @@ export const createAuthorization = async ( throw error } } + +export const getAuth0Config = async (): Promise => { + try { + const response = await fetch('/api/v2private/oauth/clientConfig') + const data = await response.json() + return data + } catch (error) { + console.error(error) + throw error + } +} diff --git a/ui/src/onboarding/containers/LoginPageContents.tsx b/ui/src/onboarding/containers/LoginPageContents.tsx index 2a46b93d5f5..8250d97d92a 100644 --- a/ui/src/onboarding/containers/LoginPageContents.tsx +++ b/ui/src/onboarding/containers/LoginPageContents.tsx @@ -14,7 +14,7 @@ import { Panel, SelectGroup, } from '@influxdata/clockface' -import auth0js from 'auth0-js' +import auth0js, {WebAuth} from 'auth0-js' // Components import {LoginForm} from 'src/onboarding/components/LoginForm' @@ -26,17 +26,10 @@ import {Transition, animated} from 'react-spring/renderprops' // Types import {Auth0Connection, FormFieldValidation} from 'src/types' -// Actions +// APIs & Actions import {notify} from 'src/shared/actions/notifications' import {passwordResetSuccessfully} from 'src/shared/copy/notifications' - -// TODO: these are filler properties that will be populated on IDPE in a later iteration -const auth0 = new auth0js.WebAuth({ - domain: 'www.influxdata.com', - clientID: 'abc123', - redirectUri: 'www.influxdata.com', - responseType: 'code', -}) +import {getAuth0Config} from 'src/authorizations/apis' interface ErrorObject { [key: string]: string | undefined @@ -62,6 +55,8 @@ interface State { } class LoginPageContents extends PureComponent { + private auth0?: typeof WebAuth + state: State = { activeTab: 'login', buttonStatus: ComponentStatus.Default, @@ -77,6 +72,16 @@ class LoginPageContents extends PureComponent { confirmPasswordError: '', } + public async componentDidMount() { + const config = await getAuth0Config() + this.auth0 = auth0js.WebAuth({ + domain: config.domain, + clientID: config.clientID, + redirectUri: config.redirectURL, + responseType: 'code', + }) + } + render() { const { buttonStatus, @@ -324,7 +329,7 @@ class LoginPageContents extends PureComponent { this.setState({buttonStatus: ComponentStatus.Loading}) if (activeTab === 'login') { - auth0.login( + this.auth0.login( { realm: Auth0Connection.Authentication, email, @@ -340,7 +345,7 @@ class LoginPageContents extends PureComponent { return } - auth0.signup( + this.auth0.signup( { connection: Auth0Connection.Authentication, email, @@ -355,7 +360,7 @@ class LoginPageContents extends PureComponent { return } // log the user into Quartz - auth0.login( + this.auth0.login( { realm: Auth0Connection.Authentication, email, @@ -397,7 +402,7 @@ class LoginPageContents extends PureComponent { } private handleSocialClick = (connection: Auth0Connection) => { - auth0.authorize({ + this.auth0.authorize({ connection, }) } @@ -409,7 +414,7 @@ class LoginPageContents extends PureComponent { this.setState({emailError: 'Please enter a valid email address'}) return } - auth0.changePassword( + this.auth0.changePassword( { email, connection: Auth0Connection.Authentication, diff --git a/ui/src/types/auth.ts b/ui/src/types/auth.ts index cb82beab00e..05822ece67b 100644 --- a/ui/src/types/auth.ts +++ b/ui/src/types/auth.ts @@ -5,3 +5,9 @@ export enum Auth0Connection { Github = 'github', Authentication = 'Username-Password-Authentication', } + +export type Auth0Config = { + clientID: string + domain: string + redirectURL: string +}