func (this *RegisterController) Post() { this.Forbbiden("not", "guest") user := models.User{} username := this.GetString("username") password := this.GetString("password") email := this.GetString("email") nickname := this.GetString("nickname") id, err := user.Register(username, password, email, nickname) if err != nil { this.Data["json"] = map[string]interface{}{ "result": false, "msg": fmt.Sprintf("%s", err), "refer": nil, } } else { this.Data["json"] = map[string]interface{}{ "result": true, "msg": fmt.Sprintf("successfully registered, id [%d]", id), "refer": nil, } } this.ServeJson() }
func (this *OAuthController) Post() { this.Forbbiden("is", "guest") password := this.GetString("password") token := this.GetString("token") username := this.Data["username"].(string) user := models.User{} oa := models.OAuth{} result, _ := user.Login(username, password) if result { u, err := user.GetUser(0, username, "", "") if err != nil { log.Warnln("get uid failed.") this.Data["json"] = map[string]interface{}{ "result": false, "msg": "get uid failed.", "refer": nil, } } if oa.GithubBind(token, "github", u.Id) { this.Data["json"] = map[string]interface{}{ "result": true, "msg": "binding success", "refer": nil, } } else { this.Data["json"] = map[string]interface{}{ "result": false, "msg": "binding failed", "refer": nil, } } } else { this.Data["json"] = map[string]interface{}{ "result": false, "msg": "wrong password.", "refer": nil, } } this.ServeJson() }
func (this *OAuthController) Get() { code := this.GetString("code") clientId := beego.AppConfig.String("github_client_id") clientSecret := beego.AppConfig.String("github_client_secret") oauthGithub := &oauth.GithubOAuth{} token, json, err := oauthGithub.GetData(clientId, clientSecret, code) if err != nil { this.Ctx.WriteString("Response Error! ") return } else { log.Blueln(json) } user := models.User{} oa := models.OAuth{} if this.Data["userIs"] == "guest" { result, usr := oa.GithubLogin(token, "github") if result { log.Blueln("github login success.") this.SetSession("username", usr.Username) this.SetSession("level", usr.Level) this.Redirect("/", 302) } else { log.Warnln("you have not register or bindding you github account.") this.Redirect("/register", 302) } } else { // have login, binding github oauth data := json.(map[string]interface{}) avatar := data["avatar_url"].(string) username := data["login"].(string) gojAvatar, _ := user.GetAvatar(0, this.Data["username"].(string), "", "") this.Data["title"] = "github绑定确认" this.Data["github_avatar"] = avatar this.Data["goj_avatar"] = gojAvatar this.Data["token"] = token this.Data["github_username"] = username this.TplNames = "user/github_binding_confirm.tpl" } }
// run before get func (this *BaseController) Prepare() { // get user level var lev string stn := time.Now() st := stn.UnixNano() this.Data["start"] = st log.Blueln(this.Ctx.Request.UserAgent()) user := this.GetSession("username") if user == nil { lev = "guest" // guest, not login } else { level := this.GetSession("level") if level == nil { lev = "user" } else { if tmplev, ok := level.(string); !ok { lev = "user" } else { lev = tmplev } } username := user.(string) usr := models.User{} u, err := usr.GetUser(0, username, "", "") if err != nil { this.Data["nickname"] = "" this.Data["email_md5"] = "" } else { this.Data["username"] = username this.Data["nickname"] = u.Nickname this.Data["email_md5"] = com.Md5(u.Email) } } this.Data["userIs"] = lev // log.Pinkln(lev) }
func (this *LoginController) Post() { this.Forbbiden("not", "guest") user := models.User{} username := this.GetString("username") password := this.GetString("password") if result, lev := user.Login(username, password); result { user, err := user.GetUser(0, username, "", "") if err != nil { this.Data["json"] = map[string]interface{}{ "result": false, "msg": "login failed, get user info failed", "refer": nil, } } else { this.SetSession("username", username) this.SetSession("userid", fmt.Sprintf("%d", user.Id)) this.SetSession("level", lev) this.Data["json"] = map[string]interface{}{ "result": true, "msg": "login success", "refer": nil, } } } else { this.Data["json"] = map[string]interface{}{ "result": false, "msg": "login failed", "refer": nil, } } this.ServeJson() }