Reputation: 23
I am new to coding but I have coded the following username and password authentication on my website.
<table>
<form name="login">
<tr><th colspan="2">Login</th></tr>
<tr><td>Username:</td>
<td><input type="text" name="username"/></td></tr>
<tr><td>Password:</td>
<td><input type="password" name="password"/></td></tr>
<td colspan="2"><center><input type="button" onclick="check(this.form)" value="Submit"/>
<input type="reset" value="Reset"/></center></td>
</form>
</table>
<script>
function check(form)
{
if(form.username.value == "user1" && form.password.value == "user1")
{
location="index.html"
}
else
{
alert("Incorrect credentials.")
}
}
</script>
Is it possible to add JWT authentication to this?
Upvotes: 0
Views: 1072
Reputation: 3444
You should NOT DO THAT!
"Hardcoding" credentials in source code is general a bad idea. Everyone who can take a look at the delivered page sources (and that's probably everyone) can take a look at your credentials!
Implementing this kind of check with a token makes no sense, the JWT authentication flow is a lot more complicated than your simple string comparison. First you have to request a token at an authentication service, store it at the client side in some way (e.g. local storage or cookie) and resend it with every (HTTPS) request within the headers. The validity of the token is also limited in time, so even when "hardcoding" would work (which won't), it would not work forever, so the refresh strategy is also something which has to be handled.
Take a look at some JWT flow explanations, maybe here or here. That should make things more clear.
Upvotes: 1