···3636 }
3737 possibly_json => {
3838 match serde_json::from_str::<Message>(possibly_json) {
3939- Ok(Message::Introduction { client_kind, try_revive }) => {
4040- match client_kind.as_str() {
4141- "web" => {
4242- client_session_data.client_type = Some(ClientType::Web)
4343- }
4444- _ => {}
4545- }
4646- match try_revive {
4747- Some(token) => {
4848- let appstate = state.0.clone();
4949- let db = &appstate.1.lock().await;
5050- match User::revive_session_from_token(token.clone(), db).await {
5151- Ok(user) => {
5252- incoming_elog!(
5353- ev_log
5454- ,
5555- "Session revived for user: {}",
5656- user.clone().username.color_bright_cyan()
5757- );
5858- client_session_data.user = Some(user.clone());
5959- let _ = stream
6060- .send(ws::Message::from(msgtojson(Message::AuthSuccess {
6161- token: token,
6262- username: user.username,
6363- })))
6464- .await;
6565- }
6666- Err(e) => {
6767- match e {
6868- LuminaError::Postgres(postgres_error) => {
6969- // Check if it's a "no rows returned" type error
7070- if postgres_error.to_string().contains("no rows") {
7171- info_elog!( ev_log,"Session revival failed: token not found or expired.");
7272- } else {
7373- info_elog!(ev_log,"Session revival failed: database error: {:?}", postgres_error);
7474- }
7575- }
7676- LuminaError::Sqlite(sqlite_error) => {
7777- match sqlite_error {
7878- r2d2_sqlite::rusqlite::Error::QueryReturnedNoRows => {
7979- // No rows returned - session not found or expired
8080- info_elog!(ev_log,"Session revival failed: token not found or expired.");
8181- }
8282- _ => {
8383- info_elog!(ev_log,"Session revival failed: database error: {:?}", sqlite_error);
8484- }
8585- }
8686- }
8787- _ => {
8888- info_elog!(ev_log,"Session revival failed: {:?}", e);
8989- }
9090- }
9191- let _ = stream
9292- .send(ws::Message::from(msgtojson(Message::AuthFailure)))
9393- .await;
9494- }
9595- }
9696- }
9797- None =>{ let _ = stream
3939+ Ok(Message::Introduction { client_kind, try_revive }) => {
4040+ match client_kind.as_str() {
4141+ "web" => {
4242+ client_session_data.client_type = Some(ClientType::Web)
4343+ }
4444+ _ => {}
4545+ }
4646+ match try_revive {
4747+ Some(token) => {
4848+ let appstate = state.0.clone();
4949+ let db = &appstate.1.lock().await;
5050+ match User::revive_session_from_token(token.clone(), db).await {
5151+ Ok(user) => {
5252+ incoming_elog!(ev_log, "Session revived for user: {}",
5353+ user.clone().username.color_bright_cyan()
5454+ );
5555+ client_session_data.user = Some(user.clone());
5656+ let _ = stream
5757+ .send(ws::Message::from(msgtojson(Message::AuthSuccess {
5858+ token,
5959+ username: user.username,
6060+ })))
6161+ .await;
6262+ }
6363+ Err(e) => {
6464+ match e {
6565+ LuminaError::Postgres(postgres_error) => {
6666+ // Check if it's a "no rows returned" type error
6767+ if postgres_error.to_string().contains("no rows") {
6868+ info_elog!( ev_log,"Session revival failed: token not found or expired.");
6969+ } else {
7070+ info_elog!(ev_log,"Session revival failed: database error: {:?}", postgres_error);
7171+ }
7272+ }
7373+ LuminaError::Sqlite(sqlite_error) => {
7474+ match sqlite_error {
7575+ r2d2_sqlite::rusqlite::Error::QueryReturnedNoRows => {
7676+ // No rows returned - session not found or expired
7777+ info_elog!(ev_log,"Session revival failed: token not found or expired.");
7878+ }
7979+ _ => {
8080+ info_elog!(ev_log,"Session revival failed: database error: {:?}", sqlite_error);
8181+ }
8282+ }
8383+ }
8484+ _ => {
8585+ info_elog!(ev_log,"Session revival failed: {:?}", e);
8686+ }
8787+ }
8888+ let _ = stream
8989+ .send(ws::Message::from(msgtojson(Message::AuthFailure)))
9090+ .await;
9191+ }
9292+ }
9393+ }
9494+ None =>{ let _ = stream
9895 .send(ws::Message::from(msgtojson(Message::Greeting {
9996 greeting: "Hello from server!".to_string(),
10097 })))
10198 .await;
102102- }
103103- }
104104- },
9999+ }
100100+ }
101101+ },
105102 Ok(Message::RegisterRequest {
106103 email,
107104 username,
···130127 Ok((session_reference, user)) => {
131128 client_session_data.user =
132129 Some(user.clone());
133133- incoming_elog!(ev_log,"User {} authenticated.",
134134- user.clone().username.color_bright_cyan()
135135- );
130130+ incoming_elog!(ev_log,"User {} authenticated.",
131131+ user.clone().username.color_bright_cyan()
132132+ );
136133 let _ = stream
137134 .send(ws::Message::from(msgtojson(
138135 Message::AuthSuccess {
···146143 match e {
147144 LuminaError::Postgres(e) =>
148145 error_elog!(ev_log,"While creating session token: {:?}", e),
149149- LuminaError::SqlitePool(e) =>
150150- warn_elog!(ev_log,"There was an error creating session token: {:?}", e),
151151- LuminaError::Sqlite(e) =>
146146+ LuminaError::SqlitePool(e) =>
147147+ warn_elog!(ev_log,"There was an error creating session token: {:?}", e),
148148+ LuminaError::Sqlite(e) =>
152149warn_elog!(ev_log,"There was an error creating session token: {:?}", e),
153153- _ => {}
150150+ _ => {}
154151 }
155152 // I would return a more specific error message
156153 // to the client here, but if the server knows the
···186183 }
187184 LuminaError::RegisterPasswordNotValid(why) => {
188185 registration_error_elog!(ev_log, "Password is not valid: {}",
189189- why
190190- );
191191- }
186186+ why
187187+ );
188188+ }
192189 e => {
193190 registration_error_elog!(ev_log, "Error creating user: {:?}",
194194- e
195195- );
196196- }
191191+ e
192192+ );
193193+ }
197194 }
198195199196 // I would return a more specific error message
···255252 }
256253 Ok(Message::LoginAuthenticationRequest { email_username, password }) =>
257254 {
258258- let appstate = state.0.clone();
255255+ let appstate = state.0.clone();
259256 let db = &appstate.1.lock().await;
260260- let msgback = match User::authenticate(email_username.clone(), password, db).await {
257257+ let msgback = match User::authenticate(email_username.clone(), password, db).await {
261258 Ok((session_reference, user)) => {
262262- incoming_elog!(ev_log,"User {} authenticated to session with id {}.\n{}", user.username.clone().color_bright_cyan(), session_reference.session_id.to_string().color_pink(), format!("(User id: {})", user.id.to_string()).style_dim());
263263- client_session_data.user = Some(user.clone());
264264- Message::AuthSuccess {token: session_reference.token, username: user.username }
265265- }
266266- ,
259259+ incoming_elog!(ev_log,"User {} authenticated to session with id {}.\n{}", user.username.clone().color_bright_cyan(), session_reference.session_id.to_string().color_pink(), format!("(User id: {})", user.id.to_string()).style_dim());
260260+ client_session_data.user = Some(user.clone());
261261+ Message::AuthSuccess {token: session_reference.token, username: user.username }
262262+ }
263263+ ,
267264 Err(s) => {
268268- match s {
269269- LuminaError::AuthenticationWrongPassword => {
270270- registration_error_elog!(ev_log,"User {} {} authenticated: Incorrect credentials", email_username.color_bright_cyan(), "not".color_red());
271271- }
272272- LuminaError::AuthenticationUserNotFound => {
273273- registration_error_elog!(ev_log,"User {} {} authenticated: User not found", email_username.color_bright_cyan(), "not".color_red());
274274- }
275275- _ => {
276276- registration_error_elog!(ev_log,"User {} {} authenticated: {:?}", email_username.color_bright_cyan(), "not".color_red(), s);
277277- }
278278- }
279279- Message::AuthFailure
265265+ match s {
266266+ LuminaError::AuthenticationWrongPassword => {
267267+ registration_error_elog!(ev_log,"User {} {} authenticated: Incorrect credentials", email_username.color_bright_cyan(), "not".color_red());
268268+ }
269269+ LuminaError::AuthenticationUserNotFound => {
270270+ registration_error_elog!(ev_log,"User {} {} authenticated: User not found", email_username.color_bright_cyan(), "not".color_red());
271271+ }
272272+ _ => {
273273+ registration_error_elog!(ev_log,"User {} {} authenticated: {:?}", email_username.color_bright_cyan(), "not".color_red(), s);
274274+ }
275275+ }
276276+ Message::AuthFailure
280277281281- },
282282- };
283283- let _ = stream.send(ws::Message::from(msgtojson(msgback))).await;
278278+ },
279279+ };
280280+ let _ = stream.send(ws::Message::from(msgtojson(msgback))).await;
284281 }
285282 Ok(Message::OwnUserInformationRequest) => {
286283 // Handle request for user's own information
+18-18
server/src/database.rs
···3232 let conn = pool.get().map_err(LuminaError::SqlitePool)?;
3333 let _ = conn.execute(
3434 "CREATE TABLE IF NOT EXISTS users (
3535- id TEXT PRIMARY KEY,
3636- email TEXT NOT NULL UNIQUE,
3737- username TEXT NOT NULL UNIQUE,
3838- password TEXT NOT NULL)",
3535+ id TEXT PRIMARY KEY,
3636+ email TEXT NOT NULL UNIQUE,
3737+ username TEXT NOT NULL UNIQUE,
3838+ password TEXT NOT NULL)",
3939 [],
4040 );
4141 let _ = conn.execute(
4242 "CREATE TABLE IF NOT EXISTS sessions (
4343- id TEXT PRIMARY KEY NOT NULL,
4444- user_id TEXT NOT NULL,
4545- session_key TEXT NOT NULL,
4646- created_at INT NOT NULL)",
4343+ id TEXT PRIMARY KEY NOT NULL,
4444+ user_id TEXT NOT NULL,
4545+ session_key TEXT NOT NULL,
4646+ created_at INT NOT NULL)",
4747 [],
4848 );
4949 let _ = conn.execute(
···150150 .0
151151 .execute(
152152 "CREATE TABLE IF NOT EXISTS users (
153153- id UUID DEFAULT gen_random_uuid (),
154154- email VARCHAR NOT NULL UNIQUE,
155155- username VARCHAR NOT NULL UNIQUE,
156156- password VARCHAR NOT NULL
157157- )",
153153+ id UUID DEFAULT gen_random_uuid (),
154154+ email VARCHAR NOT NULL UNIQUE,
155155+ username VARCHAR NOT NULL UNIQUE,
156156+ password VARCHAR NOT NULL
157157+)",
158158 &[],
159159 )
160160 .await
···164164 .0
165165 .execute(
166166 "CREATE TABLE IF NOT EXISTS sessions (
167167- id UUID DEFAULT gen_random_uuid (),
168168- user_id UUID NOT NULL,
169169- session_key VARCHAR NOT NULL,
170170- created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
171171- )",
167167+ id UUID DEFAULT gen_random_uuid (),
168168+ user_id UUID NOT NULL,
169169+ session_key VARCHAR NOT NULL,
170170+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
171171+)",
172172 &[],
173173 )
174174 .await
+18-18
server/src/user.rs
···125125 }
126126 let id_str = Uuid::new_v4().to_string();
127127 let id = conn
128128- .prepare("INSERT INTO users (id, email, username, password) VALUES (?1, ?2, ?3, ?4) RETURNING id")
129129- .map_err(LuminaError::Sqlite)?
130130- .query_row(&[&id_str, &email, &username, &password], |row| {
131131- let a: String = row.get(0)?;
132132- // Unwrap: Not entirely safe. If the database is corrupted, this will panic.
133133- Ok(Uuid::from_str(a.as_str()).unwrap())
134134- }).map_err(LuminaError::Sqlite)
135135- ?;
128128+.prepare("INSERT INTO users (id, email, username, password) VALUES (?1, ?2, ?3, ?4) RETURNING id")
129129+.map_err(LuminaError::Sqlite)?
130130+.query_row(&[&id_str, &email, &username, &password], |row| {
131131+ let a: String = row.get(0)?;
132132+ // Unwrap: Not entirely safe. If the database is corrupted, this will panic.
133133+ Ok(Uuid::from_str(a.as_str()).unwrap())
134134+}).map_err(LuminaError::Sqlite)
135135+?;
136136 Ok(User {
137137 id,
138138 email,
···247247 match db {
248248 DbConn::PgsqlConnection(client, _) => {
249249 let user = client
250250- .query_one("SELECT users.id, users.email, users.username FROM users JOIN sessions ON users.id = sessions.user_id WHERE sessions.session_key = $1", &[&token])
251251- .await
252252- .map_err(LuminaError::Postgres)?;
250250+.query_one("SELECT users.id, users.email, users.username FROM users JOIN sessions ON users.id = sessions.user_id WHERE sessions.session_key = $1", &[&token])
251251+.await
252252+.map_err(LuminaError::Postgres)?;
253253 Ok(User {
254254 id: user.get(0),
255255 email: user.get(1),
···259259 DbConn::SqliteConnectionPool(pool) => {
260260 let conn = pool.get().map_err(LuminaError::SqlitePool)?;
261261 let user = conn.query_row("SELECT users.id, users.email, users.username FROM users JOIN sessions ON users.id = sessions.user_id WHERE sessions.session_key = ?1", &[&token],
262262- |row| {
263263- let a: String = row.get(0).unwrap();
264264- Ok(User {
265265- id: Uuid::from_str(a.as_str()).unwrap(),
266266- email: row.get(1).unwrap(),
267267- username: row.get(2).unwrap(),
268268- })
262262+|row| {
263263+let a: String = row.get(0).unwrap();
264264+Ok(User {
265265+ id: Uuid::from_str(a.as_str()).unwrap(),
266266+ email: row.get(1).unwrap(),
267267+ username: row.get(2).unwrap(),
268268+})
269269 }).map_err(LuminaError::Sqlite)?;
270270 Ok(user)
271271 }