-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (73 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Using Identibyte in a React.js component
*
* This example shows how you can integrate Identibyte with a signup
* form in your React app. Here's the basic structure:
*
* - SignupForm is the main component
* - When the form is submitted, a request is made to Identibyte
* - If the email is disposable, an error is shown
* - Otherwise the form is submitted
*/
class SignupForm extends React.Component {
/*
* Initialize the component's state with an `error` that is set to
* null. This is only set to true if the user enters a disposable
* email address, in which case we will show them an error.
*/
state = {
error
}
handleFormSubmit = (event) => {
// Prevent normal form submission
event.preventDefault();
// Get the email value from the form
const email = this.refs.email.value
// Add token authorization for the Identibyte request
const encToken = btoa(`${process.env.IDENTIBYTE_TOKEN}:`)
const fetchOptions = { headers: { Authorization: `Basic ${encToken}` } }
// Check if email is disposable, then submit the form or set
// an error to be shown to the user.
return fetch(`https://identibyte.com/check/${email}`, opts)
.then(res => res.json())
.then(({ email }) => {
if (email.disposable === true) {
return this.setState({
error: 'Please do not use a disposable email address provider.'
})
} else {
return this.refs.form.submit()
}
})
}
render() {
return (
<form ref="form" method="POST">
{this.state.error ? (<div>{this.state.error}</div>) : null}
<div>
<label>Email address</label>
<input
ref="email"
name="email"
type="email"
defaultValue=""
/>
</div>
<div>
<label>Password</label>
<input
ref="password"
name="password"
type="password"
defaultValue=""
/>
</div>
<div>
<button type="submit" onClick={this.handleFormSubmit}>
Sign up
</button>
</div>
</form>
)
}
}