Skip to content

Commit

Permalink
[auth] added password reset function overload
Browse files Browse the repository at this point in the history
  • Loading branch information
DictumMortuum committed May 20, 2024
1 parent 94b01e4 commit dc048da
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
35 changes: 27 additions & 8 deletions cmd/servus-auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"net/http"
"strings"

"github.com/supertokens/supertokens-golang/ingredients/emaildelivery"
"github.com/supertokens/supertokens-golang/recipe/dashboard"
"github.com/supertokens/supertokens-golang/recipe/session"
"github.com/supertokens/supertokens-golang/recipe/thirdparty"
Expand All @@ -16,7 +18,7 @@ var SuperTokensConfig = supertokens.TypeInput{
ConnectionURI: "http://sol.dictummortuum.com:3567",
},
AppInfo: supertokens.AppInfo{
AppName: "Tables",
AppName: "DictumMortuum",
APIDomain: "https://auth.dictummortuum.com",
// WebsiteDomain: "https://tables.dictummortuum.com",
GetOrigin: func(request *http.Request, userContext supertokens.UserContext) (string, error) {
Expand All @@ -27,12 +29,10 @@ var SuperTokensConfig = supertokens.TypeInput{
// there is a privacy setting on the frontend which doesn't send
// the origin
} else {
if origin == "https://tables.dictummortuum.com" {
// query from the test site
return "https://tables.dictummortuum.com", nil
} else if origin == "http://localhost:3000" {
// query from local development
return "http://localhost:3000", nil
for _, item := range Origins {
if origin == item {
return item, nil
}
}
}
}
Expand All @@ -42,7 +42,26 @@ var SuperTokensConfig = supertokens.TypeInput{
},
},
RecipeList: []supertokens.Recipe{
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{}),
thirdpartyemailpassword.Init(&tpepmodels.TypeInput{
EmailDelivery: &emaildelivery.TypeInput{
Override: func(originalImplementation emaildelivery.EmailDeliveryInterface) emaildelivery.EmailDeliveryInterface {
ogSendEmail := *originalImplementation.SendEmail

(*originalImplementation.SendEmail) = func(input emaildelivery.EmailType, userContext supertokens.UserContext) error {
// You can change the path, domain of the reset password link,
// or even deep link it to your mobile app
// This is: `${websiteDomain}${websiteBasePath}/reset-password`
input.PasswordReset.PasswordResetLink = strings.Replace(
input.PasswordReset.PasswordResetLink,
"/auth/reset-password",
"/#/auth/reset-password", 1,
)
return ogSendEmail(input, userContext)
}
return originalImplementation
},
},
}),
session.Init(nil),
dashboard.Init(nil),
thirdparty.Init(nil),
Expand Down
15 changes: 10 additions & 5 deletions cmd/servus-auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@ import (
"github.com/supertokens/supertokens-golang/supertokens"
)

var (
Origins = []string{
"https://tables.dictummortuum.com",
"https://tables.dictummortuum.com",
"http://localhost:3000",
}
)

func Version(c *gin.Context) {
rs := map[string]any{
"version": "v0.0.3",
"version": "v0.0.4",
}
c.AbortWithStatusJSON(200, rs)
}
Expand All @@ -35,10 +43,7 @@ func main() {
r := gin.Default()

r.Use(cors.New(cors.Config{
AllowOrigins: []string{
"http://localhost:3000",
"https://tables.dictummortuum.com",
},
AllowOrigins: Origins,
AllowMethods: []string{"GET", "POST", "DELETE", "PUT", "OPTIONS"},
AllowHeaders: append([]string{"content-type"},
supertokens.GetAllCORSHeaders()...),
Expand Down
8 changes: 4 additions & 4 deletions pkg/db/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/redis/go-redis/v9"
)

func set(rdb *redis.Client, key string, val any) error {
func Set(rdb *redis.Client, key string, val any) error {
p, err := json.Marshal(val)
if err != nil {
return err
Expand All @@ -23,7 +23,7 @@ func set(rdb *redis.Client, key string, val any) error {
return nil
}

func get(rdb *redis.Client, key string, dest any) error {
func Get(rdb *redis.Client, key string, dest any) error {
p, err := rdb.Get(context.Background(), key).Result()
if err != nil {
return err
Expand All @@ -33,14 +33,14 @@ func get(rdb *redis.Client, key string, dest any) error {
}

func CachedSelect(DB *sqlx.DB, RDB *redis.Client, key string, dest any, query string, args ...any) error {
err := get(RDB, key, dest)
err := Get(RDB, key, dest)
if err == redis.Nil {
err = DB.Select(dest, query, args...)
if err != nil {
return err
}

err = set(RDB, key, dest)
err = Set(RDB, key, dest)
if err != nil {
return err
}
Expand Down

0 comments on commit dc048da

Please sign in to comment.