0

I'm facing a problem with my backend app, I want to create 3 endpoint API:

  • /login user
  • /logout
  • /getuser I'm using python FastAPI and supabase, the question is how to create session from my backend app to get connected user information to handle th log_out and the log_in.

log_in function endpoint

@app.post('/login')
async def handel_login(user_mail: str):
    if check(user_mail):
        print(supabase.auth.sign_in(email=user_mail))
    else:
        print("incorrect form of email")

log_ou endpoint

@app.get('/logout')
async def handel_logout():
    error = supabase.auth.sign_out()
    return error

getuser endpoint

@app.get('/getuser')
async def get_user():
    user = supabase.auth.user()
    return user

in all of this I get None responses!

2 Answers2

2

Supabase-py maintainer here - thanks for the query! FastAPI uses Starlet under the hood so your probably want to make use of the Session Middleware provided by Starlet.

Thereafter, you can do something similar of this format. This is for Django so you may need to make minor tweaks:

try:
    data = supabase.auth.sign_in(email="generictestemail123now@gmail.com", password="supersecurepassword")
        request.session['user'] = data.json()
except APIError:
    pass

Thereafter, you can check if the user is authenticated by reading from the session and validating the JWT.

Hope this helps and please let me know if there are any further issues.

Additional References

[0] - session object in Fastapi similar to flask

Joel Lee
  • 929
  • 8
  • 17
  • thanks for your answer, it didn't work for me! How can I get all my Users from the supabase with python i use this script "supabase.auth.api.list_users()" but i get None! – hassane JABRI Nov 01 '22 at 11:00
  • Hey @hassaneJABRI, sorry to hear that it's not working for you - let me replicate and get back to you in a second – Joel Lee Nov 01 '22 at 11:46
  • 1
    Hey it seems to work for me. I am able to `sign_up` and view all users via `list_users`. Not sure what's going on - could you confirm that you are using a service role key when initialising a client via `create_client()` and also shoot over an email to support@supabase.io with more details? It's not an official SDK atm but I can take a look – Joel Lee Nov 01 '22 at 13:11
  • 1
    Hey @Joel Lee, Thanks again here is my Code SUPABASE_URL = "https://MY_SUPA.supabase.co" SUPABASE_KEY = "MY_SUPA_KEY" supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) @app.post('/login', tags=['user']) async def handel_login(user: User = Body(default=None)): if check(user.email): user_mail = user.email error = supabase.auth.sign_in(email=user_mail) if not error: return signJWT(user_mail) else: return False else: return False So here the user is normally authenticated, and I get my JWT – hassane JABRI Nov 02 '22 at 16:41
  • But I cant signOut or get my users from the supabase! and for the [supabase.auth.on_auth_state_change()] function, I don't know how to use it? – hassane JABRI Nov 02 '22 at 16:44
1

I was interested in this because I had a next.js app that used supabase cookies for auth. That proved too difficult to pass to the FastApi service. Instead, I extract the access_token via the session, and then just pass it in via authorization header per the fastapi security tutorial.

Michael Eliot
  • 831
  • 8
  • 18