Esempio n. 1
0
func (r *JsonResult) Write(ctx framework.WebContext) error {
	data, err := json.Marshal(r.Value)

	if err != nil {
		return err
	}

	ctx.WriteHeader(http.StatusOK)

	callback, _ := ctx.Param("callback")

	if callback == "" {
		//json
		ctx.ContentType("application/json")
		_, err = ctx.Write(data)
	} else {
		//jsonp
		ctx.ContentType("application/javascript")
		_, err = ctx.Write([]byte(fmt.Sprintf(jsonpFormat, callback)))
		_, err = ctx.Write(data)
		_, err = ctx.Write(jsonpEnclosing)
	}

	return err
}
// Function that handles the callback from the Google server
func (this *FacebookController) AuthCallback(ctx framework.WebContext) framework.WebResult {
	//Get the code from the response
	code, _ := ctx.Param("code")

	t := &oauth.Transport{Config: this.oauthCfg}

	// Exchange the received code for a token
	t.Exchange(code)

	//now get user data based on the Transport which has the token
	resp, err := t.Client().Get(this.profileInfoURL)

	if err != nil {
		log.Println(err)
		return ctx.FrameworkError(framework.ToError(framework.Error_Web_UnableToAuthenticate, err))
	}

	userprofile := make(map[string]interface{})
	decoder := json.NewDecoder(resp.Body)
	err = decoder.Decode(&userprofile)

	if err != nil {
		log.Println(err)
		return ctx.FrameworkError(framework.ToError(framework.Error_Web_UnableToAuthenticate, err))
	}

	fmt.Println(userprofile)

	email := userprofile["email"].(string)
	name := userprofile["name"].(string)

	userDto := &contracts.User{Email: email, Name: name}
	userCredentialsDto := &contracts.UserAuthCredentials{
		Type:         framework.AuthType_Facebook,
		AccessToken:  t.Token.AccessToken,
		RefreshToken: t.Token.RefreshToken,
		Expiry:       t.Token.Expiry,
	}

	user, err2 := this.userService.SaveWithCredentials(userDto, userCredentialsDto)

	if err2 != nil {
		return ctx.Error(err2)
	}

	ctx.Session().SetUser(&framework.SessionUser{
		UserId:   user.Email,
		AuthType: framework.AuthType_Facebook,
		AuthData: t.AccessToken,
	})

	return ctx.Template(userInfoTemplate, fmt.Sprintf("Name: %s Email: %s", name, email))
}
func (this UserController) GetUser(ctx framework.WebContext) framework.WebResult {

	name, _ := ctx.Param("name")
	email, _ := ctx.Param("email")

	user, err := this.userService.Create(&contracts.User{Email: email, Name: name})

	if err == nil {
		return ctx.Json(user)
	}

	return ctx.Error(err)
}