func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
	gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)

	if err != nil {
		if err.Error() == "securecookie: the value is too long" {
			log.Error("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
			err = fmt.Errorf("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
		}
		return nil, goth.User{}, err
	}

	user := &models.User{
		LoginName:   gothUser.UserID,
		LoginType:   models.LoginOAuth2,
		LoginSource: loginSource.ID,
	}

	hasUser, err := models.GetUser(user)
	if err != nil {
		return nil, goth.User{}, err
	}

	if hasUser {
		return user, gothUser, nil
	}

	// search in external linked users
	externalLoginUser := &models.ExternalLoginUser{
		ExternalID:    gothUser.UserID,
		LoginSourceID: loginSource.ID,
	}
	hasUser, err = models.GetExternalLogin(externalLoginUser)
	if err != nil {
		return nil, goth.User{}, err
	}
	if hasUser {
		user, err = models.GetUserByID(externalLoginUser.UserID)
		return user, gothUser, err
	}

	// no user found to login
	return nil, gothUser, nil

}
// OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
// login the user
OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
login the user
// OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful // login the user
func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Request, response http.ResponseWriter) (*models.User, goth.User, error) {
	gothUser, err := oauth2.ProviderCallback(loginSource.Name, request, response)
	if err != nil {
		if err.Error() == "securecookie: the value is too long" {
			log.Error("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
			err = fmt.Errorf("OAuth2 Provider %s returned too long a token. Current max: %d. Either increase the [OAuth2] MAX_TOKEN_LENGTH or reduce the information returned from the OAuth2 provider", loginSource.Name, setting.OAuth2.MaxTokenLength)
		}
		return nil, goth.User{}, err
	}
	user := &models.User{
		LoginName:   gothUser.UserID,
		LoginType:   models.LoginOAuth2,
		LoginSource: loginSource.ID,
	}
	hasUser, err := models.GetUser(user)
	if err != nil {
		return nil, goth.User{}, err
	}
	if hasUser {
		return user, gothUser, nil
	}
	externalLoginUser := &models.ExternalLoginUser{
		ExternalID:    gothUser.UserID,
		LoginSourceID: loginSource.ID,
	}
	hasUser, err = models.GetExternalLogin(externalLoginUser)
	if err != nil {
		return nil, goth.User{}, err
	}
	if hasUser {
		user, err = models.GetUserByID(externalLoginUser.UserID)
		return user, gothUser, err
	}
	return nil, gothUser, nil
}
func oAuth2UserLoginCallback ( loginSource * models . LoginSource , request * http . Request , response http . ResponseWriter ) ( * models . User , goth . User , error ) { gothUser , err := oauth2 . ProviderCallback ( loginSource . Name , request , response ) if err != nil { if err . Error ( ) == " " { log . Error ( " " , loginSource . Name , setting . OAuth2 . MaxTokenLength ) err = fmt . Errorf ( " " , loginSource . Name , setting . OAuth2 . MaxTokenLength ) } return nil , goth . User { } , err } user := & models . User { LoginName : gothUser . UserID , LoginType : models . LoginOAuth2 , LoginSource : loginSource . ID , } hasUser , err := models . GetUser ( user ) if err != nil { return nil , goth . User { } , err } if hasUser { return user , gothUser , nil } externalLoginUser := & models . ExternalLoginUser { ExternalID : gothUser . UserID , LoginSourceID : loginSource . ID , } hasUser , err = models . GetExternalLogin ( externalLoginUser ) if err != nil { return nil , goth . User { } , err } if hasUser { user , err = models . GetUserByID ( externalLoginUser . UserID ) return user , gothUser , err } return nil , gothUser , nil }
OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful
login the user
OAuth2UserLoginCallback attempts to handle the callback from the OAuth2 provider and if successful login the user
// search in external linked users // no user found to login