-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from dhruvdabhi101/develop
fix: authentication and added a new pages route
- Loading branch information
Showing
13 changed files
with
176 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,29 @@ | ||
use mongodb::bson::extjson::de::Error; | ||
use mongodb::results::InsertOneResult; | ||
use rocket::{get, http::Status, post, serde::json::Json, State}; | ||
use rocket_authorization::oauth::OAuth; | ||
use rocket_authorization::{AuthError, Credential}; | ||
|
||
use crate::config::jwt::decode_jwt; | ||
use crate::{ | ||
config::jwt::create_jwt, | ||
models::user_model::{LoginResponse, LoginUser, User, UserFromMongo}, | ||
models::{user_model::{LoginResponse, LoginUser, User, UserFromMongo}, page_model::{PageCreateRequest, PageCreateResponse, Page}}, | ||
repository::mongodb_repo::MongoRepo, | ||
}; | ||
|
||
#[post("/user", data = "<new_user>")] | ||
pub fn create_user( | ||
db: &State<MongoRepo>, | ||
new_user: Json<User>, | ||
) -> Result<Json<InsertOneResult>, Status> { | ||
let data = User { | ||
username: new_user.username.clone(), | ||
password: new_user.password.clone(), | ||
email: new_user.email.clone(), | ||
name: new_user.name.clone(), | ||
#[post("/create-page", data="<new_page>")] | ||
pub fn create_page(auth: Credential<OAuth>, db: &State<MongoRepo>, new_page: Json<PageCreateRequest>) -> Result<Json<PageCreateResponse>, Status> { | ||
// get user from jwt token | ||
let token = &auth.token; | ||
println!("{:?}", token); | ||
|
||
let new_page = PageCreateRequest { | ||
title: new_page.title.clone(), | ||
content: new_page.content.clone(), | ||
slug: new_page.slug.clone(), | ||
published: new_page.published.clone(), | ||
}; | ||
let user_details = db.create_user(data); | ||
match user_details { | ||
Ok(user) => Ok(Json(user)), | ||
Err(_) => Err(Status::InternalServerError), | ||
} | ||
} | ||
|
||
#[get("/user/<path>")] | ||
pub fn get_user(db: &State<MongoRepo>, path: &str) -> Result<Json<UserFromMongo>, Status> { | ||
let id = path; | ||
if id.is_empty() { | ||
return Err(Status::BadRequest); | ||
} | ||
let user_details = db.get_user(&id); | ||
match user_details { | ||
Ok(user) => Ok(Json(user)), | ||
Err(_) => Err(Status::InternalServerError), | ||
} | ||
let page: PageCreateResponse = db.create_page(new_page, token).unwrap(); | ||
Ok(Json(page)) | ||
} | ||
#[post("/login", data = "<user>")] | ||
pub fn login(db: &State<MongoRepo>, user: Json<LoginUser>) -> Result<Json<LoginResponse>, Status> { | ||
// get user details from db | ||
let user_details: Result<UserFromMongo, Error> = db.login(&user.username, &user.password); | ||
|
||
// if user not found return 404 | ||
if user_details.is_err() { | ||
return Err(Status::InternalServerError); | ||
} else { | ||
// if user found return jwt token | ||
let user: UserFromMongo = user_details.unwrap(); | ||
Ok(Json(LoginResponse { | ||
token: create_jwt(user._id.to_hex()).unwrap(), | ||
})) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod user_model; | ||
pub mod page_model; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#[derive(Debug)] | ||
pub enum UserError { | ||
NotFound, | ||
AlreadyExists, | ||
InvalidPassword, | ||
InvalidCredentials, | ||
InternalError, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum PageError { | ||
NotFound, | ||
AlreadyExists, | ||
InternalError | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod mongodb_repo; | ||
pub mod error; |
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