Finsh discord login/logout process
This commit is contained in:
parent
c35a999fd8
commit
371661ddad
40
src/main.rs
40
src/main.rs
|
|
@ -80,6 +80,7 @@ async fn main() {
|
||||||
.route("/google_auth", get(google_auth))
|
.route("/google_auth", get(google_auth))
|
||||||
.route("/discord_auth", get(discord_auth))
|
.route("/discord_auth", get(discord_auth))
|
||||||
.route("/login", get(login))
|
.route("/login", get(login))
|
||||||
|
.route("/logout", get(logout))
|
||||||
.route("/dashboard", get(dashboard))
|
.route("/dashboard", get(dashboard))
|
||||||
.route("/auth/callback", get(google_authorized))
|
.route("/auth/callback", get(google_authorized))
|
||||||
.route("/auth/discord", get(discord_authorized))
|
.route("/auth/discord", get(discord_authorized))
|
||||||
|
|
@ -98,17 +99,26 @@ async fn main() {
|
||||||
|
|
||||||
// Session is optional
|
// Session is optional
|
||||||
async fn index(user: Option<User>) -> impl IntoResponse {
|
async fn index(user: Option<User>) -> impl IntoResponse {
|
||||||
let name = match user {
|
let (userid, name) = match user {
|
||||||
Some(u) => u.username,
|
Some(u) => (true, u.username),
|
||||||
None => "You're not logged in.".to_string(),
|
None => (false, "You're not logged in.".to_string()),
|
||||||
};
|
};
|
||||||
let template = IndexTemplate { name };
|
let template = IndexTemplate { userid, name };
|
||||||
HtmlTemplate(template)
|
HtmlTemplate(template)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn login() -> impl IntoResponse {
|
async fn login() -> impl IntoResponse {
|
||||||
let name = "".to_string();
|
let name = "".to_string();
|
||||||
let template = LoginTemplate { name };
|
let userid = false;
|
||||||
|
let template = LoginTemplate { userid, name };
|
||||||
|
HtmlTemplate(template)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Valid user session required. If there is none, redirect to the auth page
|
||||||
|
async fn dashboard(user: User) -> impl IntoResponse {
|
||||||
|
let name = user.username;
|
||||||
|
let userid = true;
|
||||||
|
let template = DashboardTemplate { userid, name };
|
||||||
HtmlTemplate(template)
|
HtmlTemplate(template)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -157,8 +167,17 @@ fn discord_oauth_client() -> BasicClient {
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "login.html")]
|
#[template(path = "login.html")]
|
||||||
struct LoginTemplate {
|
struct LoginTemplate {
|
||||||
|
userid: bool,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(path = "dashboard.html")]
|
||||||
|
struct DashboardTemplate {
|
||||||
|
userid: bool,
|
||||||
|
name: String,
|
||||||
|
}
|
||||||
|
|
||||||
struct FreshUserId {
|
struct FreshUserId {
|
||||||
pub user_id: UserId,
|
pub user_id: UserId,
|
||||||
pub cookie: HeaderValue,
|
pub cookie: HeaderValue,
|
||||||
|
|
@ -248,6 +267,7 @@ impl UserId {
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct IndexTemplate {
|
struct IndexTemplate {
|
||||||
|
userid: bool,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -269,14 +289,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Valid user session required. If there is none, redirect to the auth page
|
|
||||||
async fn dashboard(user: User) -> impl IntoResponse {
|
|
||||||
format!(
|
|
||||||
"Welcome to the protected area :)\nHere's your info:\n{:?}",
|
|
||||||
user
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn logout(
|
async fn logout(
|
||||||
Extension(store): Extension<MemoryStore>,
|
Extension(store): Extension<MemoryStore>,
|
||||||
TypedHeader(cookies): TypedHeader<headers::Cookie>,
|
TypedHeader(cookies): TypedHeader<headers::Cookie>,
|
||||||
|
|
|
||||||
|
|
@ -43,13 +43,14 @@
|
||||||
<!-- ***** Logo End ***** -->
|
<!-- ***** Logo End ***** -->
|
||||||
<!-- ***** Menu Start ***** -->
|
<!-- ***** Menu Start ***** -->
|
||||||
<ul class="nav">
|
<ul class="nav">
|
||||||
<li class="scroll-to-section"><a href="#top" class="active">Home</a></li>
|
{% if userid %}
|
||||||
|
<li><a href="/logout">Logout</a></li>
|
||||||
|
<li>{{ name }}</li>
|
||||||
|
{% else %}
|
||||||
|
<li><a href="/login">Login</a></li>
|
||||||
|
{% endif %}
|
||||||
<li><a href="">About Us</a></li>
|
<li><a href="">About Us</a></li>
|
||||||
<li class=""><a href="/login">Login</a></li>
|
|
||||||
</ul>
|
</ul>
|
||||||
<a class='menu-trigger'>
|
|
||||||
<span>Menu</span>
|
|
||||||
</a>
|
|
||||||
<!-- ***** Menu End ***** -->
|
<!-- ***** Menu End ***** -->
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -62,28 +63,7 @@
|
||||||
|
|
||||||
<!-- ***** Footer Start ***** -->
|
<!-- ***** Footer Start ***** -->
|
||||||
<footer>
|
<footer>
|
||||||
<div class="container">
|
|
||||||
<div class="row">
|
|
||||||
<div class="col-lg-6 col-xs-12">
|
|
||||||
<div class="left-text-content">
|
|
||||||
<p>Copyright © 2020 Breezed Co., Ltd.
|
|
||||||
|
|
||||||
- Design: <a rel="nofollow noopener" href="https://templatemo.com">TemplateMo</a></p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<div class="col-lg-6 col-xs-12">
|
|
||||||
<div class="right-text-content">
|
|
||||||
<ul class="social-icons">
|
|
||||||
<li><p>Follow Us</p></li>
|
|
||||||
<li><a rel="nofollow" href="https://fb.com/templatemo"><i class="fa fa-facebook"></i></a></li>
|
|
||||||
<li><a rel="nofollow" href="https://fb.com/templatemo"><i class="fa fa-twitter"></i></a></li>
|
|
||||||
<li><a rel="nofollow" href="https://fb.com/templatemo"><i class="fa fa-linkedin"></i></a></li>
|
|
||||||
<li><a rel="nofollow" href="https://fb.com/templatemo"><i class="fa fa-dribbble"></i></a></li>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
Hello {{ name }}
|
|
||||||
<!-- ***** Main Banner Area Start ***** -->
|
<!-- ***** Main Banner Area Start ***** -->
|
||||||
<div class="main-banner header-text" id="top">
|
<div class="main-banner header-text" id="top">
|
||||||
<div class="Modern-Slider">
|
<div class="Modern-Slider">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue