summaryrefslogtreecommitdiff
path: root/src/api/auth.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/auth.rs')
-rw-r--r--src/api/auth.rs57
1 files changed, 55 insertions, 2 deletions
diff --git a/src/api/auth.rs b/src/api/auth.rs
index 7f7cf9e..0ff180e 100644
--- a/src/api/auth.rs
+++ b/src/api/auth.rs
@@ -3,11 +3,34 @@ use serde::Deserialize;
use time::{Duration, OffsetDateTime};
use tower_cookies::{Cookie, Cookies};
-use crate::types::{
+use crate::{types::{
extract::{AuthorizedUser, Check, CheckResult, Json, Log},
http::ResponseCode,
session::Session,
user::User,
+}, public::docs::{EndpointDocumentation, EndpointMethod}};
+
+pub const AUTH_REGISTER: EndpointDocumentation = EndpointDocumentation {
+ uri: "/api/auth/register",
+ method: EndpointMethod::Post,
+ description: "Registeres a new account",
+ body: Some(r#"
+ {
+ "firstname": "[Object]",
+ "lastname": "object]",
+ "email": "object@object.object",
+ "password": "i love js",
+ "gender": "object",
+ "day": 1,
+ "month": 1,
+ "year": 1970
+ }
+ "#),
+ responses: &[
+ (201, "Successfully registered new user"),
+ (400, "Body does not match parameters"),
+ ],
+ cookie: None,
};
#[derive(Deserialize, Debug)]
@@ -93,9 +116,26 @@ async fn register(cookies: Cookies, Json(body): Json<RegistrationRequet>) -> Res
cookies.add(cookie);
- ResponseCode::Created.text("Successfully created new user")
+ ResponseCode::Created.text("Successfully created new user, auth cookie is returned")
}
+pub const AUTH_LOGIN: EndpointDocumentation = EndpointDocumentation {
+ uri: "/api/auth/login",
+ method: EndpointMethod::Post,
+ description: "Logs into an existing account",
+ body: Some(r#"
+ {
+ "email": "object@object.object",
+ "password": "i love js"
+ }
+ "#),
+ responses: &[
+ (200, "Successfully logged in, auth cookie is returned"),
+ (400, "Body does not match parameters, or invalid email password combination"),
+ ],
+ cookie: None,
+};
+
#[derive(Deserialize)]
struct LoginRequest {
email: String,
@@ -136,6 +176,19 @@ async fn login(cookies: Cookies, Json(body): Json<LoginRequest>) -> Response {
ResponseCode::Success.text("Successfully logged in")
}
+pub const AUTH_LOGOUT: EndpointDocumentation = EndpointDocumentation {
+ uri: "/api/auth/logout",
+ method: EndpointMethod::Post,
+ description: "Logs out of a logged in account",
+ body: None,
+ responses: &[
+ (200, "Successfully logged out"),
+ (401, "Unauthorized"),
+ (500, "Failed to log out user")
+ ],
+ cookie: None,
+};
+
async fn logout(cookies: Cookies, AuthorizedUser(user): AuthorizedUser, _: Log) -> Response {
cookies.remove(Cookie::new("auth", ""));