Example #1
0
func LoginUcenterCallback(c *middleware.Context) {
	token := c.QueryStrings("token")
	if len(token) == 0 {
		c.Handle(500, "ucenter api request error", errors.New("token params error"))
		return
	}

	uclient := ucenter.NewClient(setting.Ucenter.Api_Url, setting.Ucenter.Api_Key, setting.Ucenter.Api_Secret)

	if uid, err := service.CheckToken(uclient, strings.Join(token, "")); err != nil {
		c.Handle(500, "ucenter api request error", err)
		return
	} else {
		t := reflect.TypeOf(uid)
		switch t.Kind() {
		case reflect.Bool:
			c.Handle(500, "ucenter api request error", errors.New("token is expired or invaild"))
			return
		case reflect.Float64:
			var uidFloat float64 = uid.(float64)
			var uidInt int64 = int64(uidFloat)
			if user, err := service.GetUserById(uclient, service.Uid(uidInt)); err != nil {
				c.Handle(500, "ucenter api request error", err)
				return
			} else {
				//都是登录状态了
				//首先查询下用户是否在数据库内
			queryUser:
				userQuery := m.GetUserByLoginQuery{LoginOrEmail: user.Name}
				err := bus.Dispatch(&userQuery)

				if err != nil {
					//如果用户不存在,则插入数据
					cmd := m.CreateUserCommand{}
					cmd.Login = user.Name
					cmd.Email = user.Email
					cmd.Password = setting.AdminPassword //暂用管理员密码吧
					cmd.IsAdmin = false

					if err := bus.Dispatch(&cmd); err != nil {
						log.Error(3, "Failed to create user"+user.Name, err)
						return
					}

					log.Info("Created user: %v", user.Name)
					goto queryUser
				}

				userModel := userQuery.Result
				//记录状态
				loginUserWithUser(userModel, c)
				//跳转页面
				c.Redirect(setting.AppSubUrl + "/")
			}
		}
	}

}
Example #2
0
func checkUcenterLogout(c *middleware.Context) {
	if !setting.Ucenter.Enabled {
		return
	}
	uclient := ucenter.NewClient(setting.Ucenter.Api_Url, setting.Ucenter.Api_Key, setting.Ucenter.Api_Secret)
	callbackUrl := baseurl.BaseUrl(c.Req.Request) + "/login"
	logoutUrl, err := service.LogoutUrl(uclient, callbackUrl)

	if err != nil {
		c.Handle(500, "logoutUrl get failed", err)
		return
	}
	c.Redirect(logoutUrl.String())
}