Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring of AuthToken #84

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions src/AuthToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
#if !UNITY_5_3_OR_NEWER
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SpacetimeDB
{
public static class AuthToken
{
private static string? settingsPath = null;
private static string? token = null;
private static string? settingsPath;
private static string? token;

private const string PREFIX = "auth_token=";

/// <summary>
/// Initializes the AuthToken class. This must be called before any other methods.
Expand All @@ -36,10 +36,7 @@ public static class AuthToken
/// </summary>
public static void Init(string configFolder = ".spacetime_csharp_sdk", string configFile = "settings.ini", string? configRoot = null)
{
if (configRoot == null)
{
configRoot = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
configRoot ??= Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

if (Environment.GetCommandLineArgs().Any(arg => arg == "--client"))
{
Expand All @@ -52,14 +49,10 @@ public static void Init(string configFolder = ".spacetime_csharp_sdk", string co

if (File.Exists(settingsPath))
{
foreach (string line in File.ReadAllLines(settingsPath))
{
if (line.StartsWith("auth_token="))
{
token = line.Substring("auth_token=".Length);
break;
}
}
token =
File.ReadLines(settingsPath)
.FirstOrDefault(line => line.StartsWith(PREFIX))
?[PREFIX.Length..];
}
}

Expand All @@ -85,29 +78,23 @@ public static string? Token
/// </summary>
public static void SaveToken(string token)
{
if (settingsPath == null)
{
throw new Exception("Token not initialized. Call AuthToken.Init() first.");
}
Directory.CreateDirectory(Path.GetDirectoryName(settingsPath)!);
if (File.Exists(settingsPath))
var newAuthLine = PREFIX + token;
var lines = File.Exists(settingsPath) ? File.ReadAllLines(settingsPath).ToList() : new();
var i = lines.FindIndex(line => line.StartsWith(PREFIX));
if (i >= 0)
{
List<string> lines = File.ReadAllLines(settingsPath).ToList();
bool found = false;
for (int i = 0; i < lines.Count; i++)
{
if (lines[i].StartsWith("auth_token="))
{
lines[i] = "auth_token=" + token;
found = true;
}
}
if (!found)
{
lines.Add("auth_token=" + token);
}
File.WriteAllLines(settingsPath, lines);
lines[i] = newAuthLine;
}
else
{
File.WriteAllText(settingsPath, "auth_token=" + token);
lines.Add(newAuthLine);
}
File.WriteAllLines(settingsPath, lines);
}
}
}
Expand Down
Loading