Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login screen #685

Merged
merged 21 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions example/admin/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ func initLoginSessionBuilder(db *gorm.DB, pb *presets.Builder, ab *activity.Buil
Goth: google.New(loginGoogleKey, loginGoogleSecret, baseURL+"/auth/callback?provider="+models.OAuthProviderGoogle),
Key: models.OAuthProviderGoogle,
Text: "LoginProviderGoogleText",
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>`),
Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="24px" height="24px"><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>`),
},
&login.Provider{
Goth: microsoftonline.New(loginMicrosoftOnlineKey, loginMicrosoftOnlineSecret, baseURL+"/auth/callback"),
Key: models.OAuthProviderMicrosoftOnline,
Text: "LoginProviderMicrosoftText",
Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="16px" height="16px"><path fill="#f35325" d="M2 2h20v20H2z"/><path fill="#81bc06" d="M24 2h20v20H24z"/><path fill="#05a6f0" d="M2 24h20v20H2z"/><path fill="#ffba08" d="M24 24h20v20H24z"/></svg>`),
Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48" width="24px" height="24px"><path fill="#f35325" d="M2 2h20v20H2z"/><path fill="#81bc06" d="M24 2h20v20H24z"/><path fill="#05a6f0" d="M2 24h20v20H2z"/><path fill="#ffba08" d="M24 24h20v20H24z"/></svg>`),
},
&login.Provider{
Goth: github.New(loginGithubKey, loginGithubSecret, baseURL+"/auth/callback?provider="+models.OAuthProviderGithub),
Key: models.OAuthProviderGithub,
Text: "LoginProviderGithubText",
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>`),
Logo: RawHTML(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="24px" height="24px"><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>`),
},
).
HomeURLFunc(func(r *http.Request, user interface{}) string {
Expand Down Expand Up @@ -144,8 +144,7 @@ func initLoginSessionBuilder(db *gorm.DB, pb *presets.Builder, ab *activity.Buil
return nil
}
}).TOTP(false).MaxRetryCount(0)

loginBuilder.LoginPageFunc(loginPage(loginBuilder.ViewHelper(), pb))
loginBuilder.LoginPageFunc(plogin.NewAdvancedLoginPage(nil)(loginBuilder.ViewHelper(), pb))

genInitialUser(db)

Expand Down
4 changes: 4 additions & 0 deletions example/admin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (c *Config) GetPresetsBuilder() *presets.Builder {
return c.pb
}

func (c *Config) GetLoginSessionBuilder() *plogin.SessionBuilder {
return c.loginSessionBuilder
}

var (
s3Bucket = osenv.Get("S3_Bucket", "s3-bucket for media library storage", "example")
s3Region = osenv.Get("S3_Region", "s3-region for media library storage", "ap-northeast-1")
Expand Down
161 changes: 0 additions & 161 deletions example/admin/login_views.go

This file was deleted.

4 changes: 2 additions & 2 deletions example/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ func TestHandlerWorker(db *gorm.DB, u *models.User) http.Handler {
return mux
}

func TestL18nHandler(db *gorm.DB) http.Handler {
func TestL18nHandler(db *gorm.DB) (http.Handler, Config) {
mux := http.NewServeMux()
c := NewConfig(db, false)
c.loginSessionBuilder.Secret("test")
c.loginSessionBuilder.Mount(mux)
mux.Handle("/", c.pb)
return mux
return mux, c
}

func Router(db *gorm.DB) http.Handler {
Expand Down
2 changes: 1 addition & 1 deletion example/integration/l18n_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestL18n(t *testing.T) {
h := admin.TestL18nHandler(TestDB)
h, _ := admin.TestL18nHandler(TestDB)

dbr, _ := TestDB.DB()
profileData.TruncatePut(dbr)
Expand Down
90 changes: 90 additions & 0 deletions example/integration/login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package integration_test

import (
"net/http"
"testing"

"github.com/qor5/web/v3"
"github.com/qor5/web/v3/multipartestutils"

"github.com/qor5/admin/v3/example/admin"
plogin "github.com/qor5/admin/v3/login"
)

func TestLogin(t *testing.T) {
h, _ := admin.TestL18nHandler(TestDB)

dbr, _ := TestDB.DB()
profileData.TruncatePut(dbr)

cases := []multipartestutils.TestCase{
{
Name: "view by en",
Debug: true,
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "en")
return req
},
ExpectPageBodyContainsInOrder: []string{`Welcome`, `Qor Admin System`, `Email`, `Password`, `Sign in`, `Forget your password?`},
},
{
Name: "view by zh",
Debug: true,
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "zh")
return req
},
ExpectPageBodyContainsInOrder: []string{`欢迎`, `Qor 管理系统`, `邮箱`, `密码`, `登录`, `忘记密码?`},
},
{
Name: "view by ja",
Debug: true,
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "ja")
return req
},
ExpectPageBodyContainsInOrder: []string{`ようこそ`, `Qor 管理システム`, `メールアドレス`, `パスワード`, `サインイン`, `パスワードをお忘れですか?`},
},
{
Name: "view by en (customized)",
Debug: true,
HandlerMaker: func() http.Handler {
mux := http.NewServeMux()
c := admin.NewConfig(TestDB, false)
loginSessionBuilder := c.GetLoginSessionBuilder()
loginBuilder := c.GetLoginSessionBuilder().GetLoginBuilder()
loginBuilder.LoginPageFunc(plogin.NewAdvancedLoginPage(func(ctx *web.EventContext, config *plogin.AdvancedLoginPageConfig) (*plogin.AdvancedLoginPageConfig, error) {
config.WelcomeLabel = "Hello"
return config, nil
})(loginBuilder.ViewHelper(), c.GetPresetsBuilder()))
loginSessionBuilder.Secret("test")
loginSessionBuilder.Mount(mux)
mux.Handle("/", c.GetPresetsBuilder())
return mux
},
ReqFunc: func() *http.Request {
req := multipartestutils.NewMultipartBuilder().
PageURL("/auth/login").
BuildEventFuncRequest()
req.Header.Add("accept-language", "en")
return req
},
ExpectPageBodyContainsInOrder: []string{`Hello`, `Qor Admin System`, `Email`, `Password`, `Sign in`, `Forget your password?`},
},
}

for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
multipartestutils.RunCase(t, c, h)
})
}
}
Loading
Loading