Add HandleLoggedIn().

This commit is contained in:
Yang Luo 2021-03-15 19:11:41 +08:00
parent 4a170d1d56
commit 0ce7420907
3 changed files with 8 additions and 16 deletions

View File

@ -109,9 +109,7 @@ func (c *ApiController) Login() {
if msg != "" { if msg != "" {
resp = Response{Status: "error", Msg: msg, Data: ""} resp = Response{Status: "error", Msg: msg, Data: ""}
} else { } else {
c.SetSessionUser(userId) c.HandleLoggedIn(userId)
util.LogInfo(c.Ctx, "API: [%s] logged in", userId)
resp = Response{Status: "ok", Msg: "", Data: userId} resp = Response{Status: "ok", Msg: "", Data: userId}
} }

View File

@ -26,6 +26,11 @@ import (
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
func (c *ApiController) HandleLoggedIn(userId string) {
c.SetSessionUser(userId)
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
}
func (c *ApiController) AuthLogin() { func (c *ApiController) AuthLogin() {
var form RegisterForm var form RegisterForm
err := json.Unmarshal(c.Ctx.Input.RequestBody, &form) err := json.Unmarshal(c.Ctx.Input.RequestBody, &form)
@ -44,10 +49,8 @@ func (c *ApiController) AuthLogin() {
var resp Response var resp Response
var res authResponse var res authResponse
res.IsAuthenticated = true
if form.State != beego.AppConfig.String("AuthState") { if form.State != beego.AppConfig.String("AuthState") {
res.IsAuthenticated = false
resp = Response{Status: "error", Msg: "unauthorized", Data: res} resp = Response{Status: "error", Msg: "unauthorized", Data: res}
c.ServeJSON() c.ServeJSON()
return return
@ -57,7 +60,6 @@ func (c *ApiController) AuthLogin() {
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, httpClient) ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, httpClient)
token, err := oauthConfig.Exchange(ctx, form.Code) token, err := oauthConfig.Exchange(ctx, form.Code)
if err != nil { if err != nil {
res.IsAuthenticated = false
panic(err) panic(err)
} }
@ -95,9 +97,7 @@ func (c *ApiController) AuthLogin() {
// object.LinkMemberAccount(userId, "avatar", avatar) // object.LinkMemberAccount(userId, "avatar", avatar)
//} //}
c.SetSessionUser(userId) c.HandleLoggedIn(userId)
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
res.IsSignedUp = true
} else { } else {
//if object.IsForbidden(userId) { //if object.IsForbidden(userId) {
// c.forbiddenAccountResp(userId) // c.forbiddenAccountResp(userId)
@ -105,17 +105,13 @@ func (c *ApiController) AuthLogin() {
//} //}
if userId := object.GetUserIdByField(application, "email", res.Email); userId != "" { if userId := object.GetUserIdByField(application, "email", res.Email); userId != "" {
c.SetSessionUser(userId) c.HandleLoggedIn(userId)
util.LogInfo(c.Ctx, "API: [%s] signed in", userId)
res.IsSignedUp = true
if provider.Type == "github" { if provider.Type == "github" {
_ = object.LinkUserAccount(userId, "github", res.Method) _ = object.LinkUserAccount(userId, "github", res.Method)
} else if provider.Type == "google" { } else if provider.Type == "google" {
_ = object.LinkUserAccount(userId, "google", res.Email) _ = object.LinkUserAccount(userId, "google", res.Email)
} }
} else {
res.IsSignedUp = false
} }
} }
//res.Method = res.Email //res.Method = res.Email

View File

@ -15,8 +15,6 @@
package controllers package controllers
type authResponse struct { type authResponse struct {
IsAuthenticated bool `json:"isAuthenticated"`
IsSignedUp bool `json:"isSignedUp"`
Email string `json:"email"` Email string `json:"email"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`
Method string `json:"method"` Method string `json:"method"`