Proper welcome modal implementation
This commit is contained in:
parent
50dce8da96
commit
6b6c047b68
11 changed files with 147 additions and 14 deletions
|
@ -9,3 +9,4 @@ pub mod user {
|
|||
pub const USER_AVATAR_COOKIE: &str = "user_avatar";
|
||||
}
|
||||
pub const LANG_COOKIE: &str = "lang";
|
||||
pub const WELCOMED_COOKIE: &str = "welcomed";
|
|
@ -42,7 +42,7 @@ async fn rocket() -> _ {
|
|||
.manage(http)
|
||||
.mount(
|
||||
"/",
|
||||
routes![get_challenge, login, post_login, success, logout, testing],
|
||||
routes![get_challenge, get_guilds, login, post_login, success, logout, testing],
|
||||
)
|
||||
.mount("/css", FileServer::from(relative!("styles/css")))
|
||||
.mount("/", FileServer::from(relative!("static")).rank(1))
|
||||
|
|
35
src/routes/get_guilds.rs
Normal file
35
src/routes/get_guilds.rs
Normal file
|
@ -0,0 +1,35 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use poise::serenity_prelude::Http;
|
||||
use rocket::{http::{CookieJar, Status}, State};
|
||||
use rocket::serde::json::Json;
|
||||
|
||||
use crate::{cookies::user::USER_ID_COOKIE, models::Settings};
|
||||
|
||||
// TODO: Incrementally send guilds
|
||||
#[get("/get_guilds")]
|
||||
pub async fn get_guilds(
|
||||
cookies: &CookieJar<'_>,
|
||||
settings: &State<Settings>,
|
||||
http: &State<Http>,
|
||||
) -> Result<Json<HashMap<u64, bool>>, Status> {
|
||||
let user_id: u64 = match cookies.get_private(USER_ID_COOKIE) {
|
||||
Some(id) => match id.value().parse() {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Err(Status::BadRequest),
|
||||
},
|
||||
None => return Err(Status::Unauthorized),
|
||||
};
|
||||
Ok(Json({
|
||||
let mut guild_statuses = HashMap::with_capacity(settings.guilds.len());
|
||||
for guild in &settings.guilds {
|
||||
guild_statuses.insert(guild.id, {
|
||||
if guild.hidden {
|
||||
continue;
|
||||
}
|
||||
http.get_member(guild.id, user_id).await.is_ok()
|
||||
});
|
||||
}
|
||||
guild_statuses
|
||||
}))
|
||||
}
|
|
@ -6,7 +6,7 @@ use rocket::response::Redirect;
|
|||
pub fn login() -> Redirect {
|
||||
Redirect::to(format!(
|
||||
// Switch from response_type=code to response_type=token from URL generator
|
||||
"https://discord.com/api/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=token&scope=identify%20guilds.join%20guilds",
|
||||
"https://discord.com/api/oauth2/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=token&scope=identify",
|
||||
client_id = env::var("CLIENT_ID").unwrap(),
|
||||
redirect_uri = format!("{}success", env::var("DOMAIN").unwrap()),
|
||||
))
|
||||
|
|
|
@ -2,6 +2,10 @@
|
|||
mod _get_challenge;
|
||||
pub use _get_challenge::get_challenge;
|
||||
|
||||
#[path = "get_guilds.rs"]
|
||||
mod _get_guilds;
|
||||
pub use _get_guilds::get_guilds;
|
||||
|
||||
#[path = "login.rs"]
|
||||
mod _login;
|
||||
pub use _login::login;
|
||||
|
|
|
@ -5,7 +5,7 @@ use rocket::{
|
|||
response::Redirect,
|
||||
};
|
||||
|
||||
use crate::{cookies::token::TOKEN_EXPIRE_COOKIE, models::User};
|
||||
use crate::{cookies::{token::TOKEN_EXPIRE_COOKIE, WELCOMED_COOKIE}, models::User};
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct Login<'r> {
|
||||
|
@ -17,7 +17,7 @@ pub struct Login<'r> {
|
|||
|
||||
#[post("/login", data = "<login>")]
|
||||
pub async fn post_login(login: Form<Login<'_>>, cookies: &CookieJar<'_>) -> Redirect {
|
||||
if (login.token_type != "Bearer" || login.scope != "guilds.join+identify+guilds")
|
||||
if (login.token_type != "Bearer" || login.scope.split("+").any(|scope| scope == "identify"))
|
||||
&& User::init(login.access_token, cookies).await.is_ok()
|
||||
{
|
||||
cookies.add(Cookie::new(
|
||||
|
@ -26,6 +26,7 @@ pub async fn post_login(login: Form<Login<'_>>, cookies: &CookieJar<'_>) -> Redi
|
|||
.timestamp()
|
||||
.to_string(),
|
||||
));
|
||||
cookies.remove(Cookie::named(WELCOMED_COOKIE));
|
||||
}
|
||||
Redirect::to("/")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue