func LoginUser(url string, username string) (string, errors.ErrorCode) {
	// Find the user in the DB
	user, err := SearchUser(url, username)

	// Check if the user doesn't exist or the DB crashed
	if err != nil {
		if err.Error() == "sql: Rows are closed" {
			return "", errors.UserDoesNotExist
		}
		return "", errors.DBError
	}

	// The user exists, check if the token is still valid
	info, err := GetSiteInfo(url, user.Token)
	if err != nil {
		return "", errors.InternetError
	}
	// Check for Moodle errors
	if info.ErrorCode != "" {
		if info.ErrorCode != "invalidtoken" {
			return "", errors.ConvertMoodleError(info.ErrorCode)
		}

		// Token isn't valid, get a new one
		response, err := RequestAuthentication(url, username, user.Password)
		if err != nil {
			return "", errors.InternetError
		}

		// Save and return the new token
		var token string = response["token"].(string)

		err = SaveToken(url, username, token)
		if err != nil {
			return "", errors.DBError
		}

		return token, errors.NoError
	}

	// Token is still valid
	return user.Token, errors.NoError
}
// LoginUser authenticates a user that already exists in the DB, if the user's
// token is still valid it will login properly, if not it will get a new token
// and login
// It returns the token if it was successful, if not, it returns an error code
LoginUser authenticates a user that already exists in the DB, if the user's
token is still valid it will login properly, if not it will get a new token
and login
It returns the token if it was successful, if not, it returns an error code
// LoginUser authenticates a user that already exists in the DB if the user ' s // token is still valid it will login properly if not it will get a new token // and login // It returns the token if it was successful if not it returns an error code
func LoginUser(url string, username string) (string, errors.ErrorCode) {
	user, err := SearchUser(url, username)
	if err != nil {
		if err.Error() == "sql: Rows are closed" {
			return "", errors.UserDoesNotExist
		}
		return "", errors.DBError
	}
	info, err := GetSiteInfo(url, user.Token)
	if err != nil {
		return "", errors.InternetError
	}
	if info.ErrorCode != "" {
		if info.ErrorCode != "invalidtoken" {
			return "", errors.ConvertMoodleError(info.ErrorCode)
		}
		response, err := RequestAuthentication(url, username, user.Password)
		if err != nil {
			return "", errors.InternetError
		}
		var token string = response["token"].(string)
		err = SaveToken(url, username, token)
		if err != nil {
			return "", errors.DBError
		}
		return token, errors.NoError
	}
	return user.Token, errors.NoError
}
func LoginUser ( url string , username string ) ( string , errors . ErrorCode ) { user , err := SearchUser ( url , username ) if err != nil { if err . Error ( ) == " " { return " " , errors . UserDoesNotExist } return " " , errors . DBError } info , err := GetSiteInfo ( url , user . Token ) if err != nil { return " " , errors . InternetError } if info . ErrorCode != " " { if info . ErrorCode != " " { return " " , errors . ConvertMoodleError ( info . ErrorCode ) } response , err := RequestAuthentication ( url , username , user . Password ) if err != nil { return " " , errors . InternetError } var token string = response [ " " ] . ( string ) err = SaveToken ( url , username , token ) if err != nil { return " " , errors . DBError } return token , errors . NoError } return user . Token , errors . NoError }
LoginUser authenticates a user that already exists in the DB, if the user's
token is still valid it will login properly, if not it will get a new token
and login
It returns the token if it was successful, if not, it returns an error code
LoginUser authenticates a user that already exists in the DB if the user ' s token is still valid it will login properly if not it will get a new token and login It returns the token if it was successful if not it returns an error code
// Find the user in the DB // Check if the user doesn't exist or the DB crashed // The user exists, check if the token is still valid // Check for Moodle errors // Token isn't valid, get a new one // Save and return the new token // Token is still valid