/gotrue-csharp

C# implementation of Supabase's GoTrue

Primary LanguageC#MIT LicenseMIT


Getting Started

The Gotrue Client from this library uses a Singleton class to maintain in-memory state and timers. This is similar to the method that Firebase uses in its Client libraries. It can maintain sessions, refresh tokens, persistence, etc. for the developer, rather than having to do that yourself.

Alternatively, a StatelessClient is also provided within this library that allows interactions directly with the API without requiring initialization.

var options = new ClientOptions { Url = "https://example.com/api" };
var client = await Client.Initialize(options);

var user = await SignUp("new-user@example.com");

// Alternatively, you can use a StatelessClient and do API interactions that way
var options = new StatelessClientOptions { Url = "https://example.com/api" }
await Client.SignUp("new-user@example.com", options);

Persisting, Retrieving, and Destroying Sessions.

This Gotrue client is written to be agnostic when it comes to session persistance, retrieval, and destruction. ClientOptions exposes properties that allow these to be specified.

In the event these are specified and the AutoRefreshToken option is set, as the Client Initializes, it will also attempt to retrieve, set, and refresh an existing session.

For example, using Xamarin.Essentials in Xamarin.Forms, this might look like:

var cacheFileName = ".gotrue.cache";

async void Initialize() {
    var options = new ClientOptions
    {
        Url = GOTRUE_URL,
        SessionPersistor = SessionPersistor,
        SessionRetriever = SessionRetriever,
        SessionDestroyer = SessionDestroyer
    };
    await Client.Initialize(options);
}

//...

internal Task<bool> SessionPersistor(Session session)
{
    try
    {
        var cacheDir = FileSystem.CacheDirectory;
        var path = Path.Join(cacheDir, cacheFileName);
        var str = JsonConvert.SerializeObject(session);

        using (StreamWriter file = new StreamWriter(path))
        {
            file.Write(str);
            file.Dispose();
            return Task.FromResult(true);
        };
    }
    catch (Exception err)
    {
        Debug.WriteLine("Unable to write cache file.");
        throw err;
    }
}

3rd Party Authentication and Callbacks.

Once again, Gotrue client is written to be agnostic of platform. In order for Gotrue to sign in a user from an Oauth callback:

  1. The Callback Url must be set in the Supabase Admin panel
  2. The Application should recieve that Callback
  3. In the Callback, the Uri should be passed to Client.Instance.GetSessionFromUrl(uri)

Setting the second parameter of GetSessionFromUrl to false will prevent the storage of the parsed Session object.

Troubleshooting

I've created a User but while attempting to log in it throws an exception:

Provided the credentials are correct, make sure that the User has also confirmed their email.

Status

  • API
    • Sign Up with Email
    • Sign In with Email
    • Send Magic Link Email
    • Invite User by Email
    • Reset Password for Email
    • Signout
    • Get Url for Provider
    • Get User
    • Update User
    • Refresh Access Token
    • List Users (includes filtering, sorting, pagination)
    • Get User by Id
    • Create User
    • Update User by Id
  • Client
    • Get User
    • Refresh Session
    • Auth State Change Handler
    • Provider Sign In (Provides URL)
  • Provide Interfaces for Custom Token Persistence Functionality
  • Documentation
  • Unit Tests
  • Nuget Release

Package made possible through the efforts of:

Join the ranks! See a problem? Help fix it!

Made with contrib.rocks.

Contributing

We are more than happy to have contributions! Please submit a PR.

Testing

To run the tests locally you must have docker and docker-compose installed. Then in the root of the repository run:

  • docker-compose up -d
  • dotnet test