You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
use std::{collections::HashMap, env};
|
|
|
|
|
|
|
|
use rocket::{http::CookieJar, response::Redirect};
|
|
|
|
|
|
|
|
use crate::{cookies::token::TOKEN_COOKIE, models::User, utils::Referer};
|
|
|
|
|
|
|
|
#[get("/logout")]
|
|
|
|
pub fn logout(cookies: &CookieJar<'_>, referer: Referer) -> Redirect {
|
|
|
|
let token = match cookies.get_private(TOKEN_COOKIE) {
|
|
|
|
Some(cookie) => cookie.value().to_owned(),
|
|
|
|
None => return Redirect::to("/"),
|
|
|
|
};
|
|
|
|
rocket::tokio::spawn(async {
|
|
|
|
let client = reqwest::Client::new();
|
|
|
|
let params = {
|
|
|
|
let mut params = HashMap::new();
|
|
|
|
params.insert("client_id", env::var("CLIENT_ID").unwrap());
|
|
|
|
params.insert("client_secret", env::var("CLIENT_SECRET").unwrap());
|
|
|
|
params.insert("token", token);
|
|
|
|
params
|
|
|
|
};
|
|
|
|
if let Err(error) = client
|
|
|
|
.post("https://discord.com/api/oauth2/token/revoke")
|
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
.form(¶ms)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
{
|
|
|
|
println!("Failed to revoke token: {:?}", error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
User::purge(cookies);
|
|
|
|
let redirect_url = referer.0.unwrap_or("/".to_owned());
|
|
|
|
Redirect::to(redirect_url)
|
|
|
|
}
|