added auth object in state
parent
8e200a1d14
commit
cc1dd6a4c2
|
@ -3,10 +3,10 @@ import { TuitioContext } from "../contexts";
|
|||
|
||||
const useTuitioToken = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const token = state.token;
|
||||
const token = state.auth.token;
|
||||
|
||||
const validate = (): boolean => {
|
||||
const validUntil = state.validUntil;
|
||||
const validUntil = state.auth.validUntil;
|
||||
if (!validUntil) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import { TuitioContext } from "../contexts";
|
|||
|
||||
const useTuitioUser = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const userName = state.userName;
|
||||
const userName = state.auth.userName;
|
||||
return { userName };
|
||||
};
|
||||
|
||||
|
|
|
@ -4,10 +4,11 @@ import type { TuitioReactState, TuitioDispatchActions } from "./types";
|
|||
const { token, validUntil, userName } = fetch();
|
||||
|
||||
export const initialState: TuitioReactState = {
|
||||
// pune intr-un obiect auth
|
||||
userName,
|
||||
token,
|
||||
validUntil,
|
||||
auth: {
|
||||
userName,
|
||||
token,
|
||||
validUntil
|
||||
},
|
||||
configuration: { tuitioUrl: null }
|
||||
};
|
||||
|
||||
|
|
|
@ -8,9 +8,11 @@ export const reducer = (state: TuitioReactState = initialState, action: any): Tu
|
|||
const { token, validUntil, userName } = action;
|
||||
return {
|
||||
...state,
|
||||
token,
|
||||
validUntil,
|
||||
userName
|
||||
auth: {
|
||||
token,
|
||||
validUntil,
|
||||
userName
|
||||
}
|
||||
};
|
||||
}
|
||||
case "onLogoutSuccess": {
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
export type TuitioConfiguration = { tuitioUrl: string | null };
|
||||
export type TuitioReactState = {
|
||||
userName: string | null;
|
||||
token: string | null;
|
||||
validUntil: Date | null;
|
||||
auth: {
|
||||
userName: string | null;
|
||||
token: string | null;
|
||||
validUntil: Date | null;
|
||||
};
|
||||
configuration: TuitioConfiguration;
|
||||
};
|
||||
export type TuitioDispatchActions = {
|
||||
|
|
|
@ -40,9 +40,11 @@ describe("useTuitioClient: dispatchActions", () => {
|
|||
});
|
||||
|
||||
const tuitioContextState = {
|
||||
userName: "",
|
||||
token: "mocked-token",
|
||||
validUntil: new Date(),
|
||||
auth: {
|
||||
userName: "",
|
||||
token: "mocked-token",
|
||||
validUntil: new Date()
|
||||
},
|
||||
configuration: { tuitioUrl: null }
|
||||
};
|
||||
|
||||
|
|
|
@ -5,15 +5,13 @@ import { useTuitioToken } from "../../src/hooks";
|
|||
|
||||
describe("useTuitioToken: positive flow", () => {
|
||||
const testState = {
|
||||
userName: "test-user",
|
||||
token: "mocked-token",
|
||||
validUntil: new Date(),
|
||||
auth: { userName: "test-user", token: "mocked-token", validUntil: new Date() },
|
||||
configuration: {
|
||||
tuitioUrl: null
|
||||
}
|
||||
};
|
||||
|
||||
testState.validUntil.setHours(testState.validUntil.getHours() + 1);
|
||||
testState.auth.validUntil.setHours(testState.auth.validUntil.getHours() + 1);
|
||||
|
||||
const wrapper = ({ children }: { children?: React.ReactNode }) => (
|
||||
<TuitioContext.Provider value={testState}>{children}</TuitioContext.Provider>
|
||||
|
@ -38,9 +36,7 @@ describe("useTuitioToken: positive flow", () => {
|
|||
|
||||
describe("useTuitioToken: negative flow", () => {
|
||||
const testState = {
|
||||
userName: "test-user",
|
||||
token: null,
|
||||
validUntil: null,
|
||||
auth: { userName: "test-user", token: null, validUntil: null },
|
||||
configuration: {
|
||||
tuitioUrl: null
|
||||
}
|
||||
|
|
|
@ -6,9 +6,7 @@ import { useTuitioUser } from "../../src/hooks";
|
|||
describe("useTuitioUser: positive flow", () => {
|
||||
it("should return the userName from the TuitioContext", () => {
|
||||
const testState = {
|
||||
userName: "test-user",
|
||||
token: "mocked-token",
|
||||
validUntil: new Date(2023, 12, 31),
|
||||
auth: { userName: "test-user", token: "mocked-token", validUntil: new Date(2023, 12, 31) },
|
||||
configuration: {
|
||||
tuitioUrl: null
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ describe("reducer", () => {
|
|||
|
||||
it('should handle the "onLoginSuccess" action', () => {
|
||||
const result = reducer(initialState, { type: "onLoginSuccess", token, validUntil, userName });
|
||||
const expected = { ...initialState, token, validUntil, userName };
|
||||
const expected = { ...initialState, auth: { token, validUntil, userName } };
|
||||
expect(result).toEqual(expected);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue