summaryrefslogtreecommitdiff
path: root/db/account.go
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-07-04 13:13:23 +0530
committersinanmohd <sinan@sinanmohd.com>2024-07-05 17:02:06 +0530
commite2f67996a608346ea3f3525ef2febf6ca5d2b78c (patch)
tree932f766b65ed5a6ff3d57c772292a2b75210d9aa /db/account.go
parente309091d17720b69c53172a41c0ea45ad7b66911 (diff)
refactor: drop http api, move to sqlc/postgresql
Diffstat (limited to 'db/account.go')
-rw-r--r--db/account.go129
1 files changed, 0 insertions, 129 deletions
diff --git a/db/account.go b/db/account.go
deleted file mode 100644
index 0668016..0000000
--- a/db/account.go
+++ /dev/null
@@ -1,129 +0,0 @@
-package db
-
-import (
- "errors"
- "fmt"
- "log"
-)
-
-type Account struct {
- UserName string `validate:"required,alphanum,max=64"`
- Password string `json:",omitempty" validate:"required,min=10,max=128"`
-
- Info *Login
-}
-
-type Login struct {
- id uint
- Level uint `validate:"gte=0,lte=100"`
- FirstName, LastName string `validate:"required,alphanumunicode"`
- Bearer *Bearer
-}
-
-func (ac *Account) CreateAccount(safe *SafeDB) error {
- const sqlStatement string = `
- INSERT INTO Accounts (
- id,
- UserName,
- Password,
- Level,
- FirstName,
- LastName
- )
- VALUES (NULL, ?, ?, ?, ?, ?);
- `
-
- err := safe.validate.Struct(ac)
- if err != nil {
- return err
- }
-
- safe.mu.Lock()
- defer safe.mu.Unlock()
-
- _, err = safe.db.Exec(
- sqlStatement,
- ac.UserName,
- ToBlake3(ac.Password),
-
- ac.Info.FirstName,
- ac.Info.LastName,
- ac.Info.Level,
- )
-
- return err
-}
-
-func (ac *Account) Login(safe *SafeDB) error {
- const sqlStatementQuery string = `
- SELECT id, Password, Level, FirstName, LastName
- FROM Accounts
- WHERE Accounts.UserName = ?
- `
-
- err := safe.validate.Struct(ac)
- fmt.Println(ac.Password, ac.UserName)
- if err != nil {
- log.Println(err)
- return err
- }
-
- ac.Info = &Login{}
- ac.Info.Bearer = &Bearer{}
- safe.mu.Lock()
- row := safe.db.QueryRow(sqlStatementQuery, ac.UserName)
- safe.mu.Unlock()
-
- var Password string
- err = row.Scan(
- &ac.Info.id,
- &Password,
- &ac.Info.FirstName,
- &ac.Info.LastName,
- &ac.Info.Level,
- )
- if err != nil {
- return err
- }
- if Password != ToBlake3(ac.Password) {
- return errors.New("Auth failed")
- }
- ac.Password = ""
-
- err = ac.Info.Bearer.Generate(safe, ac.Info)
- if err != nil {
- return err
- }
-
- return err
-}
-
-func (ac *Account) fromBearer(safe *SafeDB, b *Bearer) error {
- const sqlStatementAccount string = `
- SELECT UserName, Password, Level, FirstName, LastName
- FROM Accounts
- WHERE Accounts.id = ?
- `
-
- safe.mu.Lock()
- row := safe.db.QueryRow(sqlStatementAccount, b.accountId)
- safe.mu.Unlock()
-
- ac.Info = &Login{}
- ac.Info.id = b.accountId
- ac.Info.Bearer = b
- err := row.Scan(
- &ac.UserName,
- &ac.Password,
-
- &ac.Info.FirstName,
- &ac.Info.LastName,
- &ac.Info.Level,
- )
- if err != nil {
- return err
- }
- ac.Info.Bearer = b
-
- return err
-}