Skip to content

Commit

Permalink
fixed pgtype.uuid errors
Browse files Browse the repository at this point in the history
Signed-off-by: dhruv <[email protected]>
  • Loading branch information
jaydee029 committed Oct 28, 2024
1 parent 7cf53f2 commit f936a80
Show file tree
Hide file tree
Showing 13 changed files with 206 additions and 48 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
node_modules
database.json
Barkin
verses
Verses
10 changes: 5 additions & 5 deletions create_prose.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
token, err := auth.BearerHeader(r.Header)

if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
respondWithError(w, http.StatusUnauthorized, "error decoding auth header:"+err.Error())
return
}
authorid, err := auth.ValidateToken(token, cfg.jwtsecret)

if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
respondWithError(w, http.StatusUnauthorized, "error parsing the userid:"+err.Error())
return
}
decoder := json.NewDecoder(r.Body)
Expand All @@ -65,7 +65,7 @@ func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
var pgUUID pgtype.UUID
err = pgUUID.Scan(authorid)
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
respondWithError(w, http.StatusInternalServerError, "error parsing into pgtype.uuid")
return
}
uuids := uuid.New().String()
Expand All @@ -82,13 +82,13 @@ func (cfg *apiconfig) postProse(w http.ResponseWriter, r *http.Request) {
var pgtime pgtype.Timestamp
err = pgtime.Scan(time.Now().UTC())
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
respondWithError(w, http.StatusInternalServerError, "error parsing timestamp into pgtype value")
return
}

tx, err := cfg.DBpool.Begin(r.Context())
if err != nil {
respondWithError(w, http.StatusInternalServerError, err.Error())
respondWithError(w, http.StatusInternalServerError, "error starting the transaction"+err.Error())
return
}

Expand Down
22 changes: 15 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
module github.com/jaydee029/Verses

go 1.20
go 1.21

toolchain go1.21.6

require (
github.com/go-chi/chi/v5 v5.0.10
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/google/uuid v1.5.0
github.com/jackc/pgx/v5 v5.5.5
github.com/jackc/pgx/v5 v5.7.1
github.com/joho/godotenv v1.5.1
github.com/lib/pq v1.10.9
golang.org/x/crypto v0.17.0
golang.org/x/crypto v0.27.0
)

require (
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/text v0.14.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v4 v4.18.3 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
164 changes: 164 additions & 0 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions internal/auth/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ import (
"time"

"github.com/golang-jwt/jwt/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/google/uuid"
)

type Revoke struct {
Token string `json:"token"`
Revoked_at time.Time `json:"revoked_at"`
}

func Tokenize(id pgtype.UUID, secret_key string) (string, error) {
func Tokenize(id uuid.UUID, secret_key string) (string, error) {
secret_key_byte := []byte(secret_key)

claims := &jwt.RegisteredClaims{
Issuer: "verses-access",
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().Add(time.Duration(60*60) * time.Second)), // 1 hour
Subject: string(id.Bytes[:]),
Subject: id.String(),
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Expand All @@ -33,14 +33,14 @@ func Tokenize(id pgtype.UUID, secret_key string) (string, error) {
return ss, nil
}

func RefreshToken(id pgtype.UUID, secret_key string) (string, error) {
func RefreshToken(id uuid.UUID, secret_key string) (string, error) {
secret_key_byte := []byte(secret_key)

claims := &jwt.RegisteredClaims{
Issuer: "verses-refresh",
IssuedAt: jwt.NewNumericDate(time.Now().UTC()),
ExpiresAt: jwt.NewNumericDate(time.Now().UTC().AddDate(0, 2, 0)), // 60 days
Subject: string(id.Bytes[:]),
Subject: id.String(),
}

refresh_token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
Expand Down
1 change: 1 addition & 0 deletions sql/schema/001_users.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE users(
Name VARCHAR(100) NOT NULL,
Email VARCHAR(100) NOT NULL,
passwd bytea NOT NULL,
username VARCHAR(12) NOT NULL UNIQUE,
id uuid PRIMARY KEY,
created_at TIMESTAMP NOT NULL,
updated_at TIMESTAMP NOT NULL
Expand Down
2 changes: 1 addition & 1 deletion sql/schema/002_chirps.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- +goose Up
CREATE TABLE chirps(
id INT NOT NULL,
id uuid PRIMARY KEY,
body TEXT NOT NULL,
author_id uuid NOT NULL REFERENCES users(id),
created_at TIMESTAMP NOT NULL,
Expand Down
6 changes: 2 additions & 4 deletions sql/schema/006_users.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
-- +goose Up
ALTER TABLE users
ADD COLUMN followers INT NOT NULL DEFAULT 0 CHECK (followers>=0),
ADD COLUMN followees INT NOT NULL DEFAULT 0 CHECK (followees>=0),
ADD COLUMN username VARCHAR(12) NOT NULL UNIQUE;
ADD COLUMN followees INT NOT NULL DEFAULT 0 CHECK (followees>=0);

CREATE TABLE follows(
follower_id uuid NOT NULL REFERENCES users(id),
Expand All @@ -13,7 +12,6 @@ CREATE TABLE follows(
-- +goose Down
ALTER TABLE users
DROP COLUMN followers,
DROP COLUMN followees,
DROP COLUMN username;
DROP COLUMN followees;

DROP TABLE follows;
6 changes: 0 additions & 6 deletions sql/schema/007_timeline.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@ CREATE TABLE timeline(
CREATE UNIQUE INDEX sorted_prose ON prose(created_at DESC);
CREATE UNIQUE INDEX unique_timeline ON timeline(prose_id,user_id);

ALTER TABLE prose
ALTER COLUMN id SET DATA TYPE uuid;

-- +goose Down
DROP TABLE timeline;
DROP INDEX sorted_prose;
DROP INDEX unique_timeline;

ALTER TABLE prose
ALTER COLUMN id INT;
2 changes: 1 addition & 1 deletion sql/schema/008_likes.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
CREATE TABLE post_likes(
prose_id uuid NOT NULL REFERENCES prose(id),
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (post_id,user_id)
PRIMARY KEY(prose_id,user_id)
);

ALTER TABLE prose
Expand Down
2 changes: 1 addition & 1 deletion sql/schema/009_comments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CREATE INDEX post_comments ON comments(created_at DESC);
CREATE TABLE comment_likes(
comment_id INT NOT NULL REFERENCES comments(id),
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (prose_id,user_id)
PRIMARY KEY (comment_id,user_id)
);

ALTER TABLE prose
Expand Down
18 changes: 4 additions & 14 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ import (
"net/http"
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
auth "github.com/jaydee029/Verses/internal/auth"
"github.com/jaydee029/Verses/internal/database"
)

func (cfg *apiconfig) revokeToken(w http.ResponseWriter, r *http.Request) {
/*
decoder := json.NewDecoder(r.Body)
params := User{}
err := decoder.Decode(&params)

if err != io.EOF {
respondWithError(w, http.StatusUnauthorized, "Body is provided")
return
}
*/
token, err := auth.BearerHeader(r.Header)

if err != nil {
Expand Down Expand Up @@ -86,14 +78,12 @@ func (cfg *apiconfig) verifyRefresh(w http.ResponseWriter, r *http.Request) {
return
}

var pgUUID pgtype.UUID

err = pgUUID.Scan(Idstr)
Id, err := uuid.Parse(Idstr)
if err != nil {
fmt.Println("Error setting UUID:", err)
fmt.Println("Error parsing string to UUID:", err)
}

auth_token, err := auth.Tokenize(pgUUID, cfg.jwtsecret)
auth_token, err := auth.Tokenize(Id, cfg.jwtsecret)

if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
Expand Down
8 changes: 5 additions & 3 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,17 @@ func (cfg *apiconfig) userLogin(w http.ResponseWriter, r *http.Request) {
if err != nil {
respondWithError(w, http.StatusUnauthorized, "Password doesn't match")
}
uuid.FromBytes(user.ID.Bytes[:])
Token, err := auth.Tokenize(user.ID, cfg.jwtsecret)

Userid, _ := uuid.FromBytes(user.ID.Bytes[:])

Token, err := auth.Tokenize(Userid, cfg.jwtsecret)

if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
return
}

Refresh_token, err := auth.RefreshToken(user.ID, cfg.jwtsecret)
Refresh_token, err := auth.RefreshToken(Userid, cfg.jwtsecret)

if err != nil {
respondWithError(w, http.StatusUnauthorized, err.Error())
Expand Down

0 comments on commit f936a80

Please sign in to comment.