Created
October 16, 2019 07:55
-
-
Save iwan-uschka/36e4c0277e545fbf784f78000c511089 to your computer and use it in GitHub Desktop.
Keycloak vs. React
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
import Keycloak from 'keycloak-js'; | |
class Auth { | |
constructor() { | |
this.authenticated = false; | |
this.keycloak = Keycloak('/keycloak.json'); | |
} | |
login(callback) { | |
this.keycloak | |
.init({ onLoad: 'login-required' }) | |
.success((authenticated) => { | |
this.authenticated = authenticated; | |
callback(this.authenticated); | |
}); | |
} | |
logout() { | |
this.authenticated = false; | |
this.keycloak.logout(); | |
} | |
isAuthenticated() { | |
return this.authenticated; | |
} | |
getToken() { | |
return this.keycloak.token; | |
} | |
refreshToken(minValidity = 5) { | |
return new Promise((resolve, reject) => { | |
this.keycloak.updateToken(minValidity) | |
.success(() => resolve()) | |
.error(error => reject(error)); | |
}); | |
} | |
userInfo(callback) { | |
this.keycloak.loadUserInfo().success((userInfo) => { | |
callback(userInfo.name, userInfo.email, userInfo.sub); | |
}); | |
} | |
} | |
export default new Auth(); |
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
import axios from 'axios'; | |
class AxiosInstance { | |
create({ headers }) { | |
this.instance = axios.create({ | |
baseURL: '', | |
headers: { | |
...headers, | |
Accept: 'application/json', | |
'X-Requested-With': 'XMLHttpRequest', | |
}, | |
}); | |
return this.instance; | |
} | |
get() { | |
return this.instance; | |
} | |
} | |
export default new AxiosInstance(); |
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import App from './app/components/App'; | |
import Auth from './app/service/Auth'; | |
import AxiosInstance from './app/service/AxiosInstance'; | |
Auth.login((authenticated) => { | |
if (authenticated) { | |
AxiosInstance.create({ | |
headers: { | |
Authorization: `Bearer ${Auth.getToken()}`, | |
}, | |
}).interceptors.request.use((config) => { | |
return Auth.refreshToken() | |
.then(() => { | |
config.headers.Authorization = `Bearer ${Auth.getToken()}`; | |
return Promise.resolve(config); | |
}) | |
.catch(() => { | |
Auth.login(); | |
}); | |
}); | |
ReactDOM.render( | |
<App />, | |
document.getElementById('root'), | |
); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment