react-oauth2-pkce
Advanced tools
Comparing version 1.0.3 to 1.0.4
import React from 'react'; | ||
import { AuthServiceProps, AuthService, AuthTokens } from './AuthService'; | ||
import { AuthServiceProps, AuthService } from './AuthService'; | ||
export declare type AuthContextProps = { | ||
authService: AuthService; | ||
authTokens: AuthTokens; | ||
}; | ||
@@ -7,0 +6,0 @@ export declare type AuthContextType = AuthContextProps | undefined; |
import { ReactElement, ReactNode } from 'react'; | ||
import { AuthService, AuthTokens } from './AuthService'; | ||
import { AuthService } from './AuthService'; | ||
interface AuthProviderProps { | ||
children: ReactNode; | ||
authService: AuthService; | ||
authTokens?: AuthTokens; | ||
} | ||
export declare const AuthProvider: (props: AuthProviderProps) => ReactElement; | ||
export {}; |
@@ -37,6 +37,7 @@ import { PKCECodePair } from './pkce'; | ||
isAuthenticated(): boolean; | ||
logout(): Promise<void>; | ||
logout(): Promise<boolean>; | ||
login(): Promise<void>; | ||
authorize(): void; | ||
authorize(): boolean; | ||
fetchToken(code: string): Promise<AuthTokens>; | ||
restoreUri(): void; | ||
} |
@@ -31,26 +31,4 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
children = props.children; | ||
var _useState = React.useState(authService.getAuthTokens()), | ||
authTokens = _useState[0], | ||
setAuthTokens = _useState[1]; | ||
React.useEffect(function () { | ||
var code = authService.getCodeFromLocation(window.location); | ||
if (code !== null) { | ||
authService.fetchToken(code).then(function (tokens) { | ||
console.log({ | ||
tokens: tokens | ||
}); | ||
setAuthTokens(tokens); | ||
})["catch"](function (e) { | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
} | ||
}, [window.location, authService]); | ||
return React__default.createElement(AuthContext.Provider, { | ||
value: { | ||
authTokens: authTokens, | ||
authService: authService | ||
@@ -145,3 +123,22 @@ } | ||
function AuthService(props) { | ||
var _this = this; | ||
this.props = props; | ||
var code = this.getCodeFromLocation(window.location); | ||
if (code !== null) { | ||
this.fetchToken(code).then(function () { | ||
_this.restoreUri(); | ||
})["catch"](function (e) { | ||
_this.removeItem('pkce'); | ||
_this.removeItem('auth'); | ||
_this.removeCodeFromLocation(); | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
} | ||
} | ||
@@ -238,12 +235,10 @@ | ||
try { | ||
var _this2 = this; | ||
var _this3 = this; | ||
var location = _this2.props.location; | ||
_this3.removeItem('pkce'); | ||
_this2.removeItem('pkce'); | ||
_this3.removeItem('auth'); | ||
_this2.removeItem('auth'); | ||
location.reload(); | ||
return Promise.resolve(); | ||
window.location.reload(); | ||
return Promise.resolve(true); | ||
} catch (e) { | ||
@@ -256,5 +251,5 @@ return Promise.reject(e); | ||
try { | ||
var _this4 = this; | ||
var _this5 = this; | ||
_this4.authorize(); | ||
_this5.authorize(); | ||
@@ -275,2 +270,3 @@ return Promise.resolve(); | ||
window.localStorage.setItem('pkce', JSON.stringify(pkce)); | ||
window.localStorage.setItem('preAuthUri', location.href); | ||
window.localStorage.removeItem('auth'); | ||
@@ -287,3 +283,4 @@ var codeChallenge = pkce.codeChallenge; | ||
var url = provider + "/authorize?" + toUrlEncoded(query); | ||
window.location.href = url; | ||
window.location.replace(url); | ||
return true; | ||
}; | ||
@@ -293,18 +290,16 @@ | ||
try { | ||
var _this6 = this; | ||
var _this7 = this; | ||
var _this6$props = _this6.props, | ||
clientId = _this6$props.clientId, | ||
clientSecret = _this6$props.clientSecret, | ||
contentType = _this6$props.contentType, | ||
provider = _this6$props.provider, | ||
redirectUri = _this6$props.redirectUri; | ||
var _this7$props = _this7.props, | ||
clientId = _this7$props.clientId, | ||
clientSecret = _this7$props.clientSecret, | ||
contentType = _this7$props.contentType, | ||
provider = _this7$props.provider, | ||
redirectUri = _this7$props.redirectUri; | ||
var grantType = 'authorization_code'; | ||
var pkce = _this6.getPkce(); | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
_this6.removeCodeFromLocation(); | ||
var payload = _extends(_extends({ | ||
@@ -328,6 +323,6 @@ clientId: clientId | ||
})).then(function (response) { | ||
_this6.removeItem('pkce'); | ||
_this7.removeItem('pkce'); | ||
return Promise.resolve(response.json()).then(function (json) { | ||
_this6.setAuthTokens(json); | ||
_this7.setAuthTokens(json); | ||
@@ -342,2 +337,16 @@ return json; | ||
_proto.restoreUri = function restoreUri() { | ||
var uri = window.localStorage.getItem('preAuthUri'); | ||
window.localStorage.removeItem('preAuthUri'); | ||
console.log({ | ||
uri: uri | ||
}); | ||
if (uri !== null) { | ||
window.location.replace(uri); | ||
} | ||
this.removeCodeFromLocation(); | ||
}; | ||
return AuthService; | ||
@@ -344,0 +353,0 @@ }(); |
@@ -1,2 +0,2 @@ | ||
import React, { useContext, useState, useEffect } from 'react'; | ||
import React, { useContext } from 'react'; | ||
import { randomBytes, createHash } from 'crypto'; | ||
@@ -28,26 +28,4 @@ import jwtDecode from 'jwt-decode'; | ||
children = props.children; | ||
var _useState = useState(authService.getAuthTokens()), | ||
authTokens = _useState[0], | ||
setAuthTokens = _useState[1]; | ||
useEffect(function () { | ||
var code = authService.getCodeFromLocation(window.location); | ||
if (code !== null) { | ||
authService.fetchToken(code).then(function (tokens) { | ||
console.log({ | ||
tokens: tokens | ||
}); | ||
setAuthTokens(tokens); | ||
})["catch"](function (e) { | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
} | ||
}, [window.location, authService]); | ||
return React.createElement(AuthContext.Provider, { | ||
value: { | ||
authTokens: authTokens, | ||
authService: authService | ||
@@ -142,3 +120,22 @@ } | ||
function AuthService(props) { | ||
var _this = this; | ||
this.props = props; | ||
var code = this.getCodeFromLocation(window.location); | ||
if (code !== null) { | ||
this.fetchToken(code).then(function () { | ||
_this.restoreUri(); | ||
})["catch"](function (e) { | ||
_this.removeItem('pkce'); | ||
_this.removeItem('auth'); | ||
_this.removeCodeFromLocation(); | ||
console.warn({ | ||
e: e | ||
}); | ||
}); | ||
} | ||
} | ||
@@ -235,12 +232,10 @@ | ||
try { | ||
var _this2 = this; | ||
var _this3 = this; | ||
var location = _this2.props.location; | ||
_this3.removeItem('pkce'); | ||
_this2.removeItem('pkce'); | ||
_this3.removeItem('auth'); | ||
_this2.removeItem('auth'); | ||
location.reload(); | ||
return Promise.resolve(); | ||
window.location.reload(); | ||
return Promise.resolve(true); | ||
} catch (e) { | ||
@@ -253,5 +248,5 @@ return Promise.reject(e); | ||
try { | ||
var _this4 = this; | ||
var _this5 = this; | ||
_this4.authorize(); | ||
_this5.authorize(); | ||
@@ -272,2 +267,3 @@ return Promise.resolve(); | ||
window.localStorage.setItem('pkce', JSON.stringify(pkce)); | ||
window.localStorage.setItem('preAuthUri', location.href); | ||
window.localStorage.removeItem('auth'); | ||
@@ -284,3 +280,4 @@ var codeChallenge = pkce.codeChallenge; | ||
var url = provider + "/authorize?" + toUrlEncoded(query); | ||
window.location.href = url; | ||
window.location.replace(url); | ||
return true; | ||
}; | ||
@@ -290,18 +287,16 @@ | ||
try { | ||
var _this6 = this; | ||
var _this7 = this; | ||
var _this6$props = _this6.props, | ||
clientId = _this6$props.clientId, | ||
clientSecret = _this6$props.clientSecret, | ||
contentType = _this6$props.contentType, | ||
provider = _this6$props.provider, | ||
redirectUri = _this6$props.redirectUri; | ||
var _this7$props = _this7.props, | ||
clientId = _this7$props.clientId, | ||
clientSecret = _this7$props.clientSecret, | ||
contentType = _this7$props.contentType, | ||
provider = _this7$props.provider, | ||
redirectUri = _this7$props.redirectUri; | ||
var grantType = 'authorization_code'; | ||
var pkce = _this6.getPkce(); | ||
var pkce = _this7.getPkce(); | ||
var codeVerifier = pkce.codeVerifier; | ||
_this6.removeCodeFromLocation(); | ||
var payload = _extends(_extends({ | ||
@@ -325,6 +320,6 @@ clientId: clientId | ||
})).then(function (response) { | ||
_this6.removeItem('pkce'); | ||
_this7.removeItem('pkce'); | ||
return Promise.resolve(response.json()).then(function (json) { | ||
_this6.setAuthTokens(json); | ||
_this7.setAuthTokens(json); | ||
@@ -339,2 +334,16 @@ return json; | ||
_proto.restoreUri = function restoreUri() { | ||
var uri = window.localStorage.getItem('preAuthUri'); | ||
window.localStorage.removeItem('preAuthUri'); | ||
console.log({ | ||
uri: uri | ||
}); | ||
if (uri !== null) { | ||
window.location.replace(uri); | ||
} | ||
this.removeCodeFromLocation(); | ||
}; | ||
return AuthService; | ||
@@ -341,0 +350,0 @@ }(); |
{ | ||
"name": "react-oauth2-pkce", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Authenticate against generic OAuth2 using PKCE", | ||
@@ -5,0 +5,0 @@ "author": "Gardner Bickford <gardner@bickford.nz>", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60590
737