mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
added session for auth
This commit is contained in:
parent
bb785ea459
commit
27763be864
3 changed files with 33 additions and 0 deletions
18
src/hooks/auth/useSessionCookie.ts
Normal file
18
src/hooks/auth/useSessionCookie.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
import { autorun } from 'mobx';
|
||||||
|
|
||||||
|
import { useAuthStore } from '~/store/auth/useAuthStore';
|
||||||
|
import { setCookie } from '~/utils/dom/cookie';
|
||||||
|
|
||||||
|
export const useSessionCookie = () => {
|
||||||
|
const auth = useAuthStore();
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() =>
|
||||||
|
autorun(() => {
|
||||||
|
setCookie('session', auth.token, 30);
|
||||||
|
}),
|
||||||
|
[auth]
|
||||||
|
);
|
||||||
|
};
|
13
src/utils/dom/cookie.ts
Normal file
13
src/utils/dom/cookie.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
export const setCookie = (name: string, value: string, days: number) => {
|
||||||
|
let expires;
|
||||||
|
|
||||||
|
if (days) {
|
||||||
|
const date = new Date();
|
||||||
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
||||||
|
expires = '; expires=' + date.toISOString();
|
||||||
|
} else {
|
||||||
|
expires = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
document.cookie = name + '=' + value + expires + '; path=/';
|
||||||
|
};
|
|
@ -6,6 +6,7 @@ import { EMPTY_USER } from '~/constants/auth';
|
||||||
import { useAuth } from '~/hooks/auth/useAuth';
|
import { useAuth } from '~/hooks/auth/useAuth';
|
||||||
import { useMessageEventReactions } from '~/hooks/auth/useMessageEventReactions';
|
import { useMessageEventReactions } from '~/hooks/auth/useMessageEventReactions';
|
||||||
import { useRestorePasswordRedirect } from '~/hooks/auth/useRestorePasswordRedirect';
|
import { useRestorePasswordRedirect } from '~/hooks/auth/useRestorePasswordRedirect';
|
||||||
|
import { useSessionCookie } from '~/hooks/auth/useSessionCookie';
|
||||||
|
|
||||||
interface AuthProviderContextType extends ReturnType<typeof useAuth> {}
|
interface AuthProviderContextType extends ReturnType<typeof useAuth> {}
|
||||||
|
|
||||||
|
@ -24,6 +25,7 @@ export const AuthProvider: FC = observer(({ children }) => {
|
||||||
|
|
||||||
useMessageEventReactions();
|
useMessageEventReactions();
|
||||||
useRestorePasswordRedirect();
|
useRestorePasswordRedirect();
|
||||||
|
useSessionCookie();
|
||||||
|
|
||||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue