Skip to content

Commit

Permalink
Update Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Jul 27, 2024
1 parent 9046f03 commit d2b0950
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions engine/migrations/0002_sessions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS sessions
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id INT NOT NULL,
user_agent VARCHAR(255) NOT NULL,
user_ip VARCHAR(255) NOT NULL,
last_access TIMESTAMPTZ NOT NULL DEFAULT NOW(),
valid BOOLEAN NOT NULL DEFAULT TRUE
);
5 changes: 3 additions & 2 deletions engine/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ impl Database {
Ok(user)
}

pub async fn create_session(&self, user_id: i32, user_agent: &str) -> Result<SessionState, sqlx::Error> {
let session = sqlx::query_as::<_, SessionState>("INSERT INTO sessions (user_id, user_agent) VALUES ($1, $2) RETURNING *")
pub async fn create_session(&self, user_id: i32, user_agent: &str, user_ip: &str) -> Result<SessionState, sqlx::Error> {
let session = sqlx::query_as::<_, SessionState>("INSERT INTO sessions (user_id, user_agent, user_ip) VALUES ($1, $2, $3) RETURNING *")
.bind(user_id)
.bind(user_agent)
.bind(user_ip)
.fetch_one(&self.pool)
.await?;
Ok(session)
Expand Down
40 changes: 32 additions & 8 deletions engine/src/routes/auth.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
use crate::state::AppState;
use openid::{Options, Token};
use poem::{handler, web::{cookie::{Cookie, CookieJar}, Data, Json, Query, Redirect}, IntoResponse};
use poem::{
handler,
http::HeaderMap,
web::{
cookie::{Cookie, CookieJar},
Data, Json, Query, RealIp, Redirect, RemoteAddr,
},
IntoResponse,
};
use serde::Deserialize;
use std::sync::Arc;
use tracing::info;
use uuid::Uuid;
use std::sync::Arc;

#[handler]
pub async fn login(state: Data<&Arc<AppState>>) -> impl IntoResponse {
Expand Down Expand Up @@ -44,7 +52,12 @@ pub struct MyQuery {
}

#[handler]
pub async fn callback(query: Query<MyQuery>, state: Data<&Arc<AppState>>) -> impl IntoResponse {
pub async fn callback(
query: Query<MyQuery>,
state: Data<&Arc<AppState>>,
ip: RealIp,
headers: &HeaderMap,
) -> impl IntoResponse {
let mut token = state.openid.request_token(&query.code).await.unwrap();

let mut token = Token::from(token);
Expand All @@ -65,26 +78,37 @@ pub async fn callback(query: Query<MyQuery>, state: Data<&Arc<AppState>>) -> imp
.await
.unwrap();

let session = state.database.create_session(user.id, "test").await.unwrap();
let user_agent = headers.get("user-agent").unwrap().to_str().unwrap();
let user_ip = ip.0.unwrap().to_string();

let session = state
.database
.create_session(user.id, user_agent, &user_ip)
.await
.unwrap();

// let session = state.database.get_session_by_id(&user.id).await.unwrap();

// TODO: return a token

let token = session.id;

Redirect::temporary("http://localhost:3000/hello").with_header("Set-Cookie", format!("property.v3x.token={}", token))
Redirect::temporary("http://localhost:3000/me")
.with_header("Set-Cookie", format!("property.v3x.token={}", token))
}

#[handler]
pub async fn me(state: Data<&Arc<AppState>>, cookies: &CookieJar) -> impl IntoResponse {

pub async fn me(state: Data<&Arc<AppState>>, cookies: &CookieJar) -> impl IntoResponse {
let token = cookies.get("property.v3x.token").unwrap();
let token = Uuid::parse_str(token.value_str()).unwrap();

let session = state.database.get_session_by_id(token).await.unwrap();

let user = state.database.get_user_from_id(session.user_id).await.unwrap();
let user = state
.database
.get_user_from_id(session.user_id)
.await
.unwrap();

Json(user)
}

0 comments on commit d2b0950

Please sign in to comment.