[英]GraphQL Authentication with React TypeScript
I have a login page and when the user clicks on the Submit button, I want to check authentication of the user from the data available in the GraphQL API.我有一个登录页面,当用户单击“提交”按钮时,我想从 GraphQL API 中可用的数据中检查用户的身份验证。 I tried to follow this tutorial:我尝试按照本教程进行操作:
https://www.apollographql.com/docs/react/networking/authentication/ https://www.apollographql.com/docs/react/networking/authentication/
On my graphQL playground, I use this mutation after which a token is returned to me.在我的 graphQL 操场上,我使用此更改,然后将令牌返回给我。
mutation{
loginEmail(email: "${this.state.email}",
password: "${this.state.password}")
}`,
However, I can just figure out how to integrate it in my code.但是,我可以弄清楚如何将它集成到我的代码中。 Where exactly should I pass in the username & password?我到底应该在哪里传递用户名和密码? If I call _AuthLink on my button, I get overloading errors.如果我在按钮上调用 _AuthLink,则会出现过载错误。
Here's my code for the login page:这是我的登录页面代码:
export default class LoginPage extends Component <{}, { email: string,password: string, loggedIn: boolean}>{
constructor(props: Readonly<{}>) {
super(props);
this.state = {
email: '',
password: '',
loggedIn: false,
};
}
_httpLink = createHttpLink({
uri: 'https:myapilink/graphql',
});
_AuthLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
_client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
render() {
return (
<Container component="main" maxWidth="xs">
<CssBaseline />
<div style={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center'}}>
<Avatar>
<LockOutlinedIcon />
</Avatar>
<Typography component="h1" variant="h5">
Sign in
</Typography>
<form style={{width: '100%'}} noValidate>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
id="email"
label="Email Address"
name="email"
autoComplete="email"
autoFocus
onChange={e => {
this.setState({email: e.target.value})
}}
/>
<TextField
variant="outlined"
margin="normal"
required
fullWidth
name="password"
label="Password"
type="password"
id="password"
autoComplete="current-password"
onChange={e => {
this.setState({password: e.target.value})
}}
/>
<FormControlLabel
control={<Checkbox value="remember" color="primary" />}
label="Remember me"
/>
<br></br>
<Button className='button-center'
//onClick={this._AuthLink}
>
Submit</Button>
<br></br>
<Grid container>
<Grid item xs>
<Link href="#" variant="body2">
Forgot password?
</Link>
</Grid>
<Grid item>
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
</Link>
</Grid>
</Grid>
</form>
</div>
<Box mt={8}>
<Copyright />
</Box>
</Container>
);
}
}
You need to create a mutation first!您需要先创建一个突变! This will give you mutate
function which you can pass variables and so on.这会给你mutate
函数,你可以传递变量等等。 Take a look at this example;看看这个例子;
const AddTodo = () => {
let input;
return (
<Mutation mutation={ADD_TODO}>
{(addTodo, { data }) => (
<div>
<form
onSubmit={e => {
e.preventDefault();
addTodo({ variables: { type: input.value } });
input.value = '';
}}
>
<input
ref={node => {
input = node;
}}
/>
<button type="submit">Add Todo</button>
</form>
</div>
)}
</Mutation>
);
};
Notice how we pass addTodo({ variables: { type: input.value } });
注意我们如何传递addTodo({ variables: { type: input.value } });
variables to here, you should send username and password, instead of type
.变量到这里,你应该发送用户名和密码,而不是type
。
You can do something like;你可以做类似的事情;
login({variables: {username: this.state.username, password: this.state.password}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.