main.go 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "github.com/markbates/goth/providers/github"
  8. "github.com/markbates/goth/providers/google"
  9. plogin "github.com/qor5/admin/login"
  10. "github.com/qor5/admin/presets"
  11. "github.com/qor5/web"
  12. "github.com/qor5/x/login"
  13. . "github.com/theplant/htmlgo"
  14. h "github.com/theplant/htmlgo"
  15. "github.com/theplant/testingutils"
  16. "gorm.io/driver/sqlite"
  17. "gorm.io/gorm"
  18. )
  19. type User struct {
  20. gorm.Model
  21. login.UserPass
  22. login.OAuthInfo
  23. login.SessionSecure
  24. }
  25. var (
  26. db *gorm.DB
  27. )
  28. func init() {
  29. var err error
  30. db, err = gorm.Open(sqlite.Open("/tmp/login_example.db"), &gorm.Config{
  31. DisableForeignKeyConstraintWhenMigrating: true,
  32. })
  33. if err != nil {
  34. panic(err)
  35. }
  36. if err := db.AutoMigrate(&User{}); err != nil {
  37. panic(err)
  38. }
  39. user := &User{
  40. UserPass: login.UserPass{
  41. Account: "qor@theplant.jp",
  42. Password: "123",
  43. },
  44. }
  45. user.EncryptPassword()
  46. db.Create(user)
  47. }
  48. func main() {
  49. pb := presets.New()
  50. lb := plogin.New(pb).
  51. DB(db).
  52. UserModel(&User{}).
  53. Secret("123").
  54. OAuthProviders(
  55. &login.Provider{
  56. Goth: google.New(os.Getenv("LOGIN_GOOGLE_KEY"), os.Getenv("LOGIN_GOOGLE_SECRET"), "http://localhost:9500/auth/callback?provider=google"),
  57. Key: "google",
  58. Text: "Login with Google",
  59. Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="16px" height="16px"><path fill="#fbc02d" d="M43.611,20.083H42V20H24v8h11.303c-1.649,4.657-6.08,8-11.303,8c-6.627,0-12-5.373-12-12 s5.373-12,12-12c3.059,0,5.842,1.154,7.961,3.039l5.657-5.657C34.046,6.053,29.268,4,24,4C12.955,4,4,12.955,4,24s8.955,20,20,20 s20-8.955,20-20C44,22.659,43.862,21.35,43.611,20.083z"></path><path fill="#e53935" d="M6.306,14.691l6.571,4.819C14.655,15.108,18.961,12,24,12c3.059,0,5.842,1.154,7.961,3.039 l5.657-5.657C34.046,6.053,29.268,4,24,4C16.318,4,9.656,8.337,6.306,14.691z"></path><path fill="#4caf50" d="M24,44c5.166,0,9.86-1.977,13.409-5.192l-6.19-5.238C29.211,35.091,26.715,36,24,36 c-5.202,0-9.619-3.317-11.283-7.946l-6.522,5.025C9.505,39.556,16.227,44,24,44z"></path><path fill="#1565c0" d="M43.611,20.083L43.595,20L42,20H24v8h11.303c-0.792,2.237-2.231,4.166-4.087,5.571 c0.001-0.001,0.002-0.001,0.003-0.002l6.19,5.238C36.971,39.205,44,34,44,24C44,22.659,43.862,21.35,43.611,20.083z"></path></svg>`),
  60. },
  61. &login.Provider{
  62. Goth: github.New(os.Getenv("LOGIN_GITHUB_KEY"), os.Getenv("LOGIN_GITHUB_SECRET"), "http://localhost/auth/callback?provider=github"),
  63. Key: "github",
  64. Text: "Login with Github",
  65. Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="16px" height="16px"><path fill-rule="evenodd" clip-rule="evenodd" d="M48.854 0C21.839 0 0 22 0 49.217c0 21.756 13.993 40.172 33.405 46.69 2.427.49 3.316-1.059 3.316-2.362 0-1.141-.08-5.052-.08-9.127-13.59 2.934-16.42-5.867-16.42-5.867-2.184-5.704-5.42-7.17-5.42-7.17-4.448-3.015.324-3.015.324-3.015 4.934.326 7.523 5.052 7.523 5.052 4.367 7.496 11.404 5.378 14.235 4.074.404-3.178 1.699-5.378 3.074-6.6-10.839-1.141-22.243-5.378-22.243-24.283 0-5.378 1.94-9.778 5.014-13.2-.485-1.222-2.184-6.275.486-13.038 0 0 4.125-1.304 13.426 5.052a46.97 46.97 0 0 1 12.214-1.63c4.125 0 8.33.571 12.213 1.63 9.302-6.356 13.427-5.052 13.427-5.052 2.67 6.763.97 11.816.485 13.038 3.155 3.422 5.015 7.822 5.015 13.2 0 18.905-11.404 23.06-22.324 24.283 1.78 1.548 3.316 4.481 3.316 9.126 0 6.6-.08 11.897-.08 13.526 0 1.304.89 2.853 3.316 2.364 19.412-6.52 33.405-24.935 33.405-46.691C97.707 22 75.788 0 48.854 0z" fill="#24292f"/></svg>`),
  66. },
  67. ).
  68. AfterConfirmSendResetPasswordLink(func(r *http.Request, user interface{}, extraVals ...interface{}) error {
  69. resetLink := extraVals[0]
  70. fmt.Println("#########################################start")
  71. testingutils.PrintlnJson(resetLink)
  72. fmt.Println("#########################################end")
  73. return nil
  74. }).
  75. TOTP(false)
  76. pb.ProfileFunc(func(ctx *web.EventContext) h.HTMLComponent {
  77. return h.A(h.Text("logout")).Href(lb.LogoutURL)
  78. })
  79. r := http.NewServeMux()
  80. r.Handle("/", pb)
  81. lb.Mount(r)
  82. mux := http.NewServeMux()
  83. mux.Handle("/", lb.Middleware()(r))
  84. log.Println("serving at http://localhost:9500")
  85. log.Fatal(http.ListenAndServe(":9500", mux))
  86. }