Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhittaker0024 committed Oct 3, 2023
2 parents 5764e2d + fff999e commit 5c61118
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/v2/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ FROM almalinux:latest
RUN dnf -y install \
openssh-clients \
nodejs \
openldap-devel \
rm -rf /var/cache/yum && \z
openldap-devel && \
rm -rf /var/cache/yum && \
npm i -g n && \
n 16

Expand Down
3 changes: 3 additions & 0 deletions src/v2/client/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BROWSER=none
REACT_APP_AUTH_URL=https://engineering.snow.edu/aspen/auth
REACT_APP_BASE_URL=https://localhost:44478/aspen/new
93 changes: 93 additions & 0 deletions src/v2/client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/v2/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"bootstrap": "^5.3.1",
"bootstrap-icons": "^1.11.1",
"eslint": "^8.50.0",
"oidc-client": "^1.11.5",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.1",
Expand Down
21 changes: 21 additions & 0 deletions src/v2/client/src/components/LoginButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { authService } from "../services/authService";

export const LoginButton = () => {
const loginHandler = () => {
authService.signinRedirect();
};
const logoutHandler = () => {
authService.logout();
};


return (
<>
{localStorage.getItem("LoggedInUser") === "" ? (
<button className="btn btn-secondary shadow text-white" onClick={loginHandler}>Login</button>
) : (
<button className="btn btn-secondary shadow text-white" onClick={logoutHandler}>Logout</button>
)}
</>
);
};
3 changes: 2 additions & 1 deletion src/v2/client/src/components/NavBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Link } from "react-router-dom"
import { LoginButton } from "./LoginButton"

export const NavBar = () => {
return (
Expand All @@ -25,7 +26,7 @@ export const NavBar = () => {
<Link to={"/"} className="text-white text-decoration-none">Swagger</Link>
</li>
</ul>
<button className="btn btn-secondary shadow text-white">LOGOUT</button>
<LoginButton />
</div>
</div>
</nav>
Expand Down
100 changes: 100 additions & 0 deletions src/v2/client/src/services/authService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { UserManager, WebStorageStateStore } from "oidc-client";

const authUrl = process.env.REACT_APP_AUTH_URL
var redirectUrl = "/landing/"
var userManager = new UserManager({
userStore: new WebStorageStateStore({ store: window.localStorage }),
authority:
`${authUrl || "https://engineering.snow.edu/aspen/auth"}/realms/aspen/.well-known/openid-configuration`,
client_id: "aspen-web",
redirect_uri: window.location.origin + redirectUrl,
post_logout_redirect_uri: window.location.origin + "/aspen/new/",
silent_redirect_uri: window.location.origin + "/aspen/new/",
response_type: "code",
scope: "openid profile email",
loadUserInfo: true,
automaticSilentRenew: true,
});

userManager.startSilentRenew();

export const authService = {

getUser: async () => {
const user = await userManager.getUser();
return user;
},

isLoggedIn: async () => {
const user = await userManager.getUser();
const loggedIn = user !== null && !user.expired;
return loggedIn;
},

signinRedirect: async () => {

const params = new URLSearchParams(window.location.search)
var redirectUri = window.location.pathname.replace("/aspen/new", "")

localStorage.setItem("redirectUri", redirectUri)
if (window.location.pathname === '/aspen/new/') {
localStorage.setItem("redirectUri", '/');
}
else if(redirectUri === "/TeamDetails"){
localStorage.setItem("redirectUri", `${redirectUri}?teamId=${params.get("teamId")}&ownerID=${params.get("ownerID")} `);
}
await userManager.signinRedirect();
},

signinRedirectCallback: async () => {
const desiredDestination = localStorage.getItem("redirectUri");
const tempDestination = desiredDestination?.replace('/login', '/');
const user = await userManager.signinRedirectCallback();
return { desiredDestination: tempDestination, user };
},

signinSilent: async () => {
await userManager
.signinSilent()
.then((user) => {
})
.catch((err) => {
});
},

signinSilentCallback: async () => {
return await userManager.signinSilentCallback();
},

createSigninRequest: async () => {
return await userManager.createSigninRequest();
},

logout: async () => {
await userManager.clearStaleState();
await userManager.signoutRedirect();
localStorage.setItem("LoggedInUser", "")
localStorage.setItem("LoggedInEmail", "")
localStorage.setItem("access_token", "")

// await userManager.signoutRedirect({
// id_token_hint: localStorage.getItem("id_token"),
// });
},

signoutRedirectCallback: async () => {
await userManager.signoutRedirectCallback().then(() => {
localStorage.clear();
window.location.replace('/');
});
await userManager.clearStaleState();
},

}

userManager.events.addSilentRenewError((e) => {
});

userManager.events.addAccessTokenExpired(() => {
authService.signinSilent();
});

0 comments on commit 5c61118

Please sign in to comment.