當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Session.Flashes方法代碼示例

本文整理匯總了Golang中github.com/martini-contrib/sessions.Session.Flashes方法的典型用法代碼示例。如果您正苦於以下問題:Golang Session.Flashes方法的具體用法?Golang Session.Flashes怎麽用?Golang Session.Flashes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/martini-contrib/sessions.Session的用法示例。


在下文中一共展示了Session.Flashes方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GithubCallback

func GithubCallback(session sessions.Session, r *http.Request, re render.Render) {
	code, state := r.URL.Query().Get("code"), r.URL.Query().Get("state")
	client_id, client_secret := common.Config.OAuth2Client_ID, common.Config.OAuth2Client_Secret

	flashes := session.Flashes("state")
	if code == "" || state == "" || session == nil || len(flashes) == 0 || flashes[0].(string) != state {
		re.Redirect("/")
		return
	}

	client := &http.Client{}

	form := url.Values{}
	form.Add("code", code)
	form.Add("client_id", client_id)
	form.Add("client_secret", client_secret)

	req, _ := http.NewRequest("POST", "https://github.com/login/oauth/access_token/", strings.NewReader(form.Encode()))
	req.Header.Set("Accept", "application/json")

	resp, _ := client.Do(req)
	body, _ := ioutil.ReadAll(resp.Body)
	parse := map[string]interface{}{}
	json.Unmarshal([]byte(string(body)), &parse)

	token := parse["access_token"].(string)

	req, _ = http.NewRequest("GET", "https://api.github.com/user?access_token="+token, nil)

	resp, _ = client.Do(req)
	body, _ = ioutil.ReadAll(resp.Body)
	parse = map[string]interface{}{}
	json.Unmarshal([]byte(string(body)), &parse)

	username, id := parse["login"].(string), strconv.FormatFloat(parse["id"].(float64), 'f', -1, 64)

	if !common.UserExist(id) {
		common.NewUser(id)
	}

	session.Set("loggedin", true)
	session.Set("username", username)
	session.Set("id", id)
	session.Set("token", token)

	redirectTo := session.Get("redirect_to")
	if redirectTo != nil {
		re.Redirect(redirectTo.(string))
	} else {
		re.Redirect("/")
	}
	session.Set("redirect_to", nil)
}
開發者ID:SemQuery,項目名稱:web,代碼行數:53,代碼來源:session.go

示例2: NewPageData

// NewPageData is the constructor for PageData struct
func NewPageData(tokens oauth2.Tokens, session sessions.Session) *PageData {
	pd := &PageData{}

	pd.User = CurrentUser(tokens)

	pd.GravatarImage = fmt.Sprintf(
		"https://secure.gravatar.com/avatar/%x?s=50&d=https%%3A%%2F%%2Fwww.synkee.com%%2Fapp%%2Fstatic%%2Fdist%%2Fimg%%2Fuser.png",
		md5.Sum([]byte(pd.User.Email)))

	pd.AppUrl = config.AppUrl
	pd.Errors = &binding.Errors{}

	pd.SuccessFlash = session.Flashes("success")
	pd.ErrorFlash = session.Flashes("error")

	pd.Config = config

	pd.Settings = &Setting{}
	db.Where(&Setting{UserID: int(pd.User.ID)}).First(pd.Settings)

	// log.Printf("[NewPageData] settings: %s", pd.Settings)

	return pd
}
開發者ID:tpiha,項目名稱:barecv,代碼行數:25,代碼來源:helpers.go


注:本文中的github.com/martini-contrib/sessions.Session.Flashes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。