Saturday, August 10, 2019

Login Security System in Go and MySQL

Here is a simple web based login security system in Go and MySQL as our database.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.

My personal website is http://www.jakerpomperada.com



Installing of MySQL Driver for Go


Insertion of User Name and Password in phpMyAdmin Control Panel


Login Page




Running the Program

Successfully login in the system


Sample Program Output


Program Listing

create_login.sql

create database login;
use login;
create table users(id int auto_increment primary key, username varchar(100), password varchar(100));
insert into users(username,password) values("admin",md5("admin"));

login.go

package main 

import (
    "database/sql"
    "net/http"
    "log"
    "text/template"
    _ "github.com/go-sql-driver/mysql"
    "crypto/md5"
    "encoding/hex"
)

type User struct {
    Id    int
    Name  string
    Password string
}

func dbConn() (db *sql.DB) {
    dbDriver := "mysql"
    dbUser := "root"
    dbPass := ""
    dbName := "login"
    db, err := sql.Open(dbDriver, dbUser+":"+dbPass+"@/"+dbName)
    if err != nil {
        panic(err.Error())
    }
    return db
}

var tmpl = template.Must(template.ParseGlob("templates/*"))

func main() {
    log.Println("Server started on: http://localhost:9090")
    http.HandleFunc("/", Index)
    http.HandleFunc("/login", Login)
    http.ListenAndServe(":9090", nil)
}

func Index(w http.ResponseWriter, r *http.Request) {
    tmpl.ExecuteTemplate(w, "Index", nil)
}

func Login(w http.ResponseWriter, r *http.Request) {

r.ParseForm()                     
    username := r.Form.Get("username") 
    p := r.Form.Get("password")

    password := GetMD5Hash(p)

    db := dbConn()
    sqlStmt := "SELECT * FROM Users WHERE username=? AND password=?"
    
    var id int
    row := db.QueryRow(sqlStmt,username,password)
    
    switch err := row.Scan(&id); err {
    case sql.ErrNoRows:
    tmpl.ExecuteTemplate(w, "Index", nil)
    default:
    tmpl.ExecuteTemplate(w, "Success", nil)
    }
    
    defer db.Close()
}

func GetMD5Hash(text string) string {
    hasher := md5.New()
    hasher.Write([]byte(text))
    return hex.EncodeToString(hasher.Sum(nil))
}


index.tmpl

{{ define "Index" }}
<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Login</title>
        <meta charset="UTF-8" />
    </head>
<body>
<form name="login" action="/login" method="POST">
<table>
<tr><td>Username<input type="text" name="username"/></td>
<tr><td>Password<input type="password" name="password"/></td>
<tr><td><input type="submit" value="Login"/></td>
</form>
</body>
</html>
{{ end }}

Success.tmpl

{{ define "Success" }}
<h1>Success</h1>
{{ end }}







No comments:

Post a Comment