Ejemplo n.º 1
0
Archivo: auth.go Proyecto: grffn/movier
//LoginHandler Hndler for login method
func LoginHandler(context *gin.Context, database *db.Context) {
	var model models.LoginModel
	err := context.BindJSON(&model)
	if err != nil {
		log.Println(err)
		context.AbortWithError(http.StatusBadRequest, err)
		return
	}
	user := database.FindUser(model.UserID)
	storedPassword, _ := base64.URLEncoding.DecodeString(user.Password)
	salt, _ := base64.URLEncoding.DecodeString(user.Salt)
	checkPassword, _ := util.GeneratePassword([]byte(model.Password), salt)
	if bytes.Compare(storedPassword, checkPassword) == 0 {
		token := jwt_lib.New(jwt_lib.GetSigningMethod("HS256"))
		token.Claims["ID"] = user.Username
		token.Claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
		tokenString, err := token.SignedString([]byte(secret))
		if err != nil {
			context.JSON(500, gin.H{"message": "Could not generate token"})
			return
		}
		context.JSON(200, gin.H{"token": tokenString})
	} else {
		context.JSON(http.StatusUnauthorized, gin.H{"status": "Login or password is incorrect"})
	}
}
Ejemplo n.º 2
0
//CreateHandler Create new item
func CreateHandler(context *gin.Context, database *db.Context) {
	model := models.DocModel{}
	err := context.BindJSON(&model)
	if err != nil {
		context.AbortWithStatus(http.StatusBadRequest)
		return
	}
	userID := context.MustGet("userId").(string)
	user := database.FindUser(userID)
	document := db.Document{
		ID:       bson.NewObjectId(),
		UserID:   user.ID,
		Name:     model.Name,
		Tags:     model.Tags,
		Category: model.Category,
		URL:      model.URL,
		MimeType: model.MimeType,
		Authors:  model.Authors,
	}
	database.NewDocument(document)
}