Created
May 30, 2019 19:27
-
-
Save rilleralle/b28574ec1c4cfe10ec7b05809514344b to your computer and use it in GitHub Desktop.
Example for react-admin to authenticate with OAuth2 against GitLab as the Authorization Server
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 { AUTH_LOGIN } from 'react-admin'; | |
export default (type, params) => { | |
if (type === AUTH_LOGIN) { | |
const { username, password } = params; | |
const oAuthParams = { | |
grant_type: "password", | |
username, | |
password | |
} | |
const body = Object.keys(oAuthParams).map((key) => { | |
return encodeURIComponent(key) + '=' + encodeURIComponent(oAuthParams[key]); | |
}).join('&'); | |
const request = new Request('https://gitlab.com/oauth/token', { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' | |
}, | |
body | |
}) | |
return fetch(request) | |
.then(response => { | |
if (response.status < 200 || response.status >= 300) { | |
throw new Error(response.statusText); | |
} | |
return response.json(); | |
}) | |
.then(( {access_token} ) => { | |
localStorage.setItem('token', access_token); | |
}); | |
} | |
return Promise.resolve(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment