Created
June 13, 2019 13:24
-
-
Save leonmwandiringa/42234e9bd1ceba858d876eda3289e473 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* @uses main ad auth logic wrapper | |
* @return ad methods | |
*/ | |
import axios from "axios"; | |
import { AuthenticationContext, adalGetToken, withAdalLogin, adalFetch } from 'react-adal'; | |
import adalConfig from "./config"; | |
export const authContext = new AuthenticationContext(adalConfig); | |
export const getToken = () => { | |
return authContext.getCachedToken(authContext.config.clientId); | |
}; | |
export const LogOut = () => { | |
authContext.logOut(); | |
}; | |
export const adalApiFetch = (fetch, url, options) => | |
adalFetch(authContext, adalConfig.endpoints.api, fetch, url, options); | |
export const Authuser = authContext.getCachedUser(); | |
export const authenticateUserToSevices = async ()=>{ | |
try{ | |
let authResponse = await axios.post("http://a1162d0ea8d2611e990a60ee3bcedfc5-645222381.eu-north-1.elb.amazonaws.com:5000/api/Auth", { | |
name: Authuser.profile.name, | |
oid: Authuser.profile.oid, | |
userName: Authuser.userName, | |
unique_name: Authuser.profile.unique_name, | |
email: Authuser.profile.email | |
}); | |
//save token and data in session storage | |
sessionStorage.setItem("auth_user", JSON.stringify(authResponse.data.data.data)); | |
sessionStorage.setItem("auth_token", String(authResponse.data.data.token)); | |
axios.defaults.headers.common['Authorization'] = `Bearer ${String(authResponse.data.data.token)}`; | |
}catch(err){ | |
console.log(err) | |
} | |
} | |
export const withAdLogin = withAdalLogin(authContext, adalConfig.endpoints.api); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment