-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
128 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,6 @@ __pycache__ | |
# Files to ignore | ||
profiles.json | ||
test.py | ||
TODO | ||
TODO | ||
app.db | ||
go-web/go-web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters