1

I'm using dotnet add package supabase-csharp as a c# client. I'm signed in var session = await supabase.Auth.SignIn(email, password); successfully, but when i tried to get the current user or session it get back as null:

var currentSession = _supabase.Auth.CurrentSession;
var currentUser = _supabase.Auth.CurrentUser;

The configuration:

builder.Services.AddScoped<Supabase.Client>(_ =>

new Supabase.Client(
    builder.Configuration["SUPABASE_URL"],
    builder.Configuration["SUPABASE_KEY"],
    new SupabaseOptions
    {
        AutoRefreshToken = true,
        AutoConnectRealtime = true,
    }));

Any hints related to this?

3
  • Instead of accessing them directly after login, try fetching them after login is complete. var session = await _supabase.Auth.SignIn(email, password); if (session != null) { var currentSession = _supabase.Auth.CurrentSession; var currentUser = _supabase.Auth.CurrentUser; } Commented Mar 5, 2024 at 11:05
  • 1
    The idea is that i have separated endpoints for each action: One controller for : signup, signin, signout. And another controller for : currentsession, currentuser, update. Sample: [HttpGet("currentuser")] public IActionResult GetCurrentUser() { try { var currentUser = _supabase.Auth.CurrentUser; return Ok(currentUser); } catch (Exception ex) { return BadRequest(new { Error = $"Error getting current user: {ex.Message}" }); } } Commented Mar 5, 2024 at 12:45
  • the problem is AddScoped, so when the request ends and you request a new one, you have lost authentication. Commented May 3, 2024 at 9:01

1 Answer 1

0

You can use the _supabase.Auth.GetUser(jwt) method to retrieve the current user with their token

// get JWT token from request header
var token = _httpContextAccessor.HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
var user = await _supabase.Auth.GetUser(token);

With this you can access the signed in User 's object

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.