Browse Source

Login action and refactoring

Vova Tkach 6 years ago
parent
commit
cf92c72d83
2 changed files with 56 additions and 2 deletions
  1. 6 2
      engine/actions/action_mysql.go
  2. 50 0
      engine/actions/action_signin.go

+ 6 - 2
engine/actions/action_mysql.go

@@ -56,12 +56,16 @@ func (this *Action) Action_mysql() {
 	}
 
 	// Try to install all tables
-	_, err = db.Query(fmt.Sprintf("CREATE TABLE `%s`.`users` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `first_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name', `last_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name', `email` VARCHAR(64) NOT NULL COMMENT 'User email', `password` VARCHAR(32) NOT NULL COMMENT 'User password (MD5)', PRIMARY KEY (`id`)) ENGINE = InnoDB;", pf_name))
+	_, err = db.Query(fmt.Sprintf(
+		"CREATE TABLE `%s`.`users` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `first_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name', `last_name` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name', `email` VARCHAR(64) NOT NULL COMMENT 'User email', `password` VARCHAR(32) NOT NULL COMMENT 'User password (MD5)', PRIMARY KEY (`id`)) ENGINE = InnoDB;",
+		pf_name))
 	if err != nil {
 		this.msg_error(err.Error())
 		return
 	}
-	_, err = db.Query(fmt.Sprintf("CREATE TABLE `%s`.`pages` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `parent` int(11) NOT NULL DEFAULT '0' COMMENT 'Parent page id', `user` int(11) NOT NULL COMMENT 'User id', `name` varchar(255) NOT NULL COMMENT 'Page name', `slug` varchar(255) NOT NULL COMMENT 'Page url part', `content` text NOT NULL COMMENT 'Page content', `meta_title` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title', `meta_keywords` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords', `meta_description` varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description', `datetime` datetime NOT NULL COMMENT 'Creation date/time', `status` enum('draft','public','trash') NOT NULL COMMENT 'Page status', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;", pf_name))
+	_, err = db.Query(fmt.Sprintf(
+		"CREATE TABLE `%s`.`pages` (`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI', `parent` int(11) NOT NULL DEFAULT '0' COMMENT 'Parent page id', `user` int(11) NOT NULL COMMENT 'User id', `name` varchar(255) NOT NULL COMMENT 'Page name', `slug` varchar(255) NOT NULL COMMENT 'Page url part', `content` text NOT NULL COMMENT 'Page content', `meta_title` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title', `meta_keywords` varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords', `meta_description` varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description', `datetime` datetime NOT NULL COMMENT 'Creation date/time', `status` enum('draft','public','trash') NOT NULL COMMENT 'Page status', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;",
+		pf_name))
 	if err != nil {
 		this.msg_error(err.Error())
 		return

+ 50 - 0
engine/actions/action_signin.go

@@ -1,5 +1,12 @@
 package actions
 
+import (
+	"database/sql"
+	_ "github.com/go-sql-driver/mysql"
+
+	"fmt"
+)
+
 func (this *Action) Action_signin() {
 	if dbe := this.use_database(); dbe != nil {
 		this.msg_error(dbe.Error())
@@ -26,5 +33,48 @@ func (this *Action) Action_signin() {
 		return
 	}
 
+	var user_id int
+	err := this.db.QueryRow(
+		"SELECT `id` FROM `users` WHERE `email` = ? and `password` = MD5(?) LIMIT 1;",
+		pf_email, pf_password).Scan(&user_id)
+
+	if err != nil && err != sql.ErrNoRows {
+		this.msg_error(err.Error())
+		return
+	}
+
+	if err == sql.ErrNoRows {
+		this.msg_error(`Incorrect email or password`)
+		return
+	}
+
+	/*
+	if !this.Session.IsSetInt("UserId") {
+		this.Session.SetInt("UserId", 0)
+	}
+	if !this.Session.IsSetBool("IsLogged") {
+		this.Session.SetBool("IsLogged", false)
+	}
+	*/
+
+	var session_user_id int
+	session_user_id, _ = this.wrapper.Session.GetInt("UserId")
+
+	this.msg_success(fmt.Sprintf(
+		`Test: (%d), (%d)`,
+		user_id, session_user_id))
+
+	// Reload current page
+	//this.write(fmt.Sprintf(`window.location.reload(false);`))
+
+	//this.msg_success(`Hello from web server`)
+	//this.write(fmt.Sprintf(``))
+
+	/*
+	if count <= 0 {
+		return this.wrapper.TmplBackEnd(templates.CpFirstUser, nil)
+	}
+	*/
+
 	//this.msg_success(`Hello from web server`)
 }