Last active
December 2, 2020 15:25
-
-
Save Franpastoragusti/57bd1cb63e0e08988a0fe4fadbaa6c70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IDENTITY_CONFIG, METADATA_OIDC } from "../utils/authConst"; | |
import { UserManager, WebStorageStateStore, Log } from "oidc-client"; | |
export default class AuthService { | |
UserManager; | |
constructor() { | |
this.UserManager = new UserManager({ | |
...IDENTITY_CONFIG, | |
userStore: new WebStorageStateStore({ store: window.sessionStorage }), | |
metadata: { | |
...METADATA_OIDC | |
} | |
}); | |
// Logger | |
Log.logger = console; | |
Log.level = Log.DEBUG; | |
this.UserManager.events.addUserLoaded((user) => { | |
if (window.location.href.indexOf("signin-oidc") !== -1) { | |
this.navigateToScreen(); | |
} | |
}); | |
this.UserManager.events.addSilentRenewError((e) => { | |
console.log("silent renew error", e.message); | |
}); | |
this.UserManager.events.addAccessTokenExpired(() => { | |
console.log("token expired"); | |
this.signinSilent(); | |
}); | |
} | |
signinRedirectCallback = () => { | |
this.UserManager.signinRedirectCallback().then(() => { | |
""; | |
}); | |
}; | |
getUser = async () => { | |
const user = await this.UserManager.getUser(); | |
if (!user) { | |
return await this.UserManager.signinRedirectCallback(); | |
} | |
return user; | |
}; | |
parseJwt = (token) => { | |
const base64Url = token.split(".")[1]; | |
const base64 = base64Url.replace("-", "+").replace("_", "/"); | |
return JSON.parse(window.atob(base64)); | |
}; | |
signinRedirect = () => { | |
localStorage.setItem("redirectUri", window.location.pathname); | |
this.UserManager.signinRedirect({}); | |
}; | |
navigateToScreen = () => { | |
window.location.replace("/en/dashboard"); | |
}; | |
isAuthenticated = () => { | |
const oidcStorage = JSON.parse(sessionStorage.getItem(`oidc.user:${process.env.REACT_APP_AUTH_URL}:${process.env.REACT_APP_IDENTITY_CLIENT_ID}`)) | |
return (!!oidcStorage && !!oidcStorage.access_token) | |
}; | |
signinSilent = () => { | |
this.UserManager.signinSilent() | |
.then((user) => { | |
console.log("signed in", user); | |
}) | |
.catch((err) => { | |
console.log(err); | |
}); | |
}; | |
signinSilentCallback = () => { | |
this.UserManager.signinSilentCallback(); | |
}; | |
createSigninRequest = () => { | |
return this.UserManager.createSigninRequest(); | |
}; | |
logout = () => { | |
this.UserManager.signoutRedirect({ | |
id_token_hint: localStorage.getItem("id_token") | |
}); | |
this.UserManager.clearStaleState(); | |
}; | |
signoutRedirectCallback = () => { | |
this.UserManager.signoutRedirectCallback().then(() => { | |
localStorage.clear(); | |
window.location.replace(process.env.REACT_APP_PUBLIC_URL); | |
}); | |
this.UserManager.clearStaleState(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment