Added a user system with no proper user validation but working authorisation. #1
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "user-system"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
8a41ddec86
toa1b0611abf
a1b0611abf
to33ed736f0c
33ed736f0c
tob8c246f521
b8c246f521
toaf0dcee159
5bf581356a
toa3f70b8ad7
a3f70b8ad7
todf8b553345
@ -0,0 +301,4 @@
pub email: String,
#[validate(length(min = 10), must_match = "password_repeat")]
pub password: String,
pub password_repeat: String,
Ik zou password_repeat checks in de frontend doen, als je die dan toch hebt.
Het plan is om beide te doen, want als je dan de frontend checks omzeilt krijg je alsnog narigheid. want je kan ook gewoon tegen de api aan praten.
@ -0,0 +1,42 @@
use rocket::fs::NamedFile;
use rocket::http::Method;
use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, CorsOptions};
| ^^^^^^^^^^^ use of undeclared crate or module
rocket_cors
@ -0,0 +1,43 @@
ex<!DOCTYPE html>
ex
@ -0,0 +6,4 @@
);
CREATE TABLE pwd (
id INTEGER NOT NULL PRIMARY KEY,
I recommend calling the column
user_id
in both tables. The column inpwd
should also have a foreign key constraint likeREFERENCES user ON DELETE CASCADE
. Or it could be in the same table: Using a separate table is usually only worth it if the rows are big or the relation is not one on one.@ -0,0 +129,4 @@
.values((pwd::id.eq(ids[0]), pwd::password.eq(&password_hash)))
.execute(c)
})
}).await {
Wel mooi om de grote expression waar je hier op matcht even een naam te geven zodat de match leesbaar blijft.
@ -0,0 +16,4 @@
<input type="submit">
</form>
</body>
</html>1
1
865ff9ac0e
to87c2e02380
87c2e02380
to216571a45f
216571a45f
to5f73d556c6
@ -0,0 +17,4 @@
use validator::ValidateArgs;
#[derive(Debug, Responder)]
pub enum ApiResponseVariant {
You can probably use a
Result<Value, Status>
for most endpoints and avoid a custom enum. I also recommend usingjson::Value
qualified like that becauseValue
by itself is not very descriptive.True, but in the future we might want to return a status on a non error condition, or return a Redirect, I understand it is a bit overkill now, but in a previous iteration I was also returning Redirects and then this becomes a nice solution imho.
@ -0,0 +113,4 @@
}
#[get("/gamenights")]
pub async fn gamenights(conn: DbConn, user: Option<schema::User>) -> ApiResponseVariant {
I think you can use a Request Guard (see https://api.rocket.rs/v0.5-rc/rocket/request/trait.FromRequest.html) to authenticate the user and role: For example, endpoints that require admin privileges could accept a non-optional
Admin
struct containing a user id and the request guard that generates it would only returnSuccess
if the user is logged and has the admin role.See also the examples under the header "Request-Local State" in the above link.
Reading more carefully I see you're already doing this, just that you're accepting an
Option<User>
and then checking it's notNone
while you could accept aUser
and be sure.@ -0,0 +149,4 @@
.select(user::id)
.get_results(c)
{
Ok(id) => id[0],
generates a panic if the user does not exist
@ -0,0 +149,4 @@
{
Ok(()) => (),
Err(error) => {
return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string())))
Value is not an error response
We'll it is, it's an application level error, so it's a valid request and you will get a valid http response with an "Failure" result. So that's why it returns an actual Json value
@ -0,0 +155,4 @@
match schema::insert_user(conn, register).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
Value is not an error response
@ -0,0 +121,4 @@
.eq(&new_user.username)
.and(user::email.eq(&new_user.email)),
)
.select(user::id)
called
user_id
now