-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/77/send verification email (#81)
- Loading branch information
Showing
19 changed files
with
341 additions
and
18 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
backend/.sqlc/migrations/20241205185206_create_verify_email_tokens_table.sql
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,16 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS verify_email_tokens ( | ||
-- id column will be used in the standard jwt id claims. | ||
-- which can be used to identify/select the record in the db. | ||
id UUID PRIMARY KEY DEFAULT uuid_generate_v4() NOT NULL, | ||
email VARCHAR(255) UNIQUE NOT NULL, | ||
expires_at TIMESTAMP NOT NULL, | ||
created_at TIMESTAMP DEFAULT NOW() | ||
); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS verify_email_tokens; | ||
-- +goose StatementEnd |
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,11 @@ | ||
-- name: CreateVerifyEmailToken :one | ||
INSERT INTO verify_email_tokens ( | ||
email, | ||
expires_at | ||
) VALUES ( | ||
$1, $2 | ||
) RETURNING *; | ||
|
||
-- name: GetVerifyEmailTokenByID :one | ||
SELECT * FROM verify_email_tokens | ||
WHERE id = $1 LIMIT 1; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -6,13 +6,15 @@ import ( | |
"time" | ||
|
||
"KonferCA/SPUR/db" | ||
|
||
golangJWT "github.com/golang-jwt/jwt/v5" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestJWT(t *testing.T) { | ||
// setup env | ||
os.Setenv("JWT_SECRET", "secret") | ||
os.Setenv("JWT_SECRET_VERIFY_EMAIL", "test-secret") | ||
|
||
userID := "some-user-id" | ||
role := db.UserRole("user") | ||
|
@@ -77,4 +79,37 @@ func TestJWT(t *testing.T) { | |
_, err = VerifyToken(token) | ||
assert.NotNil(t, err) | ||
}) | ||
|
||
t.Run("generate verify email token", func(t *testing.T) { | ||
email := "[email protected]" | ||
id := "some-id" | ||
exp := time.Now().Add(time.Second * 5) | ||
token, err := GenerateVerifyEmailToken(email, id, exp) | ||
assert.Nil(t, err) | ||
claims, err := VerifyEmailToken(token) | ||
assert.Nil(t, err) | ||
assert.Equal(t, claims.Email, email) | ||
assert.Equal(t, claims.ID, id) | ||
assert.Equal(t, claims.ExpiresAt.Unix(), exp.Unix()) | ||
}) | ||
|
||
t.Run("deny expired verify email token", func(t *testing.T) { | ||
email := "[email protected]" | ||
id := "some-id" | ||
exp := time.Now().Add(-1 * 5 * time.Second) | ||
token, err := GenerateVerifyEmailToken(email, id, exp) | ||
assert.Nil(t, err) | ||
_, err = VerifyEmailToken(token) | ||
assert.NotNil(t, err) | ||
}) | ||
|
||
t.Run("deny expired verify email token", func(t *testing.T) { | ||
email := "[email protected]" | ||
id := "some-id" | ||
exp := time.Now().Add(-1 * 5 * time.Second) | ||
token, err := GenerateVerifyEmailToken(email, id, exp) | ||
assert.Nil(t, err) | ||
_, err = VerifyEmailToken(token) | ||
assert.NotNil(t, err) | ||
}) | ||
} |
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
Oops, something went wrong.