I'm new to authentication, and just trying out JWT authentication on a small express app.
I've got a user authentication setup using JWTs, and I'm using the subject as the user's email.
Is this a good practice?
If I decode the JWT on jwt.io, I see:
{
"sub": "[email protected]",
"iat": 1489963760,
"exp": 1490568560
}
Is that how it is supposed to work?
1. Generate a unique code specific to user and save it in your database corresponding to that user. Send the code in the email link sent to user. When the email is clicked, recognise the user based on the code in link and mark it as verified.
The “sub” (subject) claim identifies the principal that is the subject of the JWT. The claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique.
Although JWT does eliminate the database lookup, it introduces security issues and other complexities while doing so. Security is binary—either it's secure or it's not. Thus making it dangerous to use JWT for user sessions.
To authenticate a user, a client application must send a JSON Web Token (JWT) in the authorization header of the HTTP request to your backend API. API Gateway validates the token on behalf of your API, so you don't have to add any code in your API to process the authentication.
The sub
claim must be unique. Since email addresses are unique, it is a reasonable choice for the claim.
See RFC7519
4.1.2. "sub" (Subject) Claim
The "sub" (subject) claim identifies the principal that is the subject of the JWT. The claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific.
Ensure two users do not register theirselves with the same email address, for example using a generic email like [email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With