Skip to content

Commit

Permalink
Merge pull request goplus#21 from wu-jj/feat/mvp-20240119
Browse files Browse the repository at this point in the history
refactor login service
  • Loading branch information
IRONICBo authored and wu-jj committed Jan 28, 2024
2 parents bfe0b72 + 01a0087 commit 270310c
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 114 deletions.
53 changes: 51 additions & 2 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ logger, _ := zap.NewProduction()
defer logger.Sync()
zlog := logger.Sugar()

static "/"
// static "/"

get "/success", ctx => {
ctx.yap "2xx", {}
Expand Down Expand Up @@ -355,10 +355,59 @@ post "/upload", ctx => {
ctx.JSON(200,id)
}

get "/login", ctx => {
// Get URL from query string
redirectURL := ctx.URL.Query().Get("redirect_url")
loginURL := community.RedirectToCasdoor(redirectURL)
ctx.Redirect loginURL, http.StatusFound
}


get "/callback", ctx => {
code :=ctx.URL.Query().Get("code")
state :=ctx.URL.Query().Get("state")

token, error := community.GetAccessToken(code, state)
if error != nil {
zlog.Error("err",error) // Redirect to login
}

cookie := http.Cookie{
Name: "token",
Value: token.AccessToken,
Path: "/",
MaxAge: 3600,
}
http.SetCookie(ctx.ResponseWriter, &cookie)

// Redirect to home page
// TODO: Get redirect URL from state
http.Redirect(ctx.ResponseWriter, ctx.Request, fmt.Sprintf("http://localhost:8080?token=%s", token.AccessToken), http.StatusFound)
}

conf := &core.Config{}
community, _ = core.New(todo, conf)
trans = translation.New(os.Getenv("NIUTRANS_API_KEY"), "", "")
core.CasdoorConfigInit()

// 404
handle "/",ctx => {
ctx.yap "4xx", {}
}

zlog.Info "Started in endpoint: ", endpoint
run endpoint
// run endpoint

// 500
run(endpoint, func(h http.Handler) http.Handler {

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
http.Redirect(w, r, "/failed", http.StatusFound)
}
}()

h.ServeHTTP(w, r)
})
})
Loading

0 comments on commit 270310c

Please sign in to comment.