// AuthCallback returned after goth finishes user authentication
// saves new user and profile to database
AuthCallback returned after goth finishes user authentication
saves new user and profile to database
// AuthCallback returned after goth finishes user authentication // saves new user and profile to database
func AuthCallback(c buffalo.Context) error {
	gu, err := gothic.CompleteUserAuth(c.Response(), c.Request())
	if err != nil {
		return c.Error(401, err)
	}
	tx := c.Value("tx").(*pop.Connection)
	q := tx.Where("provider = ? and provider_id = ?", gu.Provider, gu.UserID)
	exists, err := q.Exists("users")
	if err != nil {
		return errors.WithStack(err)
	}
	u := &models.User{}
	if exists {
		if err = q.First(u); err != nil {
			return errors.WithStack(err)
		}
	}
	u.Provider = gu.Provider
	u.ProviderID = gu.UserID
	u.Name = defaults.String(gu.Name, gu.NickName)
	u.Email = nulls.NewString(gu.Email)
	u.Gravatar = nulls.NewString(gu.AvatarURL)
	if err = tx.Save(u); err != nil {
		return errors.WithStack(err)
	}
	c.Session().Set("current_user_id", u.ID)
	if err = c.Session().Save(); err != nil {
		return errors.WithStack(err)
	}
	c.Flash().Add("success", "You have been logged in")
	return c.Redirect(302, "/")
}
func AuthCallback ( c buffalo . Context ) error { gu , err := gothic . CompleteUserAuth ( c . Response ( ) , c . Request ( ) ) if err != nil { return c . Error ( 401 , err ) } tx := c . Value ( " " ) . ( * pop . Connection ) q := tx . Where ( " " , gu . Provider , gu . UserID ) exists , err := q . Exists ( " " ) if err != nil { return errors . WithStack ( err ) } u := & models . User { } if exists { if err = q . First ( u ) ; err != nil { return errors . WithStack ( err ) } } u . Provider = gu . Provider u . ProviderID = gu . UserID u . Name = defaults . String ( gu . Name , gu . NickName ) u . Email = nulls . NewString ( gu . Email ) u . Gravatar = nulls . NewString ( gu . AvatarURL ) if err = tx . Save ( u ) ; err != nil { return errors . WithStack ( err ) } c . Session ( ) . Set ( " " , u . ID ) if err = c . Session ( ) . Save ( ) ; err != nil { return errors . WithStack ( err ) } c . Flash ( ) . Add ( " " , " " ) return c . Redirect ( 302 , " " ) }
AuthCallback returned after goth finishes user authentication
saves new user and profile to database
AuthCallback returned after goth finishes user authentication saves new user and profile to database
//TODO: save data to user profiles after creating user // a user should not be created if there is already an user profile // for the given user for given email. Everything should get tied to // and email