react-auth-kit
Advanced tools
Comparing version 3.0.0-alpha.39 to 3.0.0-alpha.40
@@ -24,2 +24,4 @@ import * as React from 'react'; | ||
* | ||
* @returns React Functional component with React Auth Kit Recharged. | ||
* | ||
* @remarks | ||
@@ -36,4 +38,2 @@ * Make sure you wrap your application as well as your router components in AuthProvider. | ||
* | ||
* | ||
* @returns React Functional component with React Auth Kit Recharged. | ||
*/ | ||
@@ -40,0 +40,0 @@ declare function AuthProvider<T extends object>({ store, children, }: AuthProviderProps<T>): ReturnType<React.FC>; |
@@ -1,4 +0,95 @@ | ||
import type { createRefreshParamInterface } from './types'; | ||
/** | ||
* Payload for Refresh token | ||
*/ | ||
export interface RefreshTokenActionPayload<T> { | ||
/** | ||
* New Auth token from the network response | ||
*/ | ||
newAuthToken: string; | ||
/** | ||
* New Auth Token type from the network response | ||
*/ | ||
newAuthTokenType?: string; | ||
/** | ||
* New Refresh token from the nwtwork response. Can be null | ||
*/ | ||
newRefreshToken?: string; | ||
/** | ||
* New User state from the network. Can be null | ||
*/ | ||
newAuthUserState?: T | null; | ||
} | ||
/** | ||
* Refresh Token Callback Response | ||
*/ | ||
interface RefreshTokenCallbackResponse<T> extends RefreshTokenActionPayload<T> { | ||
/** | ||
* If the refresh operation is successful or not | ||
* | ||
* If the isSuceess is `true`, then the `token` and other items will be | ||
* replaced with the new network response | ||
* | ||
* If the isSuceess is `false`, then everything will be wiped and user will | ||
* be sgined out | ||
*/ | ||
isSuccess: boolean; | ||
} | ||
/** | ||
* | ||
*/ | ||
type refreshTokenCallback<T> = (param: { | ||
/** | ||
* Existing Auth token for the refresh operation | ||
*/ | ||
authToken?: string; | ||
/** | ||
* Existing Refresh token for the refresh operation | ||
*/ | ||
refreshToken?: string; | ||
/** | ||
* Existing User State for the User state | ||
*/ | ||
authUserState: T | null; | ||
}) => Promise<RefreshTokenCallbackResponse<T>>; | ||
/** | ||
* Parameter for the Refresh operation | ||
*/ | ||
export interface createRefreshParamInterface<T> { | ||
/** | ||
* Interval on which the callback function is called | ||
*/ | ||
interval: number; | ||
/** | ||
* A Callback function which'll have the network request | ||
* | ||
* @example | ||
* ```js | ||
* refreshApiCallback: async (param) => { | ||
* try { | ||
* const response = await axios.post("/refresh", param, { | ||
* headers: {'Authorization': `Bearer ${param.authToken}`} | ||
* }) | ||
* console.log("Refreshing") | ||
* return { | ||
* isSuccess: true, | ||
* newAuthToken: response.data.token, | ||
* newAuthTokenExpireIn: 10, | ||
* newRefreshTokenExpiresIn: 60 | ||
* } | ||
* } | ||
* catch(error){ | ||
* console.error(error) | ||
* return { | ||
* isSuccess: false | ||
* } | ||
* } | ||
* } | ||
* ``` | ||
*/ | ||
refreshApiCallback: refreshTokenCallback<T>; | ||
} | ||
/** | ||
* @param param - Parameters required for the refresh engine | ||
* @returns Same params with added layer of safety net. | ||
* | ||
* @remarks | ||
@@ -9,6 +100,4 @@ * This function doesn't really "do anything" at runtime, | ||
* | ||
* @param param - Parameters required for the refresh engine | ||
* @returns Same params with added layer of safety net. | ||
*/ | ||
declare function createRefresh<T>(param: createRefreshParamInterface<T>): createRefreshParamInterface<T>; | ||
export default createRefresh; |
@@ -8,2 +8,3 @@ "use strict"; | ||
var _errors = require("./errors"); | ||
; | ||
function createRefresh(param) { | ||
@@ -10,0 +11,0 @@ if (param.interval < 0) { |
import TokenObject from './RxTokenObject'; | ||
import type { createRefreshParamInterface } from './types'; | ||
import type { createRefreshParamInterface } from './createRefresh'; | ||
/** | ||
* Store Creation Param | ||
*/ | ||
interface createStoreParam<T> { | ||
/** | ||
* The name of the cookie or localstore object on which | ||
* the auth token is stored. | ||
* | ||
* This name will also be used as a prefix for all other cookies. | ||
*/ | ||
authName: string; | ||
/** | ||
* Type of the Storage. | ||
* | ||
* - `cookie` - Store all the auth information in the cookie | ||
* - `localstorage` - Store all the auth information in the localstorage | ||
*/ | ||
authType: 'cookie' | 'localstorage'; | ||
/** | ||
* Domain of the cookie, for which the cookie is valid. | ||
* | ||
*/ | ||
refresh?: createRefreshParamInterface<T>; | ||
/** | ||
* Needed if you are using `cookie` as authType | ||
* | ||
* @see {@link https://github.com/js-cookie/js-cookie#domain} | ||
*/ | ||
cookieDomain?: string; | ||
/** | ||
* Indicating if the cookie transmission requires a secure protocol (https). | ||
* | ||
* Needed if you are using `cookie` as authType | ||
* | ||
* @see {@link https://github.com/js-cookie/js-cookie#secure} | ||
*/ | ||
cookieSecure?: boolean; | ||
/** | ||
* Refresh API. Created using `createRefresh` function. | ||
*/ | ||
refresh?: createRefreshParamInterface<T>; | ||
} | ||
/** | ||
* | ||
* Return type of createStore Function | ||
*/ | ||
export interface createStoreReturn<T> { | ||
/** | ||
* | ||
* Instance of the token object | ||
*/ | ||
tokenObject: TokenObject<T>; | ||
/** | ||
* | ||
* Instance of the Refresh interface, if there is any. | ||
*/ | ||
@@ -40,6 +57,39 @@ refresh?: createRefreshParamInterface<T>; | ||
* | ||
* @param params | ||
* @returns | ||
* createStore creates the default store for React Auth Kit. | ||
* | ||
* This store is like a Redux store, where every object and data is stored in. | ||
* | ||
* @typeParam T - Type of User State Object | ||
* @param params - Parameter to create a new store for auth kit | ||
* @returns Auth Kit Store | ||
* | ||
* @example | ||
* Here is an example on JavaScript | ||
* ```jsx | ||
* import createStore from 'react-auth-kit/createStore'; | ||
* | ||
* const store = createStore({ | ||
* authName:'_auth', | ||
* authType:'cookie', | ||
* cookieDomain: window.location.hostname, | ||
* cookieSecure: window.location.protocol === 'https:' | ||
* }) | ||
* ``` | ||
* | ||
* Here is an example on TypeScript | ||
* ```tsx | ||
* interface IUserData { | ||
* name: string; | ||
* uuid: string; | ||
* }; | ||
* | ||
* const store = createStore<IUserData>({ | ||
* authName:'_auth', | ||
* authType:'cookie', | ||
* cookieDomain: window.location.hostname, | ||
* cookieSecure: window.location.protocol === 'https:' | ||
* }) | ||
* ``` | ||
*/ | ||
export default function createStore<T>(params: createStoreParam<T>): createStoreReturn<T>; | ||
export {}; |
@@ -24,10 +24,10 @@ /** | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* @returns If the user is authenticated, | ||
* then `'auth.type auth.token'` is returned. | ||
* If the user is not authenticated, then `null` is ruturned. | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
*/ | ||
declare function useAuthHeader(): string | null; | ||
export default useAuthHeader; |
@@ -11,2 +11,9 @@ /** | ||
* | ||
* @returns React Hook with user state functionility. | ||
* If the user is authenticated, then user data is returned. | ||
* If the user is not authenticated, then `null` is ruturned. | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* @example | ||
@@ -40,11 +47,4 @@ * Here is the example for JavaScript | ||
* ``` | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* @returns React Hook with user state functionility. | ||
* If the user is authenticated, then user data is returned. | ||
* If the user is not authenticated, then `null` is ruturned. | ||
*/ | ||
declare function useAuthUser<T>(): T | null; | ||
export default useAuthUser; |
@@ -9,2 +9,7 @@ /** | ||
* | ||
* @returns React Hook with Authtication status Functionility. | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* @example | ||
@@ -24,8 +29,4 @@ * ```js | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* @returns React Hook with Authtication status Functionility. | ||
*/ | ||
declare function useIsAuthenticated(): () => boolean; | ||
export default useIsAuthenticated; |
@@ -11,6 +11,13 @@ import { signInFunctionParams } from '../types'; | ||
* @typeParam T - Type of User State Object | ||
* @param signInConfig - Params for sign In | ||
* @returns React Hook with SignIn Functionility | ||
* | ||
* @throws AuthError | ||
* - Thrown if the Hook is used outside the Provider Scope. | ||
* - Thrown if refresh token is added, in spite not used. | ||
* - Thrown if refresh token is not added, is spite used. | ||
* | ||
* @example | ||
* Here's a an example without refresh token: | ||
* ```js | ||
* ```jsx | ||
* import useSignIn from 'react-auth-kit/hooks/useSignIn' | ||
@@ -30,3 +37,3 @@ * | ||
* Here's a an example with refresh token: | ||
* ```js | ||
* ```jsx | ||
* import useSignIn from 'react-auth-kit/hooks/useSignIn' | ||
@@ -46,2 +53,23 @@ * | ||
* | ||
* Here's a an example with refresh token in TypeScript: | ||
* ```tsx | ||
* import useSignIn from 'react-auth-kit/hooks/useSignIn' | ||
* | ||
* interface IUserData { | ||
* name: string; | ||
* uuid: string; | ||
* }; | ||
* | ||
* const LoginComponent = () => { | ||
* const signIn = useSignIn<IUserData>() | ||
* signIn({ | ||
* auth: { | ||
* token: '<jwt token>' | ||
* }, | ||
* userState: {name: 'React User', uid: 123456}, | ||
* refresh: <refresh jwt token> | ||
* }) | ||
* } | ||
* ``` | ||
* | ||
* @remarks | ||
@@ -54,12 +82,4 @@ * If you are using refresh token, make sure you add that in the parameter, | ||
* | ||
* @throws AuthError | ||
* Thrown if the Hook is used outside the Provider Scope. | ||
* | ||
* Thrown if refresh token is added, in spite not used. | ||
* | ||
* Thrown if refresh token is not added, is spite used. | ||
* | ||
* @returns React Hook with SignIn Functionility | ||
*/ | ||
declare function useSignIn<T>(): (signInConfig: signInFunctionParams<T>) => boolean; | ||
export default useSignIn; |
@@ -8,2 +8,4 @@ /** | ||
* also remove the stored data from cookie or localstorage | ||
* | ||
* @returns React Hook with SignOut Functionility | ||
* | ||
@@ -35,5 +37,4 @@ * @example | ||
* | ||
* @returns React Hook with SignOut Functionility | ||
*/ | ||
declare function useSignOut(): () => (boolean); | ||
export default useSignOut; |
{ | ||
"name": "react-auth-kit", | ||
"version": "3.0.0-alpha.39", | ||
"version": "3.0.0-alpha.40", | ||
"description": "Authentication Library for React, which makes Token based auth very easy", | ||
@@ -90,3 +90,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "4800976ec3d29d8b9ae6ed3c079f7359468406b9" | ||
"gitHead": "61170722f2bc07f6af89d38f048ee8a6eeb093d7" | ||
} |
@@ -1,25 +0,72 @@ | ||
/** | ||
* | ||
* @author Arkadip Bhattacharya <hi@arkadip.dev> | ||
* @fileoverview Token Object Engine | ||
* @copyright Arkadip Bhattacharya 2020 | ||
* | ||
*/ | ||
import { Observable } from 'rxjs'; | ||
import { AuthKitStateInterface, AuthKitSetState } from './types'; | ||
import { AuthKitStateInterface } from './types'; | ||
/** | ||
* @class TokenObject | ||
* | ||
* Stores and retrieve Token | ||
* Set State Data | ||
*/ | ||
export interface AuthKitSetState<T> { | ||
/** | ||
* Authentication Object | ||
*/ | ||
auth?: { | ||
/** | ||
* JWT access token | ||
*/ | ||
token: string; | ||
/** | ||
* Type of the access token | ||
* | ||
* @example | ||
* Bearer | ||
*/ | ||
type: string; | ||
} | null; | ||
/** | ||
* Refresh JWT token | ||
*/ | ||
refresh?: string | null; | ||
/** | ||
* User state object | ||
*/ | ||
userState?: T; | ||
} | ||
declare class TokenObject<T> { | ||
/** | ||
* Name of the storage for the access token | ||
*/ | ||
private readonly authStorageName; | ||
/** | ||
* Name of the storage for the user state | ||
*/ | ||
private readonly stateStorageName; | ||
/** | ||
* Domain Name for the cookie | ||
*/ | ||
private readonly cookieDomain?; | ||
/** | ||
* HTTP Secure for the cookie | ||
*/ | ||
private readonly cookieSecure?; | ||
/** | ||
* Name of the storage for the auth token type | ||
*/ | ||
private readonly authStorageTypeName; | ||
/** | ||
* Type of the Storage to be used to store the data | ||
*/ | ||
private readonly authStorageType; | ||
/** | ||
* Name of the storage for the refresh token | ||
*/ | ||
private readonly refreshTokenName; | ||
/** | ||
* Boolean value to check if the application is using refresh token feature or not | ||
*/ | ||
private readonly isUsingRefreshToken; | ||
/** | ||
* Auth Value | ||
*/ | ||
private authValue; | ||
/** | ||
* RX Auth subject | ||
*/ | ||
private authSubject; | ||
@@ -44,44 +91,70 @@ /** | ||
* | ||
* @constructor | ||
*/ | ||
constructor(authStorageName: string, authStorageType: 'cookie' | 'localstorage', refreshTokenName: string | null, cookieDomain?: string, cookieSecure?: boolean); | ||
subscribe: (next: (value: AuthKitStateInterface<T>) => void, error?: ((err: any) => void) | undefined) => void; | ||
observe: () => Observable<AuthKitStateInterface<T>>; | ||
/** | ||
* Subscribe method for TokenObject | ||
* | ||
* @param data | ||
* @param next - A callback function that gets called by the producer during | ||
* the subscription when the producer "has" the `value`. It won't be called | ||
* if `error` or `complete` callback functions have been called | ||
* @param error - A callback function that gets called by the producer | ||
* if and when it encountered a problem of any kind | ||
* @param complete - A callback function that gets called by the producer | ||
* if and when it has no more values to provide | ||
*/ | ||
subscribe: (next: (value: AuthKitStateInterface<T>) => void, error?: ((err: any) => void) | undefined, complete?: (() => void)) => void; | ||
/** | ||
* Observe method for TokenObject | ||
* | ||
* LOGIC | ||
* @returns A RxJs Observable for further subscription | ||
* | ||
* data ------ new auth is present and not null ---------- new user state ----- Replace Auth and User state | ||
* | | | | ||
* | | | | ||
* | | ----- no new user state --- Replace only the Auth use old user state | ||
* | | | ||
* | | | ||
* | ---- new auth is null ----------------------------------------------- Clean auth and userstate | ||
* | | | ||
* | | | ||
* | ---- no new auth data present --------------------------------------- Do nothing use the old auth ans user state | ||
* @see {@link https://rxjs.dev/api/index/class/Subject#asObservable} | ||
*/ | ||
observe: () => Observable<AuthKitStateInterface<T>>; | ||
/** | ||
* @internal | ||
* @param data - The data to set the state | ||
* | ||
* @remarks | ||
* Below is the logic | ||
* ```txt | ||
* data - new auth is present - new user state ----- Replace Auth and | ||
* | | and not null | User state | ||
* | | | | ||
* | | - no new user state --- Replace only | ||
* | | the Auth use old | ||
* | | user state | ||
* | | | ||
* | | | ||
* | ---- new auth is null ----------------------- Clean auth and | ||
* | | userstate | ||
* | | | ||
* | ---- no new auth data ----------------------- Do nothing use the | ||
* | present old auth ans user state | ||
* | | ||
* -- is using refesh token is true - new refresh is ---- Update the | ||
* | | present is refresh token | ||
* | | not null | ||
* | | | ||
* | | | ||
* | - new refresh ------- Clean refresh token | ||
* | | is null | ||
* | | | ||
* | - no new refresh ---- Do nothing use | ||
* | the old refresh | ||
* | | ||
* | | ||
* --- is using refesh token is true --- new refresh is present is not null - Update the refresh token | ||
* | | | ||
* | | | ||
* | -- new refresh is null ---------------- Clean refresh token | ||
* | | | ||
* | | | ||
* | -- no new refresh --------------------- Do nothing use the old refresh | ||
* | | ||
* | | ||
* -- is using refresh token is false -------------------------------------- Do nothing use the old refrssh | ||
* | ||
* -- is using refresh token is false ------------------ Do nothing use | ||
* the old refresh | ||
* ``` | ||
*/ | ||
set: (data: AuthKitSetState<T>) => void; | ||
/** | ||
* Getter for currrent state for TokenObject | ||
*/ | ||
get value(): AuthKitStateInterface<T>; | ||
/** | ||
* Get the Initial Tokens and states | ||
* Called externally in `AuthProvider` | ||
* Get the Initial Tokens and states from storage | ||
* when the Application is bootstrapping or refreshed | ||
* | ||
* @remarks | ||
* If the `authStorageType` is `cookie`, | ||
@@ -93,3 +166,3 @@ * get information from `initialCookieToken()` function | ||
* | ||
* @returns AuthKitStateInterface | ||
* @returns Initial State | ||
*/ | ||
@@ -99,4 +172,4 @@ private initialToken_; | ||
* Get the Initial Token from Cookies | ||
* Called internally by `initialToken` method | ||
* | ||
* @remarks | ||
* If the `authStorageType` is `cookie` | ||
@@ -106,3 +179,3 @@ * then this function is called | ||
* | ||
* @returns AuthKitStateInterface | ||
* @returns Initial State from Cookies | ||
*/ | ||
@@ -112,4 +185,4 @@ private initialCookieToken_; | ||
* Get the Initial Token from LocalStorage | ||
* Called internally by `initialToken` method | ||
* | ||
* @remarks | ||
* If the `authStorageType` is `localstorage` | ||
@@ -119,26 +192,34 @@ * then this function is called | ||
* | ||
* @returns AuthKitStateInterface | ||
* @returns Initial State from LocalStorage | ||
*/ | ||
private initialLSToken_; | ||
/** | ||
* Check if the Initial token is valid or not, | ||
* Check for all the existance for the Tokens | ||
* Called Internally by `initialCookieToken_()` and `initialLSToken_()` | ||
* | ||
* If the tokens are valid, | ||
* then it response TokenObject with auth Information | ||
* Else it response TokenObject with all null | ||
* @param authToken - Auth token from cookie or localstorage | ||
* @param authTokenType - Auth token type from cookie or localstorage | ||
* @param stateCookie - User state from cookie of localstorage | ||
* @param refreshToken - Refresh token from cookie or localstorage | ||
* | ||
* @param authToken | ||
* @param authTokenType | ||
* @param authTokenTime | ||
* @param stateCookie | ||
* @param refreshToken | ||
* @param refreshTokenTime | ||
* @returns Auth State with all conditions and guard in place | ||
*/ | ||
private checkTokenExist_; | ||
/** | ||
* Function to patse the JWT | ||
* | ||
* @returns AuthKitStateInterface | ||
* @param token - JWT to purse | ||
* @returns Parsed data from JWT | ||
*/ | ||
private parseJwt; | ||
/** | ||
* Get the Expire Date from JWT | ||
* | ||
* @param token - JWT from which to get the Expire time | ||
* @returns Expire Date | ||
* | ||
* @remarks | ||
* Get `exp` param from the JWT data payload and convert that to Date | ||
*/ | ||
private checkTokenExist_; | ||
private parseJwt_; | ||
private getExpireDateTime_; | ||
private getExpireDateTime; | ||
/** | ||
@@ -190,4 +271,4 @@ * Sync Auth Tokens on time of login and logout | ||
*/ | ||
private removeRefreshToken; | ||
private removeRefreshLocalStorage; | ||
} | ||
export default TokenObject; |
@@ -34,6 +34,7 @@ "use strict"; | ||
} | ||
subscribe = (next, error) => { | ||
subscribe = (next, error, complete) => { | ||
this.authSubject.subscribe({ | ||
next: next, | ||
error: error | ||
error: error, | ||
complete: complete | ||
}); | ||
@@ -56,3 +57,3 @@ }; | ||
'type': data.auth.type, | ||
'expiresAt': this.getExpireDateTime_(data.auth.token) | ||
'expiresAt': this.getExpireDateTime(data.auth.token) | ||
}, | ||
@@ -76,3 +77,3 @@ isSignIn: true, | ||
'token': data.refresh, | ||
'expiresAt': this.getExpireDateTime_(data.refresh) | ||
'expiresAt': this.getExpireDateTime(data.refresh) | ||
} | ||
@@ -118,3 +119,3 @@ }; | ||
if (this.isUsingRefreshToken && !!refreshToken) { | ||
const refreshTokenExpiresAt = this.getExpireDateTime_(refreshToken); | ||
const refreshTokenExpiresAt = this.getExpireDateTime(refreshToken); | ||
if (refreshTokenExpiresAt < new Date()) { | ||
@@ -145,3 +146,3 @@ refresh = null; | ||
try { | ||
const expiresAt = this.getExpireDateTime_(authToken); | ||
const expiresAt = this.getExpireDateTime(authToken); | ||
if (expiresAt < new Date()) { | ||
@@ -214,3 +215,3 @@ auth = null; | ||
}; | ||
parseJwt_ = token => { | ||
parseJwt = token => { | ||
const base64Url = token.split('.')[1]; | ||
@@ -223,10 +224,10 @@ const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); | ||
}; | ||
getExpireDateTime_ = token => { | ||
const jwtData = this.parseJwt_(token); | ||
if (jwtData.hasOwnProperty('iat')) { | ||
getExpireDateTime = token => { | ||
const jwtData = this.parseJwt(token); | ||
if (jwtData.hasOwnProperty('exp')) { | ||
const d = new Date(0); | ||
d.setUTCSeconds(jwtData.iat); | ||
d.setUTCSeconds(jwtData.exp); | ||
return d; | ||
} else { | ||
throw new _errors.AuthError('JWT has no iat param'); | ||
throw new _errors.AuthError('JWT has no exp param'); | ||
} | ||
@@ -248,3 +249,3 @@ }; | ||
if (this.authStorageType === 'cookie') { | ||
const expiresAt = this.getExpireDateTime_(authToken); | ||
const expiresAt = this.getExpireDateTime(authToken); | ||
_jsCookie.default.set(this.authStorageName, authToken, { | ||
@@ -278,3 +279,3 @@ expires: expiresAt, | ||
if (this.isUsingRefreshToken && !!this.refreshTokenName && !!refreshToken) { | ||
const refreshTokenExpiresAt = this.getExpireDateTime_(refreshToken); | ||
const refreshTokenExpiresAt = this.getExpireDateTime(refreshToken); | ||
_jsCookie.default.set(this.refreshTokenName, refreshToken, { | ||
@@ -357,3 +358,3 @@ expires: refreshTokenExpiresAt, | ||
} else { | ||
this.removeRefreshToken(); | ||
this.removeRefreshLocalStorage(); | ||
} | ||
@@ -369,3 +370,3 @@ }; | ||
}; | ||
removeRefreshToken = () => { | ||
removeRefreshLocalStorage = () => { | ||
if (this.isUsingRefreshToken && !!this.refreshTokenName) { | ||
@@ -372,0 +373,0 @@ localStorage.removeItem(this.refreshTokenName); |
163
types.d.ts
/** | ||
* | ||
* @author Arkadip Bhattacharya <hi@arkadip.dev> | ||
* @fileoverview General types used in the library | ||
* @copyright Arkadip Bhattacharya 2020 | ||
* | ||
* Parameters for SignIn Action | ||
*/ | ||
export interface SignInActionPayload<T> { | ||
/** | ||
* Authentication object | ||
*/ | ||
auth: { | ||
/** | ||
* JWT access token | ||
*/ | ||
token: string; | ||
/** | ||
* `type` or `prefix` of the token, to be used in the Network Request | ||
* | ||
* @example | ||
* Bearer | ||
*/ | ||
type?: string; | ||
}; | ||
/** | ||
* JWT refresh token. | ||
* | ||
* Make sure, you are using refresh token and configured the `createRefresh` | ||
* before adding the token | ||
*/ | ||
refresh?: string | null; | ||
/** | ||
* User information | ||
*/ | ||
userState?: T; | ||
} | ||
export interface RefreshTokenActionPayload<T> { | ||
newAuthToken?: string; | ||
newAuthTokenType?: string; | ||
newRefreshToken?: string; | ||
newAuthUserState?: T | null; | ||
} | ||
export interface AuthKitSetState<T> { | ||
auth?: { | ||
token: string; | ||
type: string; | ||
} | null; | ||
refresh?: string | null; | ||
userState?: T; | ||
} | ||
/** | ||
* Parameters used by the Sign In function | ||
*/ | ||
export type signInFunctionParams<T> = SignInActionPayload<T>; | ||
/** | ||
* Auth State Object | ||
* 1. when the user is authenticated | ||
* 2. Refresh token can be there or not | ||
* 3. User state can be there or not | ||
*/ | ||
interface AuthKitStateInterfaceTrue<T> { | ||
/** | ||
* Authentication part of the State | ||
*/ | ||
auth: { | ||
/** | ||
* Auth access token | ||
*/ | ||
token: string; | ||
/** | ||
* Type of the Token | ||
* | ||
* @example | ||
* Bearer | ||
*/ | ||
type: string; | ||
/** | ||
* Expiry time of the auth token | ||
*/ | ||
expiresAt: Date; | ||
}; | ||
/** | ||
* Auth Refresh token part | ||
*/ | ||
refresh: { | ||
/** | ||
* Refresh token | ||
*/ | ||
token: string; | ||
/** | ||
* Expiry time of the Refresh token | ||
*/ | ||
expiresAt: Date; | ||
} | null; | ||
/** | ||
* User state | ||
*/ | ||
userState: T | null; | ||
/** | ||
* Boolean value to know if the user is signed in or not | ||
*/ | ||
isSignIn: boolean; | ||
/** | ||
* If the current system is using refresh token or not | ||
*/ | ||
isUsingRefreshToken: boolean; | ||
} | ||
/** | ||
* Auth State Object | ||
* 1. when the user is not authenticated | ||
* 2. Refresh token is there | ||
* 3. User state is null | ||
*/ | ||
interface AuthKitStateInterfaceNoAuthOnlyRefresh { | ||
/** | ||
* Authentication part of the State | ||
*/ | ||
auth: null; | ||
/** | ||
* Auth Refresh token part | ||
*/ | ||
refresh: { | ||
/** | ||
* Refresh token | ||
*/ | ||
token: string; | ||
/** | ||
* Expiry time of the Refresh token | ||
*/ | ||
expiresAt: Date; | ||
}; | ||
/** | ||
* User State | ||
*/ | ||
userState: null; | ||
/** | ||
* Boolean value to know if the user is signed in or not | ||
*/ | ||
isSignIn: boolean; | ||
/** | ||
* If the current system is using refresh token or not | ||
*/ | ||
isUsingRefreshToken: boolean; | ||
} | ||
/** | ||
* Auth State Object | ||
* 1. when the user is not authenticated | ||
* 2. Refresh token is null | ||
* 3. User state is null | ||
*/ | ||
interface AuthKitStateInterfaceFalse { | ||
/** | ||
* Authentication part of the State, which is null | ||
*/ | ||
auth: null; | ||
/** | ||
* Refresh token part, which is null | ||
*/ | ||
refresh: null; | ||
/** | ||
* User state, which is null | ||
*/ | ||
userState: null; | ||
/** | ||
* Is Sign In, which is false | ||
*/ | ||
isSignIn: boolean; | ||
/** | ||
* If the current system is using refresh token or not | ||
*/ | ||
isUsingRefreshToken: boolean; | ||
} | ||
export type AuthKitStateInterface<T> = AuthKitStateInterfaceTrue<T> | AuthKitStateInterfaceFalse | AuthKitStateInterfaceNoAuthOnlyRefresh; | ||
/** | ||
* SignIn function param | ||
* Used by: useSignIn and withSignIn | ||
* Auth State Object | ||
*/ | ||
export type signInFunctionParams<T> = SignInActionPayload<T>; | ||
/** | ||
* Context values type | ||
*/ | ||
/** | ||
* Refresh Token Callback Response | ||
*/ | ||
export type RefreshTokenCallbackResponse<T> = { | ||
isSuccess: boolean; | ||
newAuthToken: string; | ||
newRefreshToken?: string; | ||
newAuthUserState?: T | null; | ||
}; | ||
/** | ||
* Refresh Token types | ||
*/ | ||
export type refreshTokenCallback<T> = (param: { | ||
authToken?: string; | ||
refreshToken?: string; | ||
authUserState: T | null; | ||
}) => Promise<RefreshTokenCallbackResponse<T>>; | ||
export interface createRefreshParamInterface<T> { | ||
interval: number; | ||
refreshApiCallback: refreshTokenCallback<T>; | ||
} | ||
export type refreshFunctionType<T> = (param: createRefreshParamInterface<T>) => createRefreshParamInterface<T>; | ||
export type AuthKitStateInterface<T> = AuthKitStateInterfaceTrue<T> | AuthKitStateInterfaceFalse | AuthKitStateInterfaceNoAuthOnlyRefresh; | ||
export {}; |
import type { MutableRefObject } from 'react'; | ||
/** | ||
* @internal | ||
* | ||
* React Hook to use the `setInterval` in the component | ||
* | ||
* @internal | ||
* | ||
* @param callback - The function that will be called on each interval | ||
@@ -9,0 +8,0 @@ * @param delay - The delay on which the callback function is called |
@@ -14,4 +14,8 @@ /** | ||
*/ | ||
import { AuthKitSetState, SignInActionPayload, RefreshTokenActionPayload } from '../types'; | ||
import type { SignInActionPayload } from '../types'; | ||
import type { RefreshTokenActionPayload } from '../createRefresh'; | ||
import type { AuthKitSetState } from '../RxTokenObject'; | ||
/** | ||
* @internal | ||
* | ||
* Do Sign In reducer. | ||
@@ -21,2 +25,6 @@ * It is used to convert the incoming payload into | ||
* It doesn't contains any important buisness logic. | ||
* | ||
* @typeParam T - Type of User State Object | ||
* @param signInParams - Sign in parameters | ||
* @returns Object that is modified to auth state to used further | ||
* | ||
@@ -27,9 +35,2 @@ * @remarks | ||
* | ||
* @param signInParams - Sign in parameters | ||
* @returns Object that is modified to auth state to used further | ||
* | ||
* @internal | ||
* | ||
* @typeParam T - Type of User State Object | ||
* | ||
*/ | ||
@@ -36,0 +37,0 @@ export declare function doSignIn<T>(signInParams: SignInActionPayload<T>): AuthKitSetState<T>; |
@@ -12,2 +12,3 @@ /** | ||
* | ||
* @typeParam T - Type of User State Object | ||
* @param auth - The Auth Object | ||
@@ -18,5 +19,4 @@ * @returns A boolean value indicting if currently authenticated or not | ||
* | ||
* @typeParam T - Type of User State Object | ||
*/ | ||
declare function isAuthenticated<T>(auth: AuthKitStateInterface<T>): boolean; | ||
export { isAuthenticated }; |
84085
2284