@iad-os/react-ghost-auth
Advanced tools
Comparing version 0.1.16 to 0.1.17
import { AxiosStatic } from 'axios'; | ||
import React from 'react'; | ||
import { TokenResponse } from './models/TokenResponse'; | ||
export declare type AuthenticationOptions = { | ||
serviceUrl?: string; | ||
authorization_endpoint: string; | ||
@@ -12,3 +12,4 @@ token_endpoint: string; | ||
end_session_endpoint: string; | ||
realm: string; | ||
client_secret?: string; | ||
serviceUrl?: string; | ||
}; | ||
@@ -30,4 +31,16 @@ declare type EStatus = 'INIT' | 'LOGIN' | 'LOGGING' | 'LOGGED'; | ||
children: React.ReactNode; | ||
onTokenRequest?: (axios: AxiosStatic, data: { | ||
token_endpoint: string; | ||
client_id: string; | ||
redirect_uri: string; | ||
code: string; | ||
code_verifier: string; | ||
}) => Promise<TokenResponse>; | ||
onRefreshTokenRequest?: (axios: AxiosStatic, data: { | ||
token_endpoint: string; | ||
client_id: string; | ||
refresh_token: string; | ||
}) => Promise<TokenResponse>; | ||
}): JSX.Element; | ||
export declare function useAuthentication(): AuthCtxType; | ||
export {}; |
@@ -735,4 +735,4 @@ import { jsx, Fragment, jsxs } from 'react/jsx-runtime'; | ||
function AuthenticationProvider(_props) { | ||
const { axios } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, redirect_uri, redirect_logout_uri, } = _props.options; | ||
const { axios, onTokenRequest, onRefreshTokenRequest } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, serviceUrl, redirect_uri, redirect_logout_uri, client_secret, } = _props.options; | ||
const [status, setStatus] = useState(!!getState() ? 'LOGGING' : 'INIT'); | ||
@@ -746,16 +746,22 @@ useEffect(() => { | ||
setStatus('LOGGING'); | ||
axios | ||
.post(token_endpoint, queryString.stringify({ | ||
client_secret: '', | ||
grant_type: 'authorization_code', | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}) | ||
.then(function ({ data }) { | ||
const tokenRequest = onTokenRequest | ||
? () => onTokenRequest(axios, { | ||
token_endpoint, | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}) | ||
: () => axios | ||
.post(token_endpoint, queryString.stringify(Object.assign({ grant_type: 'authorization_code', client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier }, (client_secret && { client_secret }))), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}) | ||
.then(res => res.data); | ||
tokenRequest() | ||
.then(function (data) { | ||
setToken(data); | ||
@@ -779,13 +785,17 @@ window.location.href = redirect_uri; | ||
const refreshToken = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { data } = yield axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
const refreshTokenFn = onRefreshTokenRequest | ||
? () => onRefreshTokenRequest(axios, { | ||
client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
token_endpoint, | ||
refresh_token: getRefreshToken() || '', | ||
}) | ||
: () => axios | ||
.post(token_endpoint, queryString.stringify(Object.assign({ grant_type: 'refresh_token', client_id, refresh_token: getRefreshToken() }, (client_secret && { client_secret }))), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
setToken(data); | ||
}) | ||
.then(res => res.data); | ||
try { | ||
const data = yield refreshTokenFn(); | ||
setStatus('LOGGED'); | ||
@@ -792,0 +802,0 @@ return data; |
@@ -743,4 +743,4 @@ 'use strict'; | ||
function AuthenticationProvider(_props) { | ||
const { axios } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, realm, serviceUrl, redirect_uri, redirect_logout_uri, } = _props.options; | ||
const { axios, onTokenRequest, onRefreshTokenRequest } = _props; | ||
const { client_id, authorization_endpoint, requested_scopes, token_endpoint, end_session_endpoint, serviceUrl, redirect_uri, redirect_logout_uri, client_secret, } = _props.options; | ||
const [status, setStatus] = React.useState(!!getState() ? 'LOGGING' : 'INIT'); | ||
@@ -754,16 +754,22 @@ React.useEffect(() => { | ||
setStatus('LOGGING'); | ||
axios | ||
.post(token_endpoint, queryString.stringify({ | ||
client_secret: '', | ||
grant_type: 'authorization_code', | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}) | ||
.then(function ({ data }) { | ||
const tokenRequest = onTokenRequest | ||
? () => onTokenRequest(axios, { | ||
token_endpoint, | ||
client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier, | ||
}) | ||
: () => axios | ||
.post(token_endpoint, queryString.stringify(Object.assign({ grant_type: 'authorization_code', client_id, | ||
redirect_uri, | ||
code, | ||
code_verifier }, (client_secret && { client_secret }))), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}) | ||
.then(res => res.data); | ||
tokenRequest() | ||
.then(function (data) { | ||
setToken(data); | ||
@@ -787,13 +793,17 @@ window.location.href = redirect_uri; | ||
const refreshToken = () => __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { data } = yield axios.post(token_endpoint, queryString.stringify({ | ||
grant_type: 'refresh_token', | ||
const refreshTokenFn = onRefreshTokenRequest | ||
? () => onRefreshTokenRequest(axios, { | ||
client_id, | ||
refresh_token: getRefreshToken(), | ||
}), { | ||
token_endpoint, | ||
refresh_token: getRefreshToken() || '', | ||
}) | ||
: () => axios | ||
.post(token_endpoint, queryString.stringify(Object.assign({ grant_type: 'refresh_token', client_id, refresh_token: getRefreshToken() }, (client_secret && { client_secret }))), { | ||
headers: { | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}); | ||
setToken(data); | ||
}) | ||
.then(res => res.data); | ||
try { | ||
const data = yield refreshTokenFn(); | ||
setStatus('LOGGED'); | ||
@@ -800,0 +810,0 @@ return data; |
{ | ||
"name": "@iad-os/react-ghost-auth", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"maintainers": [ | ||
@@ -5,0 +5,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
154789
1677