Skip to content

Commit

Permalink
feat: add user authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
FlareXes committed Nov 16, 2024
1 parent 22d7aa9 commit 44ab6f2
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 19 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ __pycache__
# Files to ignore
profiles.json
test.py
TODO
TODO
app.db
go-web/go-web
5 changes: 5 additions & 0 deletions go-web/conf/app.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
appname = go-web
httpport = 8080
runmode = dev
sessionon = true
sessionprovider = memory
sessionname = beegosession
db_driver = sqlite3
db_path = ./app.db
79 changes: 79 additions & 0 deletions go-web/controllers/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package controllers

import (
"github.com/beego/beego/v2/client/orm"
"golang.org/x/crypto/bcrypt"

beego "github.com/beego/beego/v2/server/web"
models "github.com/flarexes/offsync/go-web/models"
)

type AuthController struct {
beego.Controller
}

func (c *AuthController) GetSignUp() {
c.TplName = "signup.html"
}

func (c *AuthController) GetSignIn() {
userID := c.GetSession("user_id")
if userID != nil {
c.Redirect("/", 302)
return
}

c.TplName = "signin.html"
}

func (c *AuthController) SignUp() {
username := c.GetString("username")
password := c.GetString("password")
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)

user := models.User{Username: username, Password: string(hashedPassword)}
o := orm.NewOrm()
_, err := o.Insert(&user)
if err != nil {
c.Ctx.WriteString("Error during sign up")
return
}
c.Redirect("/signup", 302)
}

func (c *AuthController) SignIn() {
userID := c.GetSession("user_id")
if userID != nil {
c.Redirect("/", 302)
return
}

username := c.GetString("username")
password := c.GetString("password")

o := orm.NewOrm()
user := models.User{Username: username}
err := o.Read(&user, "Username")

// If User Not Found Return
if err == orm.ErrNoRows {
c.Ctx.WriteString("Invalid credentials")
return
}

err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password))
if err != nil {
c.Ctx.WriteString("Invalid credentials")
return
}
c.SetSession("user_id", user.Id)
c.SetSession("username", user.Username)

c.Redirect("/", 302)
}

func (c *AuthController) LogOut() {
c.DelSession("user_id")
c.DelSession("username")
c.Redirect("/", 302)
}
30 changes: 13 additions & 17 deletions go-web/controllers/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@ type OffsyncController struct {
}

func (c *OffsyncController) Get() {
userID := c.GetSession("user_id")
if userID == nil {
c.Redirect("/signin", 302)
return
}
c.TplName = "index.tpl"
}

func (c *OffsyncController) Post() {
var profile models.Profile
c.TplName = "index.tpl"
userID := c.GetSession("user_id")
if userID == nil {
c.Redirect("/signin", 302)
return
}

var profile models.Profile
if err := c.ParseForm(&profile); err != nil {
c.Ctx.WriteString("Error binding form data: " + err.Error())
return
}

password := models.GeneratePassword(profile)

c.Data["Answer"] = password

// c.SetSession("password", password)

c.SetSession("username", "john_doe")
c.SetSession("logged_in", true)
c.Ctx.WriteString("Session set!")

// TODO
// if site == "" || username == "" || length == "" || counter == "" {
// fmt.Println("Missing form fields!")
// }
// c.Ctx.Redirect(302, "/")
// c.Redirect("/", 302)
c.Ctx.SetCookie("password", password)
c.Redirect("/", 302)
}
Binary file modified go-web/go-web
Binary file not shown.
1 change: 1 addition & 0 deletions go-web/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go-web/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
10 changes: 9 additions & 1 deletion go-web/main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package main

import (
_ "github.com/flarexes/offsync/go-web/routers"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/flarexes/offsync/go-web/routers"
_ "github.com/mattn/go-sqlite3"
)

func init() {
orm.RegisterDriver("sqlite", orm.DRSqlite)
orm.RegisterDataBase("default", "sqlite3", "./app.db")
orm.RunSyncdb("default", false, true)
}

func main() {
beego.Run()
}
13 changes: 13 additions & 0 deletions go-web/models/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package models

import "github.com/beego/beego/v2/client/orm"

type User struct {
Id int64 `orm:"auto"`
Username string `orm:"unique"`
Password string
}

func init() {
orm.RegisterModel(new(User))
}
3 changes: 3 additions & 0 deletions go-web/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ import (
func init() {
beego.Router("/", &controllers.OffsyncController{})
beego.Router("/submit", &controllers.OffsyncController{}, "post:Post")
beego.Router("/signup", &controllers.AuthController{}, "get:GetSignUp;post:SignUp")
beego.Router("/signin", &controllers.AuthController{}, "get:GetSignIn;post:SignIn")
beego.Router("/logout", &controllers.AuthController{}, "get:LogOut")
}

0 comments on commit 44ab6f2

Please sign in to comment.