reactjs - 通过 url 传递的 React/Firebase 身份验证变量
问题描述
我正在使用 Firebase 作为身份验证/数据库构建一个带有 React 的 web 应用程序,我注意到我的浏览器历史记录有一个包含纯文本 ( http://localhost:3000/login?email=big%40me.com&password=bigger
) 的用户名和密码的 URL。这是我在将字符串存储为状态然后将它们传递给 Firebase 身份验证函数时犯的错误,还是 Firebase 身份验证过程中的缺陷?有没有办法减轻这种情况?
代码:
import React from 'react';
import Input from '@material-ui/core/Input';
import Button from '@material-ui/core/Button';
import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
import {auth} from './firebase';
import logo from '../images/signature.jpg';
const styles = {
card: {
minWidth: 275,
maxWidth: 350,
},
custForm: {
clear: 'both',
},
logo: {
align: 'center',
height: 200,
width: 'auto',
margin: -30,
paddingRight: 28
},
};
class Auth extends React.Component {
constructor(props){
super(props);
this.state = {
email: '',
pass: '',
err: false
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleChange(e){
const name = e.target.name;
const value = e.target.value;
this.setState({
[name]: value
});
}
handleSubmit(e){
e.preventDefault();
auth.signInWithEmailAndPassword(this.state.email,this.state.pass)
.then(result => {
this.props.onLogin(result);
this.props.history.push('/home');
}).catch(e => {
console.log(e);
this.setState({
err: true,
email: '',
pass: ''
})}
);
}
render(){
const {classes} = this.props;
return (
<div align='center' style={{marginTop: 20}}>
<Grid
container
direction='column'
spacing={8}
justify='center'
>
<img src={logo} className={classes.logo} />
<Typography variant='title' style={{marginBottom: 20}} >Subcontractor Reporting Portal</Typography>
<Grid item className={classes.card} >
<form
onSubmit={this.handleSubmit}
className={classes.custForm}
>
<Input
onChange={this.handleChange}
fullWidth
autoFocus
type='text'
name='email'
placeholder='email'
value={this.state.email}
/>
<Input
onChange={this.handleChange}
fullWidth
name='pass'
type='password'
placeholder='password'
value={this.state.pass}
/>
<Button type='submit'>Login</Button>
</form>
{this.state.err && <p>A wrong email or password was entered</p>}
</Grid>
</Grid>
</div>
);
}
}
export default withStyles(styles)(Auth);
解决方案
您可以使用以下链接进行 Firebase 身份验证,而无需在 URL 中发送用户名和密码。
https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-email-password
端点
https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=[API_KEY]
API_KEY 是您在 firebase 中的项目的 API_KEY。所有详细信息都在该链接中。这将是通过 firebase 实现身份验证的正确方法,因为它使用令牌,并且这些令牌可以存储在您的本地存储中,即使在应用刷新后也可以让您保持登录状态。
推荐阅读
- c# - 如何在 C# Google Analytics Real Time Reporting API 中使用维度?
- python - 如何在熊猫数据框的列中搜索单词(在列表中)?
- java - Lucidworks Fusion 4.1 使用 Javascript 查询管道转换结果文档
- sed - sed 不会用特殊字符替换字符串
- date - Bi 每月日期,如何?
- python - 向 Flask 发出 POST 请求并取回数据
- swift - macOS 远程推送通知不显示警报横幅
- django-tinymce - 配置 TinyMCE 标记使用(强-> B 等)(Django-tinymce)
- mysql - 编写删除前触发器以仅根据属性计数进行删除
- java - 无法自动选择 JOption 窗格 showInputDialog