123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- package example_basics
- // @snippet_begin(LoginBasicUsage)
- import (
- "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/ui/vuetify"
- "github.com/qor5/web"
- "github.com/qor5/x/login"
- . "github.com/theplant/htmlgo"
- "gorm.io/gorm"
- )
- type User struct {
- gorm.Model
- Name string
- Address string
- login.UserPass
- login.OAuthInfo
- login.SessionSecure
- }
- func serve() {
- pb := presets.New()
- lb := plogin.New(pb).
- DB(DB).
- UserModel(&User{}).
- Secret(os.Getenv("LOGIN_SECRET")).
- OAuthProviders(
- &login.Provider{
- Goth: google.New(os.Getenv("LOGIN_GOOGLE_KEY"), os.Getenv("LOGIN_GOOGLE_SECRET"), os.Getenv("BASE_URL")+"/auth/callback?provider=google"),
- Key: "google",
- Text: "Google",
- },
- &login.Provider{
- Goth: github.New(os.Getenv("LOGIN_GITHUB_KEY"), os.Getenv("LOGIN_GITHUB_SECRET"), os.Getenv("BASE_URL")+"/auth/callback?provider=github"),
- Key: "github",
- Text: "Login with Github",
- },
- )
- pb.ProfileFunc(func(ctx *web.EventContext) HTMLComponent {
- return A(Text("logout")).Href(lb.LogoutURL)
- })
- r := http.NewServeMux()
- r.Handle("/", pb)
- lb.Mount(r)
- mux := http.NewServeMux()
- mux.Handle("/", lb.Middleware()(r))
- http.ListenAndServe(":8080", nil)
- }
- // @snippet_end
- func loginPieces() {
- // @snippet_begin(LoginEnableUserPass)
- type User struct {
- gorm.Model
- login.UserPass
- }
- // @snippet_end
- var loginBuilder *login.Builder
- var count int
- // @snippet_begin(LoginSetMaxRetryCount)
- loginBuilder.MaxRetryCount(count)
- // @snippet_end
- var enable bool
- // @snippet_begin(LoginSetTOTP)
- loginBuilder.TOTP(enable, login.TOTPConfig{
- Issuer: "Issuer",
- })
- // @snippet_end
- // @snippet_begin(LoginSetRecaptcha)
- loginBuilder.Recaptcha(enable, login.RecaptchaConfig{
- SiteKey: "SiteKey",
- SecretKey: "SecretKey",
- })
- // @snippet_end
- }
- func loginPiece2() {
- // @snippet_begin(LoginEnableOAuth)
- type User struct {
- gorm.Model
- login.OAuthInfo
- }
- // @snippet_end
- }
- func loginPiece3() {
- // @snippet_begin(LoginEnableSessionSecure)
- type User struct {
- gorm.Model
- login.UserPass
- login.OAuthInfo
- login.SessionSecure
- }
- // @snippet_end
- }
- func loginPiece4() {
- var loginBuilder *login.Builder
- // @snippet_begin(LoginCustomizePage)
- loginBuilder.LoginPageFunc(func(ctx *web.EventContext) (r web.PageResponse, err error) {
- r.Body = Text("This is login page")
- return
- })
- // @snippet_end
- var mux *http.ServeMux
- var loginPage http.Handler
- // @snippet_begin(LoginCustomizePage2)
- loginBuilder.LoginPageURL("/custom-login-page")
- loginBuilder.MountAPI(mux)
- mux.Handle("/custom-login-page", loginPage)
- // @snippet_end
- }
- func loginPiece5() {
- // @snippet_begin(LoginOpenChangePasswordDialog)
- VBtn("Change Password").OnClick(plogin.OpenChangePasswordDialogEvent)
- // @snippet_end
- var userModelBuilder *presets.ModelBuilder
- // @snippet_begin(LoginChangePasswordInEditing)
- userModelBuilder.Editing().Field("Password").
- SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
- u := obj.(*User)
- if v := ctx.R.FormValue(field.Name); v != "" {
- u.Password = v
- u.EncryptPassword()
- }
- return nil
- })
- // @snippet_end
- }
|