Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package models
- import (
- "fmt"
- "reflect"
- "regexp"
- "gopkg.in/mgo.v2/bson"
- "golang.org/x/crypto/bcrypt"
- "gopkg.in/mgo.v2"
- )
- // URL is the url to the mongodb
- var URL = "localhost:27017"
- //LoginInfo is the body of the login post method
- type LoginInfo struct {
- Username string `json:"username"`
- Password string `json:"password"`
- }
- //RegisterReq is the body of the register post method
- type RegisterReq struct {
- Username string `json:"username" bson:"username"`
- Password string `json:"password" bson:"password"`
- Email string `json:"email" bson:"email"`
- }
- //BasicRes is the basic response used for many of the api calls
- type BasicRes struct {
- Success bool `json:"success"`
- Message string `json:"message"`
- }
- //UserData is the model of the user in the mongodb
- type UserData struct {
- ID bson.ObjectId `bson:"_id"`
- Username string `bson:"username"`
- Password string `bson:"password"`
- Email string `bson:"email"`
- }
- var passwordReg = regexp.MustCompile("^[a-z0-9]{8,35}$")
- var emailReg = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
- var usernameReg = regexp.MustCompile("^\\w.{3,16}$")
- //ValidBody checks if the boddy is valid
- func (r *RegisterReq) ValidBody() error {
- req := *r
- if req.Username == "" {
- return fmt.Errorf("no username specified")
- }
- if req.Password == "" {
- return fmt.Errorf("no password specified")
- }
- if req.Email == "" {
- return fmt.Errorf("no email specified")
- }
- if !usernameReg.Match([]byte(req.Username)) {
- return fmt.Errorf("username: %v invalid username", req.Username)
- }
- if !emailReg.Match([]byte(req.Email)) {
- return fmt.Errorf("email: %v is invalid", req.Email)
- }
- if !passwordReg.Match([]byte(req.Password)) {
- return fmt.Errorf("password: %v is invalid", req.Password)
- }
- return nil
- }
- //HashPass replaces the pass with a hash
- func (r *RegisterReq) HashPass() {
- // fmt.Println(r, *r, r.Password)
- hash, _ := bcrypt.GenerateFromPassword([]byte(r.Password), 10)
- r.Password = string(hash)
- }
- //SaveToDB saves the body to the db if it doesn't exist already
- func (r RegisterReq) SaveToDB() error {
- exist := make(chan error, 2)
- type Username struct {
- Username string `bson:"username"`
- }
- type Email struct {
- Email string `bson:"email"`
- }
- session, err := mgo.Dial(URL)
- if err != nil {
- return err
- }
- defer session.Close()
- go exists(Username{Username: r.Username}, exist, session)
- go exists(Email{Email: r.Email}, exist, session)
- if err := <-exist; err != nil {
- return err
- }
- if err := <-exist; err != nil {
- return err
- }
- if err := session.DB("dobrich-uslugi").C("users").Insert(r); err != nil {
- return err
- }
- return nil
- }
- //exists checks if the email or username already exists in the db
- func exists(param interface{}, ch chan error, session *mgo.Session) {
- var result UserData
- session.DB("dobrich-uslugi").C("users").Find(param).One(&result)
- if (UserData{}) != result {
- ch <- fmt.Errorf("user w/ such %v exists", reflect.TypeOf(param).Name())
- return
- }
- ch <- nil
- }
Add Comment
Please, Sign In to add comment