vendredi 22 janvier 2016

How do I generate a unique token (for e-mail verification)?

I want to implement a system that after user signs up, user will receive an email includes a link to verify this email is for that user.

The way I generate the token for verifying the email is like this:

import (
    "crypto/rand"
    "encoding/base64"
)

func generateToken() (string, error) {
    b := make([]byte, 35)
    _, err := rand.Read(b)
    if err != nil {
        return "", err
    }
    return base64.URLEncoding.EncodeToString(b), nil
}

But what I want to ask is if this method is OK? How to make all the token generated by this method is unique ?

What is the normal way to implement this system ?

Please give me some ideas and tell me if this method of generating token is good.

Thanks.




Aucun commentaire:

Enregistrer un commentaire