Finsh discord login/logout process

This commit is contained in:
Chris Jean-Marie 2022-03-25 20:12:03 -04:00
parent c35a999fd8
commit 371661ddad
3 changed files with 33 additions and 42 deletions

View File

@ -80,6 +80,7 @@ async fn main() {
.route("/google_auth", get(google_auth))
.route("/discord_auth", get(discord_auth))
.route("/login", get(login))
.route("/logout", get(logout))
.route("/dashboard", get(dashboard))
.route("/auth/callback", get(google_authorized))
.route("/auth/discord", get(discord_authorized))
@ -98,17 +99,26 @@ async fn main() {
// Session is optional
async fn index(user: Option<User>) -> impl IntoResponse {
let name = match user {
Some(u) => u.username,
None => "You're not logged in.".to_string(),
};
let template = IndexTemplate { name };
let (userid, name) = match user {
Some(u) => (true, u.username),
None => (false, "You're not logged in.".to_string()),
};
let template = IndexTemplate { userid, name };
HtmlTemplate(template)
}
async fn login() -> impl IntoResponse {
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)
}
@ -157,8 +167,17 @@ fn discord_oauth_client() -> BasicClient {
#[derive(Template)]
#[template(path = "login.html")]
struct LoginTemplate {
userid: bool,
name: String,
}
#[derive(Template)]
#[template(path = "dashboard.html")]
struct DashboardTemplate {
userid: bool,
name: String,
}
struct FreshUserId {
pub user_id: UserId,
pub cookie: HeaderValue,
@ -248,6 +267,7 @@ impl UserId {
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
userid: bool,
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(
Extension(store): Extension<MemoryStore>,
TypedHeader(cookies): TypedHeader<headers::Cookie>,

View File

@ -43,13 +43,14 @@
<!-- ***** Logo End ***** -->
<!-- ***** Menu Start ***** -->
<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 class=""><a href="/login">Login</a></li>
</ul>
<a class='menu-trigger'>
<span>Menu</span>
</a>
<!-- ***** Menu End ***** -->
</nav>
</div>
@ -62,28 +63,7 @@
<!-- ***** Footer Start ***** -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-6 col-xs-12">
<div class="left-text-content">
<p>Copyright &copy; 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>

View File

@ -1,6 +1,5 @@
{% extends "base.html" %}
{% block content %}
Hello {{ name }}
<!-- ***** Main Banner Area Start ***** -->
<div class="main-banner header-text" id="top">
<div class="Modern-Slider">