Content inside supertokenslib
(The raw file follows this syntax highlighted file.)
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
stl "git.ondollo.com/supertokenslib"
)
func envRequire(k string) string {
v, exists := os.LookupEnv(k)
if !exists {
panic(fmt.Sprintf("Required env var '%s' was not defined in the current environment", k))
}
return v
}
func main() {
cfg := stl.Config{
PlainUI: true,
ConnectionURI: envRequire("CONNECTION_URI"),
APIKey: envRequire("API_KEY"),
AppName: envRequire("APP_NAME"),
APIDomain: envRequire("API_DOMAIN"),
WebsiteDomain: envRequire("WEBSITE_DOMAIN"),
APIBasePath: envRequire("API_BASE_PATH"),
WebsiteBasePath: envRequire("WEBSITE_BASE_PATH"),
SuperTokensWebLibBase: envRequire("SUPERTOKENS_WEB_LIB_BASE"),
UserSignedUp: func(uid string, uemail string) { fmt.Println("Signed up:", uid, uemail) },
UserSignedIn: func(uid string, uemail string) { fmt.Println("Signed in:", uid, uemail) },
}
cfg.Init()
mux := http.NewServeMux()
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
if userID, userEmail, exists := cfg.GetUser(w, r); exists {
fmt.Println("Test endpoint called with session:", userID, userEmail)
fmt.Fprint(w, `{"loggedIn":true}`)
} else {
fmt.Println("Test endpoint called WITHOUT session")
fmt.Fprint(w, `{"loggedIn":false}`)
}
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, `
<!DOCTYPE html>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script type="module" src="%s/client.js"></script>
<script>
globalThis.authHooks = [["catch", (err) => console.log("Auth err:", err)],["state", (s,d) => console.log("Auth state:", s, d)]]
console.log("Calling test")
fetch("/test").then((d)=>d.json()).then((d)=>console.log("Test result:", d)).catch((err)=>console.log("Test call failed:", err))
console.log("Done calling test")
</script>
Main
`, cfg.APIBasePath)
})
s := &http.Server{
Addr: ":8080",
Handler: cfg.Middleware(mux),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Println("Listening")
log.Fatal(s.ListenAndServe())
}
package main import ( "fmt" "log" "net/http" "os" "time" stl "git.ondollo.com/supertokenslib" ) func envRequire(k string) string { v, exists := os.LookupEnv(k) if !exists { panic(fmt.Sprintf("Required env var '%s' was not defined in the current environment", k)) } return v } func main() { cfg := stl.Config{ PlainUI: true, ConnectionURI: envRequire("CONNECTION_URI"), APIKey: envRequire("API_KEY"), AppName: envRequire("APP_NAME"), APIDomain: envRequire("API_DOMAIN"), WebsiteDomain: envRequire("WEBSITE_DOMAIN"), APIBasePath: envRequire("API_BASE_PATH"), WebsiteBasePath: envRequire("WEBSITE_BASE_PATH"), SuperTokensWebLibBase: envRequire("SUPERTOKENS_WEB_LIB_BASE"), UserSignedUp: func(uid string, uemail string) { fmt.Println("Signed up:", uid, uemail) }, UserSignedIn: func(uid string, uemail string) { fmt.Println("Signed in:", uid, uemail) }, } cfg.Init() mux := http.NewServeMux() mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { if userID, userEmail, exists := cfg.GetUser(w, r); exists { fmt.Println("Test endpoint called with session:", userID, userEmail) fmt.Fprint(w, `{"loggedIn":true}`) } else { fmt.Println("Test endpoint called WITHOUT session") fmt.Fprint(w, `{"loggedIn":false}`) } }) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, ` <!DOCTYPE html> <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <script type="module" src="%s/client.js"></script> <script> globalThis.authHooks = [["catch", (err) => console.log("Auth err:", err)],["state", (s,d) => console.log("Auth state:", s, d)]] console.log("Calling test") fetch("/test").then((d)=>d.json()).then((d)=>console.log("Test result:", d)).catch((err)=>console.log("Test call failed:", err)) console.log("Done calling test") </script> Main `, cfg.APIBasePath) }) s := &http.Server{ Addr: ":8080", Handler: cfg.Middleware(mux), ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } fmt.Println("Listening") log.Fatal(s.ListenAndServe()) }