Skip to content

Commit

Permalink
Add changes to logged in function to run each time user logs in/out t…
Browse files Browse the repository at this point in the history
…o alter navbar.

Relates #44.

Co-authored-by: Giovanna <[email protected]>
  • Loading branch information
HettieM and glrta committed Apr 24, 2020
1 parent 3667a3b commit 6c1fc5d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 42 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import router from "./router.js";
import isLoggedIn from "./auth-status.js";
isLoggedIn();

//Other
import home from "./routes/home.js";
Expand Down
27 changes: 10 additions & 17 deletions auth-status.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
const header = document.querySelector("#navBar");
const token = localStorage.getItem("token");

function isLoggedIn() {
const link1 = document.createElement("a");
const link2 = document.createElement("a");
if (token) {
link1.href = "/log-out";
link1.textContent = "Log Out";
link1.setAttribute("id", "logOutBtn");
link2.href = "/user"; // View my dogs
link2.textContent = "View My Dogs";
link2.setAttribute("id", "viewMyDogsBtn");
} else {
const link1 = document.querySelector("#link1");
const link2 = document.querySelector("#link2");
const token = localStorage.getItem("token");
if (!token) {
link1.href = "/log-in";
link1.textContent = "Log In";
link1.setAttribute("id", "logInBtn");
link2.href = "/sign-up";
link2.textContent = "Sign Up";
link2.setAttribute("id", "signUpBtn");
} else {
link1.href = "/log-out";
link1.textContent = "Log Out";
link2.href = "/user"; // View my dogs
link2.textContent = "View My Dogs";
}

header.append(link1);
header.append(link2);
}

isLoggedIn();
export default isLoggedIn;
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- <script defer type="module" src="./auth-status.js"></script> -->
<script defer type="module" src="./app.js"></script>
<title>API-Interface</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="style.css" />
</head>

<body>

<nav id="navBar" class="navbar">
<a href="/">Home</a>
<script defer src="./auth-status.js"></script>
<a id="link1" href="#"></a>
<a id="link2" href="#"></a>
</nav>
<div id="app" class="app-container">
<!-- Append from routes -->
</div>

</body>
</html>
6 changes: 3 additions & 3 deletions routes/all-dogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import query from "../query.js";
const app = document.querySelector("#app");

// The secret code.
let audio = document.createElement("audio");
audio.src = "../surprise/who-let-the-dogs-out-ringtone.mp3";
// let audio = document.createElement("audio");
// audio.src = "../surprise/who-let-the-dogs-out-ringtone.mp3";

const html = /*html*/ `
<h1>Our Fury Friends!</h1>
Expand Down Expand Up @@ -35,7 +35,7 @@ function createDogElement(dog) {
function allDogs() {
query("https://dogs-rest.herokuapp.com/v1/dogs")
.then(arrayDogs => {
audio.play();
// audio.play();
app.innerHTML = html;
const addDog = document.querySelector("#addDog");
const token = localStorage.getItem("token");
Expand Down
2 changes: 2 additions & 0 deletions routes/log-in.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import query from "../query.js";
import isLoggedIn from "../auth-status.js";

const app = document.querySelector("#app");

Expand Down Expand Up @@ -32,6 +33,7 @@ function logIn({redirect}) {
.then(body => {
window.localStorage.setItem("token", body.access_token);
window.localStorage.setItem("userId", body.id);
isLoggedIn();
redirect("/dogs"); // After login go to show all dogs
})
.catch(error => {
Expand Down
24 changes: 6 additions & 18 deletions routes/log-out.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
import query from "../query.js";
const app = document.querySelector("#app");
import isLoggedIn from "../auth-status.js";

// const html; /*html template literal*/

function logOut() {
const button = document.querySelector("#logOutBtn");
function logOut({redirect}) {
window.localStorage.removeItem("id");
window.localStorage.removeItem("token");
isLoggedIn();
redirect("/");
}

export default logOut;

function home({redirect}) {
const token = localStorage.getItem("token");
if (!token) {
app.innerHTML = loggedOut;
} else {
app.innerHTML = loggedIn;
app.querySelector("#logOut").addEventListener("click", () => {
window.localStorage.removeItem("token");
redirect("/");
});
}
}

0 comments on commit 6c1fc5d

Please sign in to comment.