@eduzz/accounts-react-wrapper
Advanced tools
Comparing version 0.4.2 to 1.0.0
@@ -19,6 +19,8 @@ "use strict"; | ||
var AuthContext = (0, react_1.createContext)({ | ||
bearerTokenDecoded: null, | ||
loading: true, | ||
bearerToken: null, | ||
bearerTokenDecoded: null, | ||
logout: undefined, | ||
getRefreshTokenFromCookie: utils_1.getRefreshTokenFromCookie, | ||
tryRefreshToken: null, | ||
}); | ||
@@ -35,2 +37,11 @@ function AuthProvider(_a) { | ||
}), bearerTokenDecoded = _b.bearerTokenDecoded, loading = _b.loading, tryRefreshToken = _b.tryRefreshToken, callAccountsLogin = _b.callAccountsLogin, tryLogin = _b.tryLogin, callAccountsLogout = _b.callAccountsLogout, bearerToken = _b.bearerToken; | ||
function tryRefreshTokenCallback(token) { | ||
tryRefreshToken({ | ||
saveRefreshTokenOnCookie: utils_1.saveRefreshTokenOnCookie, | ||
eraseRefreshTokenOnCookie: utils_1.eraseRefreshTokenOnCookie, | ||
callAccountsLogout: callAccountsLogout, | ||
refreshToken: token, | ||
refreshResponseDefinitions: refreshResponseDefinitions, | ||
}); | ||
} | ||
(0, react_1.useEffect)(function () { | ||
@@ -40,9 +51,3 @@ var refreshToken = (0, utils_1.getRefreshTokenFromCookie)(); | ||
if (refreshToken && !isSupport) { | ||
tryRefreshToken({ | ||
saveRefreshTokenOnCookie: utils_1.saveRefreshTokenOnCookie, | ||
eraseRefreshTokenOnCookie: utils_1.eraseRefreshTokenOnCookie, | ||
callAccountsLogout: callAccountsLogout, | ||
refreshToken: refreshToken, | ||
refreshResponseDefinitions: refreshResponseDefinitions, | ||
}); | ||
tryRefreshTokenCallback(refreshToken); | ||
} | ||
@@ -62,2 +67,4 @@ else { | ||
logout: callAccountsLogout, | ||
tryRefreshToken: tryRefreshTokenCallback, | ||
getRefreshTokenFromCookie: utils_1.getRefreshTokenFromCookie, | ||
} }, { children: children }), void 0)); | ||
@@ -64,0 +71,0 @@ } |
@@ -54,5 +54,5 @@ "use strict"; | ||
bearerToken: null, | ||
logout: undefined, | ||
}; | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)(condition) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
var conditionGenerated = (0, mocks_1.AuthProviderPropsMock)({ condition: condition }); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: conditionGenerated }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
expect(test_tools_1.screen.getByTestId('status')).toHaveTextContent('loading...'); | ||
@@ -70,3 +70,3 @@ expect(test_tools_1.screen.getByTestId('user')).toHaveTextContent(''); | ||
}; | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)(condition) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)({ condition: condition }) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
expect(test_tools_1.screen.getByTestId('status')).toHaveTextContent('loaded'); | ||
@@ -86,3 +86,3 @@ expect(test_tools_1.screen.getByTestId('user')).toHaveTextContent(bearerTokenDecoded.name); | ||
mock.mockImplementation(function () { return 'teste'; }); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)(condition) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)({ condition: condition }) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
expect(mock).toHaveBeenCalled(); | ||
@@ -101,3 +101,3 @@ expect(mocks_1.tryRefreshTokenFn).toHaveBeenCalled(); | ||
mock.mockImplementation(function () { return undefined; }); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)(condition) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: (0, mocks_1.AuthProviderPropsMock)({ condition: condition }) }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
expect(mock).toHaveBeenCalled(); | ||
@@ -120,3 +120,3 @@ expect(mocks_1.callAccountsLoginFn).toHaveBeenCalled(); | ||
.mockImplementation(function () { return true; }); | ||
var config = (0, mocks_1.AuthProviderPropsMock)(condition); | ||
var config = (0, mocks_1.AuthProviderPropsMock)({ condition: condition }); | ||
(0, test_tools_1.render)((0, jsx_runtime_1.jsx)(index_1.AuthProvider, __assign({ config: config }, { children: (0, jsx_runtime_1.jsx)(Component, {}, void 0) }), void 0)); | ||
@@ -123,0 +123,0 @@ expect(mockGetRefreshToken).toHaveBeenCalled(); |
/// <reference types="jest" /> | ||
import { AuthProviderConfig, AuthContextInterface, LoginServiceType, RefreshServiceType, BackOfficeConfigType } from './types'; | ||
import { AuthProviderConfig, LoginServiceType, RefreshServiceType, CallAccountsLoginParams, TryRefreshTokenParams, TryLoginParams } from './types'; | ||
declare const subscribeFn: jest.Mock<any, any>; | ||
@@ -14,20 +14,23 @@ declare const logoutFn: jest.Mock<any, any>; | ||
declare const mockUrl: string; | ||
declare const authenticationSpy: ({ loading, bearerTokenDecoded, bearerToken, }: AuthContextInterface<any>) => { | ||
declare type AuthenticationSpyType = { | ||
loading: boolean; | ||
bearerTokenDecoded: any; | ||
bearerToken: string | null; | ||
bearerTokenDecoded: any; | ||
callAccountsLogin: jest.Mock<any, any>; | ||
tryRefreshToken: jest.Mock<any, any>; | ||
tryLogin: jest.Mock<any, any>; | ||
callAccountsLogout: jest.Mock<any, any>; | ||
callAccountsLogin: (params: CallAccountsLoginParams) => void; | ||
callAccountsLogout: () => void; | ||
tryRefreshToken: (params: TryRefreshTokenParams) => Promise<void>; | ||
tryLogin: (params: TryLoginParams) => Promise<void>; | ||
}; | ||
declare const AuthProviderPropsMock: (condition: { | ||
declare const authenticationSpy: ({ loading, bearerTokenDecoded, bearerToken, callAccountsLogin, callAccountsLogout, tryRefreshToken, tryLogin, }: AuthenticationSpyType) => { | ||
loading: boolean; | ||
bearerTokenDecoded: any | null; | ||
bearerToken: string | null; | ||
logout: undefined | (() => void); | ||
backOffice?: BackOfficeConfigType; | ||
}) => AuthProviderConfig; | ||
bearerTokenDecoded: any; | ||
callAccountsLogout: () => void; | ||
callAccountsLogin: (params: CallAccountsLoginParams) => void; | ||
tryRefreshToken: (params: TryRefreshTokenParams) => Promise<void>; | ||
tryLogin: (params: TryLoginParams) => Promise<void>; | ||
}; | ||
declare const AuthProviderPropsMock: ({ condition }: any) => AuthProviderConfig; | ||
declare const loginService: LoginServiceType; | ||
declare const refreshService: RefreshServiceType; | ||
export { mockUrl, subscribeFn, logoutFn, authenticationSpy, loginFn, tryLoginFn, replaceStateFn, callAccountsLogoutFn, eraseAccountsLogoutFn, saveRefreshTokenOnCookieFn, AuthProviderPropsMock, tryRefreshTokenFn, callAccountsLoginFn, loginService, refreshService, }; |
@@ -27,15 +27,2 @@ "use strict"; | ||
exports.mockUrl = mockUrl; | ||
var authenticationSpy = function (_a) { | ||
var loading = _a.loading, bearerTokenDecoded = _a.bearerTokenDecoded, bearerToken = _a.bearerToken; | ||
return { | ||
loading: loading, | ||
bearerToken: bearerToken, | ||
bearerTokenDecoded: bearerTokenDecoded, | ||
callAccountsLogin: callAccountsLoginFn, | ||
tryRefreshToken: tryRefreshTokenFn, | ||
tryLogin: jest.fn(), | ||
callAccountsLogout: jest.fn(), | ||
}; | ||
}; | ||
exports.authenticationSpy = authenticationSpy; | ||
var location = { | ||
@@ -55,4 +42,18 @@ href: mockUrl + "?accounts_token=\"1234\"", | ||
}); | ||
var AuthProviderPropsMock = function (condition) { | ||
var authenticationSpy = function (_a) { | ||
var loading = _a.loading, bearerTokenDecoded = _a.bearerTokenDecoded, bearerToken = _a.bearerToken, callAccountsLogin = _a.callAccountsLogin, callAccountsLogout = _a.callAccountsLogout, tryRefreshToken = _a.tryRefreshToken, tryLogin = _a.tryLogin; | ||
return { | ||
loading: loading, | ||
bearerToken: bearerToken, | ||
bearerTokenDecoded: bearerTokenDecoded, | ||
callAccountsLogout: callAccountsLogout || callAccountsLogoutFn, | ||
callAccountsLogin: callAccountsLogin || callAccountsLoginFn, | ||
tryRefreshToken: tryRefreshToken || tryRefreshTokenFn, | ||
tryLogin: tryLogin || tryLoginFn, | ||
}; | ||
}; | ||
exports.authenticationSpy = authenticationSpy; | ||
var AuthProviderPropsMock = function (_a) { | ||
var condition = _a.condition; | ||
return { | ||
accounts: { | ||
@@ -67,5 +68,2 @@ env: '', | ||
}, | ||
backOffice: condition.backOffice || { | ||
urlParam: 'accounts_login_token', | ||
}, | ||
authentication: function () { return authenticationSpy(condition); }, | ||
@@ -72,0 +70,0 @@ }; |
@@ -7,2 +7,4 @@ /// <reference types="react" /> | ||
logout?: () => void; | ||
tryRefreshToken: ((token: string) => void) | null; | ||
getRefreshTokenFromCookie: () => string | undefined; | ||
} | ||
@@ -9,0 +11,0 @@ export declare type AuthProviderProps = { |
@@ -13,3 +13,3 @@ "use strict"; | ||
function saveRefreshTokenOnCookie(refreshToken) { | ||
document.cookie = "refreshToken=" + refreshToken; | ||
document.cookie = "refreshToken=" + refreshToken + "; path=/; expires=Session"; | ||
} | ||
@@ -16,0 +16,0 @@ exports.saveRefreshTokenOnCookie = saveRefreshTokenOnCookie; |
@@ -16,6 +16,8 @@ var __assign = (this && this.__assign) || function () { | ||
var AuthContext = createContext({ | ||
bearerTokenDecoded: null, | ||
loading: true, | ||
bearerToken: null, | ||
bearerTokenDecoded: null, | ||
logout: undefined, | ||
getRefreshTokenFromCookie: getRefreshTokenFromCookie, | ||
tryRefreshToken: null, | ||
}); | ||
@@ -32,2 +34,11 @@ function AuthProvider(_a) { | ||
}), bearerTokenDecoded = _b.bearerTokenDecoded, loading = _b.loading, tryRefreshToken = _b.tryRefreshToken, callAccountsLogin = _b.callAccountsLogin, tryLogin = _b.tryLogin, callAccountsLogout = _b.callAccountsLogout, bearerToken = _b.bearerToken; | ||
function tryRefreshTokenCallback(token) { | ||
tryRefreshToken({ | ||
saveRefreshTokenOnCookie: saveRefreshTokenOnCookie, | ||
eraseRefreshTokenOnCookie: eraseRefreshTokenOnCookie, | ||
callAccountsLogout: callAccountsLogout, | ||
refreshToken: token, | ||
refreshResponseDefinitions: refreshResponseDefinitions, | ||
}); | ||
} | ||
useEffect(function () { | ||
@@ -37,9 +48,3 @@ var refreshToken = getRefreshTokenFromCookie(); | ||
if (refreshToken && !isSupport) { | ||
tryRefreshToken({ | ||
saveRefreshTokenOnCookie: saveRefreshTokenOnCookie, | ||
eraseRefreshTokenOnCookie: eraseRefreshTokenOnCookie, | ||
callAccountsLogout: callAccountsLogout, | ||
refreshToken: refreshToken, | ||
refreshResponseDefinitions: refreshResponseDefinitions, | ||
}); | ||
tryRefreshTokenCallback(refreshToken); | ||
} | ||
@@ -59,2 +64,4 @@ else { | ||
logout: callAccountsLogout, | ||
tryRefreshToken: tryRefreshTokenCallback, | ||
getRefreshTokenFromCookie: getRefreshTokenFromCookie, | ||
} }, { children: children }), void 0)); | ||
@@ -61,0 +68,0 @@ } |
@@ -33,5 +33,5 @@ var __assign = (this && this.__assign) || function () { | ||
bearerToken: null, | ||
logout: undefined, | ||
}; | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock(condition) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
var conditionGenerated = AuthProviderPropsMock({ condition: condition }); | ||
render(_jsx(AuthProvider, __assign({ config: conditionGenerated }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
expect(screen.getByTestId('status')).toHaveTextContent('loading...'); | ||
@@ -49,3 +49,3 @@ expect(screen.getByTestId('user')).toHaveTextContent(''); | ||
}; | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock(condition) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock({ condition: condition }) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
expect(screen.getByTestId('status')).toHaveTextContent('loaded'); | ||
@@ -65,3 +65,3 @@ expect(screen.getByTestId('user')).toHaveTextContent(bearerTokenDecoded.name); | ||
mock.mockImplementation(function () { return 'teste'; }); | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock(condition) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock({ condition: condition }) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
expect(mock).toHaveBeenCalled(); | ||
@@ -80,3 +80,3 @@ expect(tryRefreshTokenFn).toHaveBeenCalled(); | ||
mock.mockImplementation(function () { return undefined; }); | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock(condition) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
render(_jsx(AuthProvider, __assign({ config: AuthProviderPropsMock({ condition: condition }) }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
expect(mock).toHaveBeenCalled(); | ||
@@ -99,3 +99,3 @@ expect(callAccountsLoginFn).toHaveBeenCalled(); | ||
.mockImplementation(function () { return true; }); | ||
var config = AuthProviderPropsMock(condition); | ||
var config = AuthProviderPropsMock({ condition: condition }); | ||
render(_jsx(AuthProvider, __assign({ config: config }, { children: _jsx(Component, {}, void 0) }), void 0)); | ||
@@ -102,0 +102,0 @@ expect(mockGetRefreshToken).toHaveBeenCalled(); |
/// <reference types="jest" /> | ||
import { AuthProviderConfig, AuthContextInterface, LoginServiceType, RefreshServiceType, BackOfficeConfigType } from './types'; | ||
import { AuthProviderConfig, LoginServiceType, RefreshServiceType, CallAccountsLoginParams, TryRefreshTokenParams, TryLoginParams } from './types'; | ||
declare const subscribeFn: jest.Mock<any, any>; | ||
@@ -14,20 +14,23 @@ declare const logoutFn: jest.Mock<any, any>; | ||
declare const mockUrl: string; | ||
declare const authenticationSpy: ({ loading, bearerTokenDecoded, bearerToken, }: AuthContextInterface<any>) => { | ||
declare type AuthenticationSpyType = { | ||
loading: boolean; | ||
bearerTokenDecoded: any; | ||
bearerToken: string | null; | ||
bearerTokenDecoded: any; | ||
callAccountsLogin: jest.Mock<any, any>; | ||
tryRefreshToken: jest.Mock<any, any>; | ||
tryLogin: jest.Mock<any, any>; | ||
callAccountsLogout: jest.Mock<any, any>; | ||
callAccountsLogin: (params: CallAccountsLoginParams) => void; | ||
callAccountsLogout: () => void; | ||
tryRefreshToken: (params: TryRefreshTokenParams) => Promise<void>; | ||
tryLogin: (params: TryLoginParams) => Promise<void>; | ||
}; | ||
declare const AuthProviderPropsMock: (condition: { | ||
declare const authenticationSpy: ({ loading, bearerTokenDecoded, bearerToken, callAccountsLogin, callAccountsLogout, tryRefreshToken, tryLogin, }: AuthenticationSpyType) => { | ||
loading: boolean; | ||
bearerTokenDecoded: any | null; | ||
bearerToken: string | null; | ||
logout: undefined | (() => void); | ||
backOffice?: BackOfficeConfigType; | ||
}) => AuthProviderConfig; | ||
bearerTokenDecoded: any; | ||
callAccountsLogout: () => void; | ||
callAccountsLogin: (params: CallAccountsLoginParams) => void; | ||
tryRefreshToken: (params: TryRefreshTokenParams) => Promise<void>; | ||
tryLogin: (params: TryLoginParams) => Promise<void>; | ||
}; | ||
declare const AuthProviderPropsMock: ({ condition }: any) => AuthProviderConfig; | ||
declare const loginService: LoginServiceType; | ||
declare const refreshService: RefreshServiceType; | ||
export { mockUrl, subscribeFn, logoutFn, authenticationSpy, loginFn, tryLoginFn, replaceStateFn, callAccountsLogoutFn, eraseAccountsLogoutFn, saveRefreshTokenOnCookieFn, AuthProviderPropsMock, tryRefreshTokenFn, callAccountsLoginFn, loginService, refreshService, }; |
@@ -13,14 +13,2 @@ import { internet } from 'faker'; | ||
var mockUrl = internet.url(); | ||
var authenticationSpy = function (_a) { | ||
var loading = _a.loading, bearerTokenDecoded = _a.bearerTokenDecoded, bearerToken = _a.bearerToken; | ||
return { | ||
loading: loading, | ||
bearerToken: bearerToken, | ||
bearerTokenDecoded: bearerTokenDecoded, | ||
callAccountsLogin: callAccountsLoginFn, | ||
tryRefreshToken: tryRefreshTokenFn, | ||
tryLogin: jest.fn(), | ||
callAccountsLogout: jest.fn(), | ||
}; | ||
}; | ||
var location = { | ||
@@ -40,4 +28,17 @@ href: mockUrl + "?accounts_token=\"1234\"", | ||
}); | ||
var AuthProviderPropsMock = function (condition) { | ||
var authenticationSpy = function (_a) { | ||
var loading = _a.loading, bearerTokenDecoded = _a.bearerTokenDecoded, bearerToken = _a.bearerToken, callAccountsLogin = _a.callAccountsLogin, callAccountsLogout = _a.callAccountsLogout, tryRefreshToken = _a.tryRefreshToken, tryLogin = _a.tryLogin; | ||
return { | ||
loading: loading, | ||
bearerToken: bearerToken, | ||
bearerTokenDecoded: bearerTokenDecoded, | ||
callAccountsLogout: callAccountsLogout || callAccountsLogoutFn, | ||
callAccountsLogin: callAccountsLogin || callAccountsLoginFn, | ||
tryRefreshToken: tryRefreshToken || tryRefreshTokenFn, | ||
tryLogin: tryLogin || tryLoginFn, | ||
}; | ||
}; | ||
var AuthProviderPropsMock = function (_a) { | ||
var condition = _a.condition; | ||
return { | ||
accounts: { | ||
@@ -52,5 +53,2 @@ env: '', | ||
}, | ||
backOffice: condition.backOffice || { | ||
urlParam: 'accounts_login_token', | ||
}, | ||
authentication: function () { return authenticationSpy(condition); }, | ||
@@ -57,0 +55,0 @@ }; |
@@ -7,2 +7,4 @@ /// <reference types="react" /> | ||
logout?: () => void; | ||
tryRefreshToken: ((token: string) => void) | null; | ||
getRefreshTokenFromCookie: () => string | undefined; | ||
} | ||
@@ -9,0 +11,0 @@ export declare type AuthProviderProps = { |
@@ -9,3 +9,3 @@ function getRefreshTokenFromCookie() { | ||
function saveRefreshTokenOnCookie(refreshToken) { | ||
document.cookie = "refreshToken=" + refreshToken; | ||
document.cookie = "refreshToken=" + refreshToken + "; path=/; expires=Session"; | ||
} | ||
@@ -12,0 +12,0 @@ function eraseRefreshTokenOnCookie() { |
{ | ||
"name": "@eduzz/accounts-react-wrapper", | ||
"version": "0.4.2", | ||
"version": "1.0.0", | ||
"main": "./lib/cjs/index.js", | ||
@@ -11,3 +11,4 @@ "module": "./lib/esm/index.js", | ||
"/lib", | ||
"README.md" | ||
"README.md", | ||
"CHANGELOG.md" | ||
], | ||
@@ -14,0 +15,0 @@ "scripts": { |
@@ -17,4 +17,8 @@ # Eduzz Accounts React Wrapper | ||
## Tabela de conteúdos | ||
## Play with | ||
https://codesandbox.io/s/accounts-sdk-example-vh0g0?file=/src/index.tsx | ||
## Sumário | ||
--- | ||
@@ -21,0 +25,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
131620
55
2670
1
219
0