In my project I need to send letter to user email with confirmation link. My solution:
- Add string column “code” and boolean column “is_active” (with default value false) to user table.
- When user register, generate unique string key and save to database. Send to email link, for example host.com/user/email/{code}/confirm
- Then find by the code (generated string value) user and set flag “is_active” – true.
- Clear value of column “code”.
How are you create confirmation link for email?
Advertisement
Answer
I use similar practice, with the following differences:
- I would make the URL, i.e.
host.com/user/email/{code}/confirm
secure, so that the user must login to verify himself. This ensures a bit more security. For example, if the user had typed a wrong email id while registering, that wrong person shouldn’t be able to verify even after getting the mail. - Instead of searching by
code
, I would thus fetch the user byid
(the id of the currently logged in user). - For the code, I use
UUID.randomUUID().toString()
.
Also, it depends on personal choice, but I don’t use an is_active
flag. Instead, I have a roles
set, in which I put “UNVERIFIED” role. That helps me populating the authorities of the user a bit more easily while using Spring Security. Another way would be just to check if the code
is null
or not.