Skip to content

Commit

Permalink
feat(ui): added logout functionality to epic (#17045)
Browse files Browse the repository at this point in the history
refactored the logout component and added in logout functionality behind a feature flag
  • Loading branch information
asalem1 committed Mar 6, 2020
1 parent 5566bfc commit 98cb5e3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 32 deletions.
40 changes: 24 additions & 16 deletions ui/src/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
// 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'

// Components
import {ErrorHandling} from 'src/shared/decorators/errors'

type Props = WithRouterProps

@ErrorHandling
export class Logout extends PureComponent<Props> {
public componentDidMount() {
this.handleSignOut()
}

public render() {
return null
}

private handleSignOut = async () => {
// Utils
import {isFlagEnabled} from 'src/shared/utils/featureFlag'

const Logout: FC<WithRouterProps> = ({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
Expand All @@ -34,9 +37,14 @@ export class Logout extends PureComponent<Props> {
throw new Error(resp.data.message)
}

this.props.router.push(`/signin`)
router.push(`/signin`)
}
}

useEffect(() => {
handleSignOut()
}, [])
return null
}

export default withRouter<Props>(Logout)
export default ErrorHandling(withRouter<WithRouterProps>(Logout))
13 changes: 12 additions & 1 deletion ui/src/authorizations/apis/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,3 +17,14 @@ export const createAuthorization = async (
throw error
}
}

export const getAuth0Config = async (): Promise<Auth0Config> => {
try {
const response = await fetch('/api/v2private/oauth/clientConfig')
const data = await response.json()
return data
} catch (error) {
console.error(error)
throw error
}
}
35 changes: 20 additions & 15 deletions ui/src/onboarding/containers/LoginPageContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand All @@ -62,6 +55,8 @@ interface State {
}

class LoginPageContents extends PureComponent<DispatchProps> {
private auth0?: typeof WebAuth

state: State = {
activeTab: 'login',
buttonStatus: ComponentStatus.Default,
Expand All @@ -77,6 +72,16 @@ class LoginPageContents extends PureComponent<DispatchProps> {
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,
Expand Down Expand Up @@ -324,7 +329,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
this.setState({buttonStatus: ComponentStatus.Loading})

if (activeTab === 'login') {
auth0.login(
this.auth0.login(
{
realm: Auth0Connection.Authentication,
email,
Expand All @@ -340,7 +345,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
return
}

auth0.signup(
this.auth0.signup(
{
connection: Auth0Connection.Authentication,
email,
Expand All @@ -355,7 +360,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
return
}
// log the user into Quartz
auth0.login(
this.auth0.login(
{
realm: Auth0Connection.Authentication,
email,
Expand Down Expand Up @@ -397,7 +402,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
}

private handleSocialClick = (connection: Auth0Connection) => {
auth0.authorize({
this.auth0.authorize({
connection,
})
}
Expand All @@ -409,7 +414,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
this.setState({emailError: 'Please enter a valid email address'})
return
}
auth0.changePassword(
this.auth0.changePassword(
{
email,
connection: Auth0Connection.Authentication,
Expand Down
6 changes: 6 additions & 0 deletions ui/src/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ export enum Auth0Connection {
Github = 'github',
Authentication = 'Username-Password-Authentication',
}

export type Auth0Config = {
clientID: string
domain: string
redirectURL: string
}

0 comments on commit 98cb5e3

Please sign in to comment.