login.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package example_basics
  2. // @snippet_begin(LoginBasicUsage)
  3. import (
  4. "net/http"
  5. "os"
  6. "github.com/markbates/goth/providers/github"
  7. "github.com/markbates/goth/providers/google"
  8. plogin "github.com/qor5/admin/login"
  9. "github.com/qor5/admin/presets"
  10. . "github.com/qor5/ui/vuetify"
  11. "github.com/qor5/web"
  12. "github.com/qor5/x/login"
  13. . "github.com/theplant/htmlgo"
  14. "gorm.io/gorm"
  15. )
  16. type User struct {
  17. gorm.Model
  18. Name string
  19. Address string
  20. login.UserPass
  21. login.OAuthInfo
  22. login.SessionSecure
  23. }
  24. func serve() {
  25. pb := presets.New()
  26. lb := plogin.New(pb).
  27. DB(DB).
  28. UserModel(&User{}).
  29. Secret(os.Getenv("LOGIN_SECRET")).
  30. OAuthProviders(
  31. &login.Provider{
  32. Goth: google.New(os.Getenv("LOGIN_GOOGLE_KEY"), os.Getenv("LOGIN_GOOGLE_SECRET"), os.Getenv("BASE_URL")+"/auth/callback?provider=google"),
  33. Key: "google",
  34. Text: "Google",
  35. },
  36. &login.Provider{
  37. Goth: github.New(os.Getenv("LOGIN_GITHUB_KEY"), os.Getenv("LOGIN_GITHUB_SECRET"), os.Getenv("BASE_URL")+"/auth/callback?provider=github"),
  38. Key: "github",
  39. Text: "Login with Github",
  40. },
  41. )
  42. pb.ProfileFunc(func(ctx *web.EventContext) HTMLComponent {
  43. return A(Text("logout")).Href(lb.LogoutURL)
  44. })
  45. r := http.NewServeMux()
  46. r.Handle("/", pb)
  47. lb.Mount(r)
  48. mux := http.NewServeMux()
  49. mux.Handle("/", lb.Middleware()(r))
  50. http.ListenAndServe(":8080", nil)
  51. }
  52. // @snippet_end
  53. func loginPieces() {
  54. // @snippet_begin(LoginEnableUserPass)
  55. type User struct {
  56. gorm.Model
  57. login.UserPass
  58. }
  59. // @snippet_end
  60. var loginBuilder *login.Builder
  61. var count int
  62. // @snippet_begin(LoginSetMaxRetryCount)
  63. loginBuilder.MaxRetryCount(count)
  64. // @snippet_end
  65. var enable bool
  66. // @snippet_begin(LoginSetTOTP)
  67. loginBuilder.TOTP(enable, login.TOTPConfig{
  68. Issuer: "Issuer",
  69. })
  70. // @snippet_end
  71. // @snippet_begin(LoginSetRecaptcha)
  72. loginBuilder.Recaptcha(enable, login.RecaptchaConfig{
  73. SiteKey: "SiteKey",
  74. SecretKey: "SecretKey",
  75. })
  76. // @snippet_end
  77. }
  78. func loginPiece2() {
  79. // @snippet_begin(LoginEnableOAuth)
  80. type User struct {
  81. gorm.Model
  82. login.OAuthInfo
  83. }
  84. // @snippet_end
  85. }
  86. func loginPiece3() {
  87. // @snippet_begin(LoginEnableSessionSecure)
  88. type User struct {
  89. gorm.Model
  90. login.UserPass
  91. login.OAuthInfo
  92. login.SessionSecure
  93. }
  94. // @snippet_end
  95. }
  96. func loginPiece4() {
  97. var loginBuilder *login.Builder
  98. // @snippet_begin(LoginCustomizePage)
  99. loginBuilder.LoginPageFunc(func(ctx *web.EventContext) (r web.PageResponse, err error) {
  100. r.Body = Text("This is login page")
  101. return
  102. })
  103. // @snippet_end
  104. var mux *http.ServeMux
  105. var loginPage http.Handler
  106. // @snippet_begin(LoginCustomizePage2)
  107. loginBuilder.LoginPageURL("/custom-login-page")
  108. loginBuilder.MountAPI(mux)
  109. mux.Handle("/custom-login-page", loginPage)
  110. // @snippet_end
  111. }
  112. func loginPiece5() {
  113. // @snippet_begin(LoginOpenChangePasswordDialog)
  114. VBtn("Change Password").OnClick(plogin.OpenChangePasswordDialogEvent)
  115. // @snippet_end
  116. var userModelBuilder *presets.ModelBuilder
  117. // @snippet_begin(LoginChangePasswordInEditing)
  118. userModelBuilder.Editing().Field("Password").
  119. SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
  120. u := obj.(*User)
  121. if v := ctx.R.FormValue(field.Name); v != "" {
  122. u.Password = v
  123. u.EncryptPassword()
  124. }
  125. return nil
  126. })
  127. // @snippet_end
  128. }