#[derive(Deserialize, Serialize, Debug)]
struct Credentials {
username: String,
password: String,
}
struct AppState {
d1: D1Database,
jwt_secret: String
}
async fn login(mut req: Request, ctx: RouteContext<AppState>) -> Result<Response> {
let creds: Credentials = req.json().await?;
tracing::info!("Credentials: {:#?}", creds);
let d1 = ctx.data.d1;
tracing::info!("Get User");
let result = d1
.prepare("SELECT * FROM users WHERE username = ? AND password = ?")
.bind(&[creds.username.into(), creds.password.into()])
.unwrap()
.run()
.await?;
match result.results::<User>() {
Ok(users) => {
tracing::info!("User Found");
let exp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 3600; // 1 hour expiration
tracing::info!("Generate JWT");
let claims = Claims {
sub: users[0].id.to_string(),
exp,
};
...
#[derive(Deserialize, Serialize, Debug)]
struct Credentials {
username: String,
password: String,
}
struct AppState {
d1: D1Database,
jwt_secret: String
}
async fn login(mut req: Request, ctx: RouteContext<AppState>) -> Result<Response> {
let creds: Credentials = req.json().await?;
tracing::info!("Credentials: {:#?}", creds);
let d1 = ctx.data.d1;
tracing::info!("Get User");
let result = d1
.prepare("SELECT * FROM users WHERE username = ? AND password = ?")
.bind(&[creds.username.into(), creds.password.into()])
.unwrap()
.run()
.await?;
match result.results::<User>() {
Ok(users) => {
tracing::info!("User Found");
let exp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as usize + 3600; // 1 hour expiration
tracing::info!("Generate JWT");
let claims = Claims {
sub: users[0].id.to_string(),
exp,
};
...