<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=FastAPI_Sessions</id>
	<title>FastAPI Sessions - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://performiq.com/kb/index.php?action=history&amp;feed=atom&amp;title=FastAPI_Sessions"/>
	<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=FastAPI_Sessions&amp;action=history"/>
	<updated>2026-05-18T19:45:08Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.37.1</generator>
	<entry>
		<id>https://performiq.com/kb/index.php?title=FastAPI_Sessions&amp;diff=5258&amp;oldid=prev</id>
		<title>PeterHarding: Created page with &quot;Not sure where this came from - shoul dhave captured the link.   &lt;pre&gt; # ============================================================================= # Session setup # -----------------------------------------------------------------------------  class SessionData(BaseModel):     username: str  cookie_params = CookieParameters()  # Uses UUID cookie = SessionCookie(     cookie_name=&quot;cookie&quot;,     identifier=&quot;general_verifier&quot;,     auto_error=True,     secret_key=&quot;DONOTUSE...&quot;</title>
		<link rel="alternate" type="text/html" href="https://performiq.com/kb/index.php?title=FastAPI_Sessions&amp;diff=5258&amp;oldid=prev"/>
		<updated>2022-05-07T01:28:40Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Not sure where this came from - shoul dhave captured the link.   &amp;lt;pre&amp;gt; # ============================================================================= # Session setup # -----------------------------------------------------------------------------  class SessionData(BaseModel):     username: str  cookie_params = CookieParameters()  # Uses UUID cookie = SessionCookie(     cookie_name=&amp;quot;cookie&amp;quot;,     identifier=&amp;quot;general_verifier&amp;quot;,     auto_error=True,     secret_key=&amp;quot;DONOTUSE...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Not sure where this came from - shoul dhave captured the link.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# =============================================================================&lt;br /&gt;
# Session setup&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
class SessionData(BaseModel):&lt;br /&gt;
    username: str&lt;br /&gt;
&lt;br /&gt;
cookie_params = CookieParameters()&lt;br /&gt;
&lt;br /&gt;
# Uses UUID&lt;br /&gt;
cookie = SessionCookie(&lt;br /&gt;
    cookie_name=&amp;quot;cookie&amp;quot;,&lt;br /&gt;
    identifier=&amp;quot;general_verifier&amp;quot;,&lt;br /&gt;
    auto_error=True,&lt;br /&gt;
    secret_key=&amp;quot;DONOTUSE&amp;quot;,&lt;br /&gt;
    cookie_params=cookie_params,&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
backend = InMemoryBackend[UUID, SessionData]()&lt;br /&gt;
&lt;br /&gt;
# -----------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
class BasicVerifier(SessionVerifier[UUID, SessionData]):&lt;br /&gt;
    def __init__(&lt;br /&gt;
        self,&lt;br /&gt;
        *,&lt;br /&gt;
        identifier: str,&lt;br /&gt;
        auto_error: bool,&lt;br /&gt;
        backend: InMemoryBackend[UUID, SessionData],&lt;br /&gt;
        auth_http_exception: HTTPException,&lt;br /&gt;
    ):&lt;br /&gt;
        self._identifier = identifier&lt;br /&gt;
        self._auto_error = auto_error&lt;br /&gt;
        self._backend = backend&lt;br /&gt;
        self._auth_http_exception = auth_http_exception&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def identifier(self):&lt;br /&gt;
        return self._identifier&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def backend(self):&lt;br /&gt;
        return self._backend&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def auto_error(self):&lt;br /&gt;
        return self._auto_error&lt;br /&gt;
&lt;br /&gt;
    @property&lt;br /&gt;
    def auth_http_exception(self):&lt;br /&gt;
        return self._auth_http_exception&lt;br /&gt;
&lt;br /&gt;
    def verify_session(self, model: SessionData) -&amp;gt; bool:&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;If the session exists, it is valid&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        return True&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
verifier = BasicVerifier(&lt;br /&gt;
    identifier=&amp;quot;general_verifier&amp;quot;,&lt;br /&gt;
    auto_error=True,&lt;br /&gt;
    backend=backend,&lt;br /&gt;
    auth_http_exception=HTTPException(status_code=403, detail=&amp;quot;invalid session&amp;quot;),&lt;br /&gt;
)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@app.post(&amp;quot;/create_session/{name}&amp;quot;)&lt;br /&gt;
async def create_session(name: str, response: Response):&lt;br /&gt;
&lt;br /&gt;
    session = uuid4()&lt;br /&gt;
    data = SessionData(username=name)&lt;br /&gt;
&lt;br /&gt;
    await backend.create(session, data)&lt;br /&gt;
    cookie.attach_to_response(response, session)&lt;br /&gt;
&lt;br /&gt;
    return f&amp;quot;created session for {name}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@app.get(&amp;quot;/whoami&amp;quot;, dependencies=[Depends(cookie)])&lt;br /&gt;
async def whoami(session_data: SessionData = Depends(verifier)):&lt;br /&gt;
    return session_data&lt;br /&gt;
&lt;br /&gt;
@app.post(&amp;quot;/delete_session&amp;quot;)&lt;br /&gt;
async def del_session(response: Response, session_id: UUID = Depends(cookie)):&lt;br /&gt;
    await backend.delete(session_id)&lt;br /&gt;
    cookie.delete_from_response(response)&lt;br /&gt;
    return &amp;quot;deleted session&amp;quot;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:FastApi]]&lt;br /&gt;
[[Category:Python]]&lt;/div&gt;</summary>
		<author><name>PeterHarding</name></author>
	</entry>
</feed>