-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f33e157
commit 2e55a91
Showing
21 changed files
with
457 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use cookie::Cookie; | ||
use diesel::prelude::*; | ||
use rinja::Template; | ||
use salvo::oapi::extract::*; | ||
use salvo::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::hoops::jwt; | ||
use crate::schema::*; | ||
use crate::{db, json_ok, utils, AppResult, JsonResult}; | ||
|
||
#[handler] | ||
pub async fn login_page(res: &mut Response) -> AppResult<()> { | ||
#[derive(Template)] | ||
#[template(path = "login.html")] | ||
struct LoginTemplate {} | ||
if let Some(cookie) = res.cookies().get("jwt_token") { | ||
let token = cookie.value().to_string(); | ||
if jwt::decode_token(&token) { | ||
res.render(Redirect::other("/users")); | ||
return Ok(()); | ||
} | ||
} | ||
let hello_tmpl = LoginTemplate {}; | ||
res.render(Text::Html(hello_tmpl.render().unwrap())); | ||
Ok(()) | ||
} | ||
#[derive(Deserialize, ToSchema, Default, Debug)] | ||
pub struct LoginInData { | ||
pub username: String, | ||
pub password: String, | ||
} | ||
#[derive(Serialize, ToSchema, Default, Debug)] | ||
pub struct LoginOutData { | ||
pub id: String, | ||
pub username: String, | ||
pub token: String, | ||
pub exp: i64, | ||
} | ||
#[endpoint(tags("auth"))] | ||
pub async fn post_login( | ||
idata: JsonBody<LoginInData>, | ||
res: &mut Response, | ||
) -> JsonResult<LoginOutData> { | ||
let idata = idata.into_inner(); | ||
let conn = &mut db::connect()?; | ||
let Some((id, username, hashed)) = users::table | ||
.filter(users::username.eq(&idata.username)) | ||
.select((users::id, users::username, users::password)) | ||
.first::<(String, String, String)>(conn) | ||
.optional()? | ||
else { | ||
return Err(StatusError::unauthorized() | ||
.brief("User does not exist.") | ||
.into()); | ||
}; | ||
|
||
if utils::verify_password(&idata.password, hashed) | ||
.await | ||
.is_err() | ||
{ | ||
return Err(StatusError::unauthorized() | ||
.brief("Addount not exist or password is incorrect.") | ||
.into()); | ||
} | ||
|
||
let (token, exp) = jwt::get_token(username.clone(), id.clone())?; | ||
let odata = LoginOutData { | ||
id, | ||
username, | ||
token, | ||
exp, | ||
}; | ||
let cookie = Cookie::build(("jwt_token", odata.token.clone())) | ||
.path("/") | ||
.http_only(true) | ||
.build(); | ||
res.add_cookie(cookie); | ||
json_ok(odata) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
use diesel::prelude::*; | ||
use rinja::Template; | ||
use salvo::oapi::extract::*; | ||
use salvo::prelude::*; | ||
use serde::Deserialize; | ||
use ulid::Ulid; | ||
use validator::Validate; | ||
|
||
use crate::models::{SafeUser, User}; | ||
use crate::schema::*; | ||
use crate::{db, empty_ok, json_ok, utils, AppResult, EmptyResult, JsonResult}; | ||
|
||
#[derive(Template)] | ||
#[template(path = "user_list_page.html")] | ||
pub struct UserListPageTemplate {} | ||
|
||
#[derive(Template)] | ||
#[template(path = "user_list_frag.html")] | ||
pub struct UserListFragTemplate {} | ||
|
||
#[handler] | ||
pub async fn list_page(req: &mut Request, res: &mut Response) -> AppResult<()> { | ||
let is_fragment = req.headers().get("X-Fragment-Header"); | ||
match is_fragment { | ||
Some(_) => { | ||
let hello_tmpl = UserListFragTemplate {}; | ||
res.render(Text::Html(hello_tmpl.render().unwrap())); | ||
} | ||
None => { | ||
let hello_tmpl = UserListPageTemplate {}; | ||
res.render(Text::Html(hello_tmpl.render().unwrap())); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[derive(Deserialize, Debug, Validate, ToSchema, Default)] | ||
pub struct CreateInData { | ||
#[validate(length(min = 5, message = "username length must be greater than 5"))] | ||
pub username: String, | ||
#[validate(length(min = 6, message = "password length must be greater than 5"))] | ||
pub password: String, | ||
} | ||
#[endpoint(tags("users"))] | ||
pub async fn create_user(idata: JsonBody<CreateInData>) -> JsonResult<SafeUser> { | ||
let CreateInData { username, password } = idata.into_inner(); | ||
let conn = &mut db::connect()?; | ||
let user = User { | ||
id: Ulid::new().to_string(), | ||
username, | ||
password: utils::hash_password(&password).await?, | ||
}; | ||
diesel::insert_into(users::table) | ||
.values(&user) | ||
.execute(conn)?; | ||
let User { id, username, .. } = user; | ||
json_ok(SafeUser { id, username }) | ||
} | ||
|
||
#[derive(Deserialize, Debug, Validate, ToSchema)] | ||
struct UpdateInData { | ||
#[validate(length(min = 5, message = "username length must be greater than 5"))] | ||
username: String, | ||
#[validate(length(min = 6, message = "password length must be greater than 5"))] | ||
password: String, | ||
} | ||
#[endpoint(tags("users"), parameters(("id", description = "user id")))] | ||
pub async fn update_user( | ||
user_id: PathParam<String>, | ||
idata: JsonBody<UpdateInData>, | ||
) -> JsonResult<SafeUser> { | ||
let user_id = user_id.into_inner(); | ||
let UpdateInData { username, password } = idata.into_inner(); | ||
let conn = &mut db::connect()?; | ||
diesel::update(users::table.find(&user_id)) | ||
.set(( | ||
users::username.eq(&username), | ||
users::password.eq(utils::hash_password(&password).await?), | ||
)) | ||
.execute(conn)?; | ||
json_ok(SafeUser { | ||
id: user_id, | ||
username, | ||
}) | ||
} | ||
|
||
#[endpoint(tags("users"))] | ||
pub async fn delete_user(user_id: PathParam<String>) -> EmptyResult { | ||
let conn = &mut db::connect()?; | ||
diesel::delete(users::table.find(user_id.into_inner())).execute(conn)?; | ||
empty_ok() | ||
} | ||
|
||
#[endpoint(tags("users"))] | ||
pub async fn list_users() -> JsonResult<Vec<SafeUser>> { | ||
let conn = &mut db::connect()?; | ||
let users = users::table.select(SafeUser::as_select()).load(conn)?; | ||
json_ok(users) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
{%- if db_type == "postgres" %} | ||
DATABASE_URL=postgresql://postgres:root@localhost/{{project_name}} | ||
{%- endif %} | ||
{%- if db_type == "sqlite" %} | ||
DATABASE_URL="sqlite:data/{{project_name}}.sqlite" | ||
{%- endif %} | ||
{%- if db_type == "mysql" %} | ||
DATABASE_URL="mysql://root:root@localhost/{{project_name}}" | ||
{%- endif %} |
Oops, something went wrong.