Is there a way to retrieve url parameters passed on pages of project built on GatsbyJS? I’m trying to implement a password reset function on my page using AWS, but they can only send the parameters through a link sent to the user’s email.
So the flow would be like this :
User triggers Forgot Password -> AWS sends email to user with link -> Link directs to my page with the parameters -> Reset password form automatically populates fields with the parameters passed
Update
Here’s my App.js code :
JavaScript
x
17
17
1
import { Router } from "@reach/router"
2
3
4
const App = () => (
5
<Layout>
6
<Router>
7
<Login path="signin" exact component={Login} />
8
<Resources path="api/resources" exact component={Resources} />
9
<Verify path="verify" exact component={Verify} />
10
<Forgot path="forgot" exact component={Forgot} />
11
<Reset path="account/reset/:code" exact component={Reset}/>
12
</Router>
13
</Layout>
14
)
15
16
export default App;
17
Reset.js :
JavaScript
1
273
273
1
export default class ResetForm extends Component {
2
constructor(props) {
3
super(props);
4
5
this.state = {
6
password: "",
7
confirmPassword: "",
8
vercode: "",
9
email: "",
10
emailValid: false,
11
passValid: false,
12
confirmValid: false,
13
codeValid: false,
14
formValid: true,
15
formErrors : {
16
email: false,
17
password: false,
18
confirmPassword: false,
19
vercode: false,
20
},
21
respErrors : {
22
email: {
23
isValid : true,
24
message : ""
25
},
26
password: {
27
isValid : true,
28
message : ""
29
},
30
code : {
31
isValid : true,
32
message : ""
33
}
34
}
35
36
};
37
38
}
39
40
validateField(field, value) {
41
42
let password = this.state.password
43
let fieldValidationErrors = this.state.formErrors;
44
let emailValid = this.state.emailValid
45
let passValid = this.state.passValid
46
let confirmValid = this.state.confirmValid
47
let codeValid = this.state.vercode
48
let fieldValidationMessages = this.state.respErrors;
49
50
51
switch(field){
52
case 'email' :
53
emailValid = validator.isEmail(value);
54
fieldValidationErrors.email = emailValid ? false : true;
55
fieldValidationMessages.email.isValid = true;
56
fieldValidationMessages.email.message = "Invalid E-Mail Address";
57
break;
58
59
case 'password' :
60
passValid = validator.matches(value, RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*])(?=.{8,})'));
61
62
fieldValidationMessages.password.message = passValid ? '' : undefined;
63
64
if(!validator.matches(value,RegExp('^(?=.*[a-z])(?=.*[A-Z])'))){
65
fieldValidationMessages.password.message = "At least 1 Upper case character is required";
66
}
67
68
if(!validator.matches(value,RegExp('^(?=.*[!@#$%^&*])'))){
69
fieldValidationMessages.password.message = "At least 1 Symbol is required";
70
}
71
72
if(!validator.matches(value,RegExp('^(?=.{8,})'))){
73
fieldValidationMessages.password.message = "Password must have at least 8 characters";
74
}
75
76
77
fieldValidationErrors.password = passValid ? false : true;
78
break;
79
80
case 'confirmPassword' :
81
confirmValid = validator.equals(value, password);
82
fieldValidationErrors.confirmPassword = confirmValid ? false : true;
83
break;
84
85
case 'vercode' :
86
codeValid = !validator.isEmpty(value);
87
fieldValidationErrors.vercode = codeValid ? false : true;
88
break;
89
90
default :
91
92
break
93
}
94
95
this.setState({
96
formErrors: fieldValidationErrors,
97
emailValid: emailValid,
98
passValid: passValid,
99
confirmValid: confirmValid,
100
codeValid: codeValid,
101
}, this.validateForm())
102
103
}
104
105
validateForm(){
106
this.setState({
107
formValid:
108
this.state.emailValid && this.state.confirmValid && this.state.codeValid && this.state.passValid
109
})
110
111
}
112
113
handleChange = event => {
114
const name = event.target.id;
115
const value = event.target.value;
116
this.setState({
117
[name]: value
118
},
119
() => {
120
this.validateField(name, value)
121
}
122
);
123
}
124
125
126
handleSubmit = async (event) => {
127
event.preventDefault()
128
const state = this.state
129
130
await handleReset(state)
131
.then(async (data) => {
132
if(data.isValid){
133
await handleLogin(state)
134
.then(() => {
135
navigate('/')
136
})
137
.catch(err => console.log(err))
138
} else {
139
switch (data.code) {
140
case CODE_RESET.RESET_EXPIRED:
141
data.message = "The verification code you have submitted is already expired."
142
break
143
case CODE_RESET.RESET_MISMATCH:
144
data.message = "The verification code you have submitted is invalid."
145
break
146
147
default:
148
data.message = "Something went wrong."
149
break;
150
}
151
152
this.setState({
153
[state.respErrors.code.isValid] : data.isValid,
154
[state.respErrors.code.message] : data.message
155
})
156
}
157
})
158
.catch(async(err) => {
159
console.log(err)
160
})
161
162
}
163
164
render() {
165
if(isLoggedIn()) {
166
navigate(`/`)
167
}
168
169
return (
170
171
<Row className={[formStyles.formContainer, "row"].join(' ')} >
172
<Col sm={{
173
size:12
174
}}
175
md={{
176
size: 8,
177
offset: 2
178
}}
179
>
180
<Form
181
onSubmit={this.handleSubmit}
182
>
183
<h3 style={{
184
fontWeight: 'bolder'
185
}}>
186
Reset Password
187
</h3>
188
<FormGroup>
189
<Label for="email">Email</Label>
190
<Input
191
id="email"
192
autoFocus
193
type="email"
194
name="email"
195
value={this.state.email.value}
196
onChange={this.handleChange}
197
className={formStyles.signUp}
198
valid={this.state.emailValid}
199
invalid={(this.state.formErrors.email || !this.state.respErrors.email.isValid ) ? true : undefined}
200
/>
201
<FormFeedback invalid={this.state.respErrors.email.isValid ? '' : undefined}>
202
{this.state.respErrors.email.message}
203
</FormFeedback>
204
</FormGroup>
205
206
<FormGroup>
207
<Label for="password">New Password</Label>
208
<Input
209
id="password"
210
type="password"
211
name="password"
212
value={this.state.password.value}
213
onChange={this.handleChange}
214
className={formStyles.signUp}
215
valid={this.state.passValid }
216
invalid={this.state.formErrors.password ? true : undefined}
217
/>
218
<FormText invalid={this.state.respErrors.password.isValid ? '' : undefined}>
219
{this.state.respErrors.password.message}
220
221
</FormText>
222
</FormGroup>
223
<FormGroup>
224
<Label for="confirmPassword">Confirm Password</Label>
225
<Input
226
id="confirmPassword"
227
type="password"
228
name="confirmPassword"
229
value={this.state.confirmPassword.value}
230
onChange={this.handleChange}
231
className={formStyles.signUp}
232
valid={this.state.confirmValid }
233
invalid={this.state.formErrors.confirmPassword ? true : undefined}
234
/>
235
<FormFeedback
236
invalid={this.state.formErrors.confirmPassword ? '' : undefined}
237
>
238
Password does not match
239
</FormFeedback>
240
241
</FormGroup>
242
243
<FormGroup>
244
<Label for="vercode">Verification Code</Label>
245
<Input
246
id="vercode"
247
type="text"
248
name="vercode"
249
maxLength={6}
250
value={this.state.vercode.value}
251
onChange={this.handleChange}
252
className={formStyles.signUp}
253
valid={this.state.codeValid.value }
254
invalid={this.state.formErrors.vercode || !this.state.respErrors.code.isValid ? true : undefined}
255
/>
256
<FormFeedback invalid={this.state.respErrors.code.isValid ? '' : undefined} >
257
{this.state.respErrors.code.message}
258
</FormFeedback>
259
</FormGroup>
260
261
<Button
262
color="primary"
263
disabled={!this.state.formValid}
264
>
265
Submit
266
</Button>
267
</Form>
268
</Col>
269
</Row>
270
)
271
}
272
}
273
Advertisement
Answer
Use URLSearchParams.get()
as described on MDN:
JavaScript
1
15
15
1
// Get the location object that is implicitly passed as props
2
// for every page in the `pages` folder
3
const Index = ({ location }) => {
4
console.log(location); // inspect location for yourself
5
6
// the parameter to return for URL. Example:
7
// https://localhost:8000/?parameter1=firstParam¶meter2=secondParam
8
const params = new URLSearchParams(location.search);
9
const parameter1 = params.get("parameter1");
10
const parameter2 = params.get("parameter2");
11
12
console.log(parameter1); // -> "firstParam"
13
console.log(parameter2); // -> "secondParam"
14
// ...
15
Alternative: package query-string
yarn add query-string
or npm i --save query-string
JavaScript
1
14
14
1
import * as queryString from "query-string";
2
3
// Get the location object that is implicitly passed as props
4
// for every page in the `pages` folder
5
const Index = ({ location }) => {
6
console.log(location); // inspect location for yourself
7
8
// query-string parses the parameters that are contained in the location object
9
const { parameter1, parameter2 } = queryString.parse(location.search);
10
11
console.log(parameter1);
12
console.log(parameter2);
13
// ...
14