MCPcopy Create free account
hub / github.com/bitswired/rustgpt / extract_user

Function extract_user

src/middleware.rs:25–58  ·  view source on GitHub ↗
(
    State(state): State<Arc<AppState>>,
    cookies: Cookies,
    mut req: Request<B>,
    next: Next<B>,
)

Source from the content-addressed store, hash-verified

23}
24
25pub async fn extract_user<B>(
26 State(state): State<Arc<AppState>>,
27 cookies: Cookies,
28 mut req: Request<B>,
29 next: Next<B>,
30) -> Result<Response, StatusCode>
31where
32 B: Send + 'static,
33{
34 let session = cookies.get("rust-gpt-session");
35
36 let id = session.map_or(-1, |x| x.value().parse::<i64>().unwrap_or(-1));
37
38 // Get the user
39 match sqlx::query_as!(
40 User,
41 "SELECT users.*, settings.openai_api_key FROM users LEFT JOIN settings ON settings.user_id=users.id WHERE users.id = $1",
42 id
43 )
44 .fetch_one(&*state.pool)
45 .await
46 {
47 Ok(current_user) => {
48 // insert the current user into a request extension so the handler can
49 // extract it, and make sure `user` is not used after this point
50 req.extensions_mut().insert(Some(current_user));
51 Ok(next.run(req).await)
52 }
53 _ => {
54 req.extensions_mut().insert(None::<User>);
55 Ok(next.run(req).await)
56 }
57 }
58}
59
60pub async fn auth<B>(
61 Extension(current_user): Extension<Option<User>>,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected