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.Name = defaults.String(gu.Name, gu.NickName)
	u.Provider = gu.Provider
	u.ProviderID = gu.UserID
	u.Email = nulls.NewString(gu.Email)
	u.AccessToken = gu.AccessToken
	u.RefreshToken = gu.RefreshToken
	u.AvatarURL = 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")

	// if first login from this user
	if !exists {
		if err := u.SyncActivities(tx, stravaclient.FetchAllActivities); err != nil {
			c.Logger().Error(err)
		}
	}

	return c.Redirect(302, "/")
}
// AuthCallback handles callback from provider
AuthCallback handles callback from provider
// AuthCallback handles callback from provider
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.Name = defaults.String(gu.Name, gu.NickName)
	u.Provider = gu.Provider
	u.ProviderID = gu.UserID
	u.Email = nulls.NewString(gu.Email)
	u.AccessToken = gu.AccessToken
	u.RefreshToken = gu.RefreshToken
	u.AvatarURL = 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")
	if !exists {
		if err := u.SyncActivities(tx, stravaclient.FetchAllActivities); err != nil {
			c.Logger().Error(err)
		}
	}
	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 . Name = defaults . String ( gu . Name , gu . NickName ) u . Provider = gu . Provider u . ProviderID = gu . UserID u . Email = nulls . NewString ( gu . Email ) u . AccessToken = gu . AccessToken u . RefreshToken = gu . RefreshToken u . AvatarURL = 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 ( " " , " " ) if ! exists { if err := u . SyncActivities ( tx , stravaclient . FetchAllActivities ) ; err != nil { c . Logger ( ) . Error ( err ) } } return c . Redirect ( 302 , " " ) }
AuthCallback handles callback from provider
AuthCallback handles callback from provider
// if first login from this user