コード例 #1
0
ファイル: register.go プロジェクト: ErikBrown/5sur
func ResetPassword(db *sql.DB, email string) error {
	if email == "" {
		return util.NewError(nil, "Correo electrónico requerido", 400)
	}

	username, err := unusedEmail(db, email)
	if err != nil {
		return err
	}

	if username == "" {
		return util.NewError(nil, "Correo electrónico no registrado", 400)
	}

	alphaNum := []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv")
	randValue := ""
	for i := 0; i < 32; i++ {
		num, err := util.RandKey(58)
		if err != nil {
			return err
		}
		randValue = randValue + string(alphaNum[num])
	}
	hashed := sha256.New()
	hashed.Write([]byte(randValue))
	hashedStr := hex.EncodeToString(hashed.Sum(nil))

	err = storePasswordToken(db, email, hashedStr)
	if err != nil {
		return err
	}

	subject := "5sur - Restablecer contraseña"
	text := "<b>" + username + "</b> - Para cambiar tu contraseña haz click en el siguiente link."
	link := "https://5sur.com/passwordChange?t=" + randValue + "&u=" + username
	body := util.EmailTemplate(text, "Cambiar contraseña", link)
	err = util.SendEmail(email, subject, body)

	return nil
}
コード例 #2
0
ファイル: register.go プロジェクト: ErikBrown/5sur
func UserAuth(db *sql.DB, username string, password string, email string) error {
	// Create auth token
	alphaNum := []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv")
	randValue := ""
	for i := 0; i < 32; i++ {
		num, err := util.RandKey(58)
		if err != nil {
			return err
		}
		randValue = randValue + string(alphaNum[num])
	}
	hashed := sha256.New()
	hashed.Write([]byte(randValue))
	hashedStr := hex.EncodeToString(hashed.Sum(nil))

	err := deleteUserAuth(db, email)
	if err != nil {
		return err
	}

	err = createUserAuth(db, username, password, email, hashedStr)
	if err != nil {
		return err
	}

	subject := "Verificación de correo electrónico 5sur"
	text := "Bienvenido a 5sur.com! haz click en el siguiente link para registrarte."
	link := "https://5sur.com/auth/?t=" + randValue
	body := util.EmailTemplate(text, "Regístrate", link)
	err = util.SendEmail(email, subject, body)
	if err != nil {
		return err
	}

	return nil
}
コード例 #3
0
ファイル: alerts.go プロジェクト: ErikBrown/5sur
func emailAlert(db *sql.DB, user int, category string, targetId int) error {
	send, err := emailPref(db, user, category)
	if err != nil {
		return util.NewError(err, "Error de la base de datos", 500)
	}

	toAddress, err := returnUserEmail(db, user)
	if err != nil {
		return util.NewError(err, "Error de la base de datos", 500)
	}

	// If email pref set to not email for that category, return nil and send no email
	if !send {
		return nil
	}

	subject := ""
	text := ""
	buttonText := ""
	buttonLink := ""

	id := strconv.Itoa(targetId)
	switch category {
	case "pending": // The target id is the listing id
		listing, err := ReturnIndividualListing(db, targetId)
		if err != nil {
			return err
		}
		subject = "5sur - Nuevos usuarios pendientes"
		text = `Nuevos usuarios pendientes en el viaje ` + listing.Origin + ` > ` + listing.Destination + `.`
		buttonText = "Ver viaje"
		buttonLink = "https://5sur.com/dashboard/listings?i=" + id
	case "dropped":
		listing, err := ReturnIndividualListing(db, targetId)
		if err != nil {
			return err
		}
		subject = "5sur - Alguien se ha retirado de tu viaje"
		text = `Alguien se ha retirado de tu viaje ` + listing.Origin + ` > ` + listing.Destination + `.`
		buttonText = "Ver viaje"
		buttonLink = "https://5sur.com/dashboard/listings?i=" + id
	case "message": // The target id is the user id
		message, err := returnAlertMessage(db, user, targetId)
		if err != nil {
			return err
		}
		subject = "5sur - Mensaje nuevo"
		text = `Tienes un mensaje nuevo de ` + message.Name + `.`
		buttonText = "Ver mensaje"
		buttonLink = "https://5sur.com/dashboard/messages?i=" + id
	case "accepted":
		listing, err := ReturnIndividualListing(db, targetId)
		if err != nil {
			return err
		}
		subject = "5sur - Has sido aceptado por un viaje"
		text = `Has sido aceptado por viajee ` + listing.Origin + ` > ` + listing.Destination + `.`
		buttonText = "Ver reserva"
		buttonLink = "https://5sur.com/dashboard/reservations?i=" + id
	case "removed":
		listing, err := ReturnIndividualListing(db, targetId)
		if err != nil {
			return err
		}
		subject = "5sur - You have been removed from a ride you were registered for"
		text = `You have been removed from ride ` + listing.Origin + ` > ` + listing.Destination + `.`
		buttonText = "Ver reservas"
		buttonLink = "https://5sur.com/dashboard/reservations"
	case "deleted":
		subject = "5sur - Viaje eliminado"
		text = `Un viaje por lo cual estabas registrado ha sido eliminado por el conductor.`
		buttonText = "Ver reservas"
		buttonLink = "https://5sur.com/dashboard/reservations"
	}

	body := util.EmailTemplate(text, buttonText, buttonLink)

	err = util.SendEmail(toAddress, subject, body)
	if err != nil {
		return err
	}

	return nil
}