Retrieve a session
Return the current session, refreshing it if it's about to expire.
Return the current Session for the signed-in user. If the session is within ten seconds of expires_at (the EXPIRY_MARGIN constant), get_session transparently calls the refresh-token endpoint and returns the freshly-rotated session. Returns nil when no session is stored.
When persist_session: true (the default), the session is read from the configured storage backend; otherwise it is read from the in-memory @current_session.
Signature
supabase.auth.get_sessionNo arguments.
Parameters
This method takes no parameters.
Returns
A Struct with :access_token, :refresh_token, :token_type, :expires_in, :expires_at, :provider_token, :provider_refresh_token, and :user. Returns nil when no session is stored, or when stored session data is unparseable (e.g. corrupted storage). If the session has already expired and a refresh attempt fails, Supabase::Auth::Errors::AuthSessionMissing is raised.
Example — read the current session
session = supabase.auth.get_session
if session
puts "Signed in as #{session.user.email}"
puts "Token expires at #{Time.at(session.expires_at)}"
else
puts "Not signed in"
endExample — use the access token in a downstream request
session = supabase.auth.get_session
raise "Not signed in" unless session
Faraday.get(
"https://api.example.com/me",
nil,
{ "Authorization" => "Bearer #{session.access_token}" }
)Example — gate work on an explicit refresh
session = supabase.auth.get_session
# get_session has already refreshed if the token was within 10s of expiry,
# but you can force-refresh by passing the refresh_token to refresh_session:
fresh = supabase.auth.refresh_session(session.refresh_token) if sessionEXPIRY_MARGIN is 10 seconds — refresh fires within that window. Persist-session/in-memory branching is driven by ClientOptions. Fields are accessed with method-style readers (session.access_token).