Please help in solving the error.
Could you please post the error?
Half of a megabyte for a screenshot that still is hard to properly read on a mobile is not what I want to download right now... Traffic is valuable...
Perhaps show some code along side the error...
Okay
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
extern crate diesel;
extern crate ganesh;
extern crate rand;
extern crate time;
use std::fs::File;
use rocket::request::Form;
use rocket::response::Redirect;
use rocket::http::{Cookie, Cookies};
use ganesh::{create_new_users_session, establish_connection};
use time::Duration;
#[derive(FromForm)]
struct User
{
username: String,
password: String,
}
fn generate_session_token(length: u8) -> Result<String, std::io::Error>
{
let bytes: Vec = (0..length).map(|_| rand::random::()).collect();
let strings: Vec = byyes.iter().map(|byte| format!("{:02X}", byte)).collect();
return Ok(strings.join(""));
}
#[get("/")]
fn index() -> File {
File::open("hello.html").expect("File not found")
}
#[post("/", data = "")]
fn login(mut cookies: Cookies, userdata: Form) -> Result<Redirect, String, i64>
{
if userdata.username == "axel".to_string() && userdata.password == "password"
{
match generate_session_token(64)
{
Ok(session_token) => {
let connection = establish_connection();
let user_id =1;
create_new_users_session(connection, username, password);
create_new_user_session(connection, user_id);
let mut c = Cookie::new("session_token", session_token);
c.set_max_age(Duration::hours(24));
cookies.add_private(c);
Ok(Redirect::to("/"))
}
Err(String::from("Login failed")),
}
}
else
{
Err(String::from("Username or password incorrect"))
}
}
fn main() {
rocket::ignite().mount("/", routes![index,login]).launch();
}
the above code is written in my main.rs file
#[macro_use]
extern crate diesel;
extern crate dotenv;
pub mod models;
pub mod schema;
use diesel::prelude::*;
use dotenv::dotenv;
use std::env;
use self::models::{NewUserSession, UserSession};
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
}
pub fn create_new_user_session
(
conn: &PgConnection,
token: String
) -> UserSession {
use schema::user_session;
let new_user_session = NewUserSession
{
token: token,
};
diesel::insert_into(user_session::table)
.values(&new_user_session)
.execute(conn)
.expect("Error saving new session");
user_session::table
.order(user_session::id.desc())
.first(conn)
.unwrap()
}
Please edit your two code posts above, putting a line with three backticks (`) both before and after the code. That will cause github to display the code with Rust formatting.
Edit: See also this more recent post for a better explanation.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.