Skip to content

Instantly share code, notes, and snippets.

@leonmwandiringa
Created June 13, 2019 13:24
Show Gist options
  • Save leonmwandiringa/42234e9bd1ceba858d876eda3289e473 to your computer and use it in GitHub Desktop.
Save leonmwandiringa/42234e9bd1ceba858d876eda3289e473 to your computer and use it in GitHub Desktop.
/**
* @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