diff options
author | Julien Dessaux | 2021-04-13 22:53:29 +0200 |
---|---|---|
committer | Julien Dessaux | 2021-04-13 22:57:10 +0200 |
commit | 2d75d2cb7443a2c398dc317ae6335153b4c05f03 (patch) | |
tree | 787bf916964e47635c4051472e20f59b24b468f1 /pkg/database/sessions.go | |
parent | Renamed password column to hash in database (diff) | |
download | trains-2d75d2cb7443a2c398dc317ae6335153b4c05f03.tar.gz trains-2d75d2cb7443a2c398dc317ae6335153b4c05f03.tar.bz2 trains-2d75d2cb7443a2c398dc317ae6335153b4c05f03.zip |
Added a session table and a CreateSession function
Diffstat (limited to '')
-rw-r--r-- | pkg/database/sessions.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/pkg/database/sessions.go b/pkg/database/sessions.go new file mode 100644 index 0000000..72930f0 --- /dev/null +++ b/pkg/database/sessions.go @@ -0,0 +1,33 @@ +package database + +import ( + "git.adyxax.org/adyxax/trains/pkg/model" + "github.com/google/uuid" +) + +func (env *DBEnv) CreateSession(user *model.User) (*string, error) { + token := uuid.NewString() + + query := ` + INSERT INTO sessions + (token, user_id) + VALUES + ($1, $2);` + tx, err := env.db.Begin() + if err != nil { + return nil, newTransactionError("Could not Begin()", err) + } + _, err = tx.Exec( + query, + token, + user.Id, + ) + if err != nil { + tx.Rollback() + return nil, newQueryError("Could not run database query: most likely the token already exists in database, or the user id does not exist", err) + } + if err := tx.Commit(); err != nil { + return nil, newTransactionError("Could not commit transaction", err) + } + return &token, nil +} |