hooks refactoring
parent
627e2105f8
commit
20a1fcb7dd
|
@ -0,0 +1,3 @@
|
|||
export * from "./useTuitioClient";
|
||||
export * from "./useTuitioToken";
|
||||
export * from "./useTuitioUser";
|
|
@ -1,5 +1,5 @@
|
|||
import { useContext } from "react";
|
||||
import { TuitioContext, TuitioDispatchContext } from "./contexts";
|
||||
import { TuitioContext, TuitioDispatchContext } from "../contexts";
|
||||
import { TuitioClient, invalidate } from "@flare/tuitio-client";
|
||||
import type { TuitioAuthenticationResult } from "@flare/tuitio-client";
|
||||
|
||||
|
@ -40,30 +40,4 @@ const useTuitioClient = (options: TuitioClientHookOptions) => {
|
|||
return { login, logout };
|
||||
};
|
||||
|
||||
const useTuitioUser = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const userName = state.userName;
|
||||
const lastLoginDate = state.token?.validFrom;
|
||||
return { userName, lastLoginDate };
|
||||
};
|
||||
|
||||
const useTuitioToken = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const token = state.token;
|
||||
|
||||
const validate = (): boolean => {
|
||||
const token = state.token;
|
||||
if (!token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const valid = new Date(token.validUntil) >= new Date();
|
||||
return valid;
|
||||
};
|
||||
|
||||
const valid: boolean = validate();
|
||||
|
||||
return { token, validate, valid };
|
||||
};
|
||||
|
||||
export { useTuitioClient, useTuitioUser, useTuitioToken };
|
||||
export { useTuitioClient };
|
|
@ -0,0 +1,23 @@
|
|||
import { useContext } from "react";
|
||||
import { TuitioContext } from "../contexts";
|
||||
|
||||
const useTuitioToken = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const token = state.token;
|
||||
|
||||
const validate = (): boolean => {
|
||||
const token = state.token;
|
||||
if (!token) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const valid = new Date(token.validUntil) >= new Date();
|
||||
return valid;
|
||||
};
|
||||
|
||||
const valid: boolean = validate();
|
||||
|
||||
return { token, validate, valid };
|
||||
};
|
||||
|
||||
export { useTuitioToken };
|
|
@ -0,0 +1,11 @@
|
|||
import { useContext } from "react";
|
||||
import { TuitioContext } from "../contexts";
|
||||
|
||||
const useTuitioUser = () => {
|
||||
const state = useContext(TuitioContext);
|
||||
const userName = state.userName;
|
||||
const lastLoginDate = state.token?.validFrom;
|
||||
return { userName, lastLoginDate };
|
||||
};
|
||||
|
||||
export { useTuitioUser };
|
Loading…
Reference in New Issue