package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/google"
plogin "github.com/qor5/admin/login"
"github.com/qor5/admin/presets"
"github.com/qor5/web"
"github.com/qor5/x/login"
. "github.com/theplant/htmlgo"
h "github.com/theplant/htmlgo"
"github.com/theplant/testingutils"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type User struct {
gorm.Model
login.UserPass
login.OAuthInfo
login.SessionSecure
}
var (
db *gorm.DB
)
func init() {
var err error
db, err = gorm.Open(sqlite.Open("/tmp/login_example.db"), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
panic(err)
}
if err := db.AutoMigrate(&User{}); err != nil {
panic(err)
}
user := &User{
UserPass: login.UserPass{
Account: "qor@theplant.jp",
Password: "123",
},
}
user.EncryptPassword()
db.Create(user)
}
func main() {
pb := presets.New()
lb := plogin.New(pb).
DB(db).
UserModel(&User{}).
Secret("123").
OAuthProviders(
&login.Provider{
Goth: google.New(os.Getenv("LOGIN_GOOGLE_KEY"), os.Getenv("LOGIN_GOOGLE_SECRET"), "http://localhost:9500/auth/callback?provider=google"),
Key: "google",
Text: "Login with Google",
Logo: RawHTML(``),
},
&login.Provider{
Goth: github.New(os.Getenv("LOGIN_GITHUB_KEY"), os.Getenv("LOGIN_GITHUB_SECRET"), "http://localhost/auth/callback?provider=github"),
Key: "github",
Text: "Login with Github",
Logo: RawHTML(``),
},
).
AfterConfirmSendResetPasswordLink(func(r *http.Request, user interface{}, extraVals ...interface{}) error {
resetLink := extraVals[0]
fmt.Println("#########################################start")
testingutils.PrintlnJson(resetLink)
fmt.Println("#########################################end")
return nil
}).
TOTP(false)
pb.ProfileFunc(func(ctx *web.EventContext) h.HTMLComponent {
return h.A(h.Text("logout")).Href(lb.LogoutURL)
})
r := http.NewServeMux()
r.Handle("/", pb)
lb.Mount(r)
mux := http.NewServeMux()
mux.Handle("/", lb.Middleware()(r))
log.Println("serving at http://localhost:9500")
log.Fatal(http.ListenAndServe(":9500", mux))
}