Assistance is needed in comparing and validating data from client-side to server-side to determine the validity of an email. The process involves the client entering an email, clicking a verification button, which triggers a check on the server side to see if the email exists in the database. If the email exists, the user is deemed valid; otherwise, they are not allowed to proceed further. Issues have been encountered with implementing this functionality using Express and MySQL queries. Testing via Postman consistently returns 'valid'. Sample emails from the MySQL database:
https://i.sstatic.net/hltHZ.png
The JavaScript Express code utilizes the app.post
command but seems to have errors causing incorrect condition checking and statement writing. When tested in Postman, it always returns 'valid', preventing authentication on the client side. Uncertainty persists regarding the conditions that should be implemented due to a lack of familiarity with Express.
app.post('/verifyEmail', (req, res) => {
var email = req.body.email;
let sql = 'SELECT * FROM email WHERE email = ?'; //incorrect condition checking
let query = db.query(sql, (err, result) => {
if (email == null || !email) { //incorrect condition checking
throw err;
res.send('invalid');
}
console.log('valid');
res.send('valid');
})
})
The front-end uses Angular with TypeScript for handling the email verification process.
//service.ts
email: string[];
getEmailAPI(): Observable < any > {
return this.http.get("http://localhost:8000/verifyEmail")
.map((res: Response) => res.json())
.catch((error: any) => Observable.throw(error.json().error || 'Server error'))
}
//component.ts
Email = [];
isVerified: boolean;
getEmailAPI() {
this.QuestionService.getEmailAPI().subscribe(
data => console.log('All email', this.Email = data),
error => console.log('server returns error')
);
}
verifyEmail(formValue) {
this.AppService.getEmailAPI().subscribe(
data => {
if (data) {
// do whatever needed is with returned data
this.isVerified = true;
} else {
this.isVerified = false;
}
},
error => console.log('server returns error')
);
}
<!--component.html-->
<form #emailVerification='ngForm' ngNativeValidate>
<label>Please Enter Your Email Below</label>
<input name="email" type="text" required/>
<button type="submit" (click)="verifyEmail(emailVerification.value)">VERIFY</button>
</form>
Seeking assistance to resolve the issues mentioned above. Additional details can be provided upon request.