This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): basic password authentication
fix #1387
- Loading branch information
Showing
13 changed files
with
273 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useEffect, useRef } from 'react' | ||
import PropTypes from 'prop-types' | ||
import { FormattedMessage } from 'react-intl' | ||
import { Box, Flex } from 'rebass/styled-components' | ||
import { Button, CenteredContent, Message, Text } from 'components/UI' | ||
import { Form, PasswordInput } from 'components/Form' | ||
import ArrowRight from 'components/Icon/ArrowRight' | ||
import ZapLogo from 'components/Icon/ZapLogo' | ||
import messages from './messages' | ||
|
||
const Login = ({ login, loginError, clearLoginError, ...rest }) => { | ||
const formApiRef = useRef(null) | ||
|
||
useEffect(() => { | ||
const { current: formApi } = formApiRef | ||
loginError && formApi.setFormError(loginError) | ||
clearLoginError() | ||
}, [loginError, formApiRef, clearLoginError]) | ||
|
||
const handleSubmit = ({ password }) => login(password) | ||
|
||
return ( | ||
<Form getApi={api => (formApiRef.current = api)} onSubmit={handleSubmit} width={1}> | ||
{({ formState: { submits, error } }) => { | ||
const willValidateInline = submits > 0 | ||
return ( | ||
<CenteredContent {...rest} mx="auto" width={11 / 16}> | ||
<Box px={0} py={4} width={1} {...rest}> | ||
<Box mx="auto" width={5 / 8}> | ||
<Flex justifyContent="center" mb={4}> | ||
<ZapLogo height={34} width={34} /> | ||
</Flex> | ||
<Text mb={4} textAlign="center"> | ||
<FormattedMessage {...messages.intro} /> | ||
</Text> | ||
{error && ( | ||
<Flex alignItems="center" flexDirection="column"> | ||
<Message mb={3} variant="error" width="auto"> | ||
{error} | ||
</Message> | ||
</Flex> | ||
)} | ||
<Flex alignItems="flex-start" width={1}> | ||
<PasswordInput | ||
field="password" | ||
hasMessageSpacer | ||
isRequired | ||
validateOnBlur={willValidateInline} | ||
validateOnChange={willValidateInline} | ||
width={1} | ||
/> | ||
<Button ml={2} px={3} type="submit"> | ||
<ArrowRight /> | ||
</Button> | ||
</Flex> | ||
</Box> | ||
</Box> | ||
</CenteredContent> | ||
) | ||
}} | ||
</Form> | ||
) | ||
} | ||
|
||
Login.propTypes = { | ||
clearLoginError: PropTypes.func.isRequired, | ||
login: PropTypes.func.isRequired, | ||
loginError: PropTypes.string, | ||
} | ||
|
||
export default Login |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Login from './Login' | ||
|
||
export default Login |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { defineMessages } from 'react-intl' | ||
|
||
/* eslint-disable max-len */ | ||
export default defineMessages({ | ||
intro: 'Enter your password to continue.', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { connect } from 'react-redux' | ||
import { Redirect } from 'react-router' | ||
import { settingsSelectors } from 'reducers/settings' | ||
import { login, clearLoginError, accountSelectors } from 'reducers/account' | ||
import Login from 'components/Login' | ||
|
||
const WrappedLogin = ({ isPasswordEnabled, isLoggedIn, ...rest }) => { | ||
if (isPasswordEnabled && !isLoggedIn) { | ||
return <Login {...rest} /> | ||
} | ||
return <Redirect to="/init" /> | ||
} | ||
|
||
WrappedLogin.propTypes = { | ||
isLoggedIn: PropTypes.bool, | ||
isPasswordEnabled: PropTypes.bool, | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
isPasswordEnabled: settingsSelectors.currentConfig(state).password.active, | ||
isLoggedIn: accountSelectors.isLoggedIn(state), | ||
loginError: accountSelectors.loginError(state), | ||
}) | ||
|
||
const mapDispatchToProps = { | ||
login, | ||
clearLoginError, | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(WrappedLogin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Login from './Login' | ||
|
||
export default Login |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.