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