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

feat(ui): added logout functionality to epic #17045

Merged
merged 4 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
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}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need some more types in here. try running this in influxdb/ui:
yarn tsc -noImplicitAny | grep -A 3 -i "$(git diff --name-status master | awk '{ print substr($2,4) }')

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()
}, [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

return null
}

export default withRouter<Props>(Logout)
export default ErrorHandling(withRouter<WithRouterProps>(Logout))
15 changes: 14 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,16 @@ export const createAuthorization = async (
throw error
}
}

export const getAuth0Config = async (): Promise<Auth0Config> => {
try {
const {data} = await AJAX({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are you doubling down on using the old request library for new features?

https://giphy.com/embed/AuIvUrZpzBl04

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great question. I did a global search of influxdb for /api/v2private and followed that convention since we aren't actually generating a route for this particular endpoint on IDPE

Copy link
Contributor

@drdelambre drdelambre Feb 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fetch then to keep consistency with oats

method: 'GET',
url: '/api/v2private/oauth/clientConfig',
})
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 @@ -25,17 +25,10 @@ import {GoogleLogo, GithubLogo} from 'src/clientLibraries/graphics'
// 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 @@ -61,6 +54,8 @@ interface State {
}

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

state: State = {
activeTab: 'login',
buttonStatus: ComponentStatus.Default,
Expand All @@ -76,6 +71,16 @@ class LoginPageContents extends PureComponent<DispatchProps> {
confirmPasswordError: '',
}

public async componentDidMount() {
const config = await getAuth0Config()
this.auth0 = auth0js.WebAuth({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dope

domain: config.domain,
clientID: config.clientID,
redirectUri: config.redirectURL,
responseType: 'code',
})
}

render() {
const {
buttonStatus,
Expand Down Expand Up @@ -273,7 +278,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 @@ -289,7 +294,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
return
}

auth0.signup(
this.auth0.signup(
{
connection: Auth0Connection.Authentication,
email,
Expand All @@ -304,7 +309,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 @@ -346,7 +351,7 @@ class LoginPageContents extends PureComponent<DispatchProps> {
}

private handleSocialClick = (connection: Auth0Connection) => {
auth0.authorize({
this.auth0.authorize({
connection,
})
}
Expand All @@ -358,7 +363,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
}