From 609327a71b2803edde7c68d726596baf2e275a6e Mon Sep 17 00:00:00 2001 From: Palak Bhojani Date: Wed, 9 Jan 2019 11:01:41 -0800 Subject: [PATCH 1/4] Add interval to sign in that checks for profile data --- ui/src/Signin.tsx | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/ui/src/Signin.tsx b/ui/src/Signin.tsx index 3247bbbb6c..8b24657747 100644 --- a/ui/src/Signin.tsx +++ b/ui/src/Signin.tsx @@ -8,6 +8,7 @@ import {trySources} from 'src/onboarding/apis' import {ErrorHandling} from 'src/shared/decorators/errors' import SigninPage from 'src/onboarding/containers/SigninPage' import Notifications from 'src/shared/components/notifications/Notifications' +import {getMe} from 'src/shared/apis/v2/user' // Types import {RemoteDataState} from 'src/types' @@ -21,8 +22,11 @@ interface Props { children: ReactElement } +const FETCH_WAIT = 60000 + @ErrorHandling export class Signin extends PureComponent { + private intervalID: NodeJS.Timer constructor(props: Props) { super(props) @@ -36,6 +40,7 @@ export class Signin extends PureComponent { const isSourcesAllowed = await trySources() const isUserSignedIn = isSourcesAllowed this.setState({loading: RemoteDataState.Done, isUserSignedIn}) + this.intervalID = setInterval(this.checkForLogin, FETCH_WAIT) } public render() { @@ -65,8 +70,18 @@ export class Signin extends PureComponent { } private handleSignInUser = () => { + this.intervalID = setInterval(this.checkForLogin, FETCH_WAIT) this.setState({isUserSignedIn: true}) } + + private checkForLogin = async () => { + try { + await getMe() + } catch (error) { + clearInterval(this.intervalID) + this.setState({isUserSignedIn: false}) + } + } } export default Signin From 2e8a8c5c720b7580400c4f625e56830637aa5b8b Mon Sep 17 00:00:00 2001 From: Palak Bhojani Date: Wed, 9 Jan 2019 11:13:39 -0800 Subject: [PATCH 2/4] Update Signin with router for redirect to login page --- ui/src/Signin.tsx | 12 ++++++++++-- ui/src/index.tsx | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ui/src/Signin.tsx b/ui/src/Signin.tsx index 8b24657747..f3111c9352 100644 --- a/ui/src/Signin.tsx +++ b/ui/src/Signin.tsx @@ -1,5 +1,6 @@ // Libraries import React, {ReactElement, PureComponent} from 'react' +import {withRouter, WithRouterProps} from 'react-router' // APIs import {trySources} from 'src/onboarding/apis' @@ -18,10 +19,12 @@ interface State { isUserSignedIn: boolean } -interface Props { +interface OwnProps { children: ReactElement } +type Props = OwnProps & WithRouterProps + const FETCH_WAIT = 60000 @ErrorHandling @@ -41,6 +44,9 @@ export class Signin extends PureComponent { const isUserSignedIn = isSourcesAllowed this.setState({loading: RemoteDataState.Done, isUserSignedIn}) this.intervalID = setInterval(this.checkForLogin, FETCH_WAIT) + if (!isUserSignedIn) { + this.props.router.push('/signin') + } } public render() { @@ -72,6 +78,7 @@ export class Signin extends PureComponent { private handleSignInUser = () => { this.intervalID = setInterval(this.checkForLogin, FETCH_WAIT) this.setState({isUserSignedIn: true}) + this.props.router.push('/dashboards') } private checkForLogin = async () => { @@ -80,8 +87,9 @@ export class Signin extends PureComponent { } catch (error) { clearInterval(this.intervalID) this.setState({isUserSignedIn: false}) + this.props.router.push('/signin') } } } -export default Signin +export default withRouter(Signin) diff --git a/ui/src/index.tsx b/ui/src/index.tsx index 5c7f69b540..0af1d38ae8 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -19,6 +19,7 @@ import SetActiveSource from 'src/shared/containers/SetActiveSource' import GetOrganizations from 'src/shared/containers/GetOrganizations' import Setup from 'src/Setup' import Signin from 'src/Signin' +import SigninPage from 'src/onboarding/containers/SigninPage' import Logout from 'src/Logout' import TaskPage from 'src/tasks/containers/TaskPage' import TasksPage from 'src/tasks/containers/TasksPage' @@ -94,6 +95,7 @@ class Root extends PureComponent { + Date: Wed, 9 Jan 2019 13:47:47 -0800 Subject: [PATCH 3/4] Add Context so the handler is passed as props in the signinpage component Co-authoerd-by: Delmer Reed --- ui/src/Signin.tsx | 24 ++-- ui/src/index.tsx | 2 +- ui/src/onboarding/components/SigninForm.tsx | 132 ++++++++++++++++++ ui/src/onboarding/containers/SigninPage.tsx | 146 +++----------------- ui/src/utils/auth.tsx | 7 + 5 files changed, 167 insertions(+), 144 deletions(-) create mode 100644 ui/src/onboarding/components/SigninForm.tsx create mode 100644 ui/src/utils/auth.tsx diff --git a/ui/src/Signin.tsx b/ui/src/Signin.tsx index f3111c9352..3fde313ea5 100644 --- a/ui/src/Signin.tsx +++ b/ui/src/Signin.tsx @@ -7,10 +7,11 @@ import {trySources} from 'src/onboarding/apis' // Components import {ErrorHandling} from 'src/shared/decorators/errors' -import SigninPage from 'src/onboarding/containers/SigninPage' -import Notifications from 'src/shared/components/notifications/Notifications' import {getMe} from 'src/shared/apis/v2/user' +// Utils +import {AuthContext} from 'src/utils/auth' + // Types import {RemoteDataState} from 'src/types' @@ -43,28 +44,21 @@ export class Signin extends PureComponent { const isSourcesAllowed = await trySources() const isUserSignedIn = isSourcesAllowed this.setState({loading: RemoteDataState.Done, isUserSignedIn}) - this.intervalID = setInterval(this.checkForLogin, FETCH_WAIT) if (!isUserSignedIn) { this.props.router.push('/signin') } } public render() { - const {isUserSignedIn} = this.state - if (this.isLoading) { return
} - if (!isUserSignedIn) { - return ( -
- - -
- ) - } else { - return this.props.children && React.cloneElement(this.props.children) - } + + return ( + + {this.props.children && React.cloneElement(this.props.children)} + + ) } private get isLoading(): boolean { diff --git a/ui/src/index.tsx b/ui/src/index.tsx index 0af1d38ae8..5a9d9c3085 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -90,12 +90,12 @@ class Root extends PureComponent { component={OnboardingWizardPage} /> + - void + onSignInUser: () => void +} + +interface State { + username: string + password: string +} + +@ErrorHandling +class SigninForm extends PureComponent { + public state: State = { + username: '', + password: '', + } + + public render() { + const {username, password} = this.state + return ( +
+ + + + + + + + + + + + + + +