Ejemplo n.º 1
0
func FBLogin(c *gin.Context) {
	// grab the code fragment

	aecontext := appengine.NewContext(c.Request)

	code := c.Query("code")

	fbConfig := FBConfig()

	token, err := fbConfig.Exchange(aecontext, code)

	if err != nil {
		log.Errorf(aecontext, err.Error())
	}

	client := urlfetch.Client(aecontext)
	response, err := client.Get("https://graph.facebook.com/v2.5/me?fields=id,name,email&access_token=" + token.AccessToken)
	log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken))

	// handle err. You need to change this into something more robust
	// such as redirect back to home page with error message
	if err != nil {
		c.String(http.StatusOK, "Error")
	}

	var fb_user FB_User
	log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response))
	Bind(response, &fb_user)
	log.Debugf(aecontext, fmt.Sprintf("token %#v", token))
	user := &auth.User{
		Email:     fb_user.Email,
		Name:      fb_user.Username,
		Token:     *token,
		LoginType: "facebook",
	}

	var redir_url = ""
	if service.VerifyVolunteerRequest(c, user) {
		redir_url = "/volsuccess"
	}

	log.Debugf(aecontext, fmt.Sprintf("user %#v", user))
	if !auth.IsAuthenticated(c) {
		auth.CreateSession(c, user)
	}
	if redir_url == "" {
		auth.Redirect(c, redir_url)
	} else {
		auth.Redirect(c)
	}
}
Ejemplo n.º 2
0
func GoogleLogin(c *gin.Context) {
	// grab the code fragment

	aecontext := appengine.NewContext(c.Request)

	code := c.Query("code")

	googleconf := GoogleConfig()

	token, err := googleconf.Exchange(aecontext, code)

	if err != nil {
		log.Errorf(aecontext, err.Error())
	}

	client := urlfetch.Client(aecontext)
	response, err := client.Get("https://www.googleapis.com/userinfo/v2/me?access_token=" + token.AccessToken)
	log.Debugf(aecontext, fmt.Sprintf("token.AccessToken %#v", token.AccessToken))

	// handle err. You need to change this into something more robust
	// such as redirect back to home page with error message
	if err != nil {
		c.String(http.StatusOK, "Error")
	}

	var goog_user GoogleUser
	log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", response))
	Bind(response, &goog_user)
	log.Debugf(aecontext, fmt.Sprintf("fb_response %#v", goog_user))
	user := &auth.User{
		Email:   goog_user.Email,
		Name:    goog_user.Username,
		Picture: goog_user.PictureUrl,
	}
	log.Debugf(aecontext, fmt.Sprintf("user %#v", user))
	if !auth.IsAuthenticated(c) {
		auth.CreateSession(c, user)
	}
	auth.Redirect(c)
}