Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Session redo #64

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@heroicons/react": "^2.0.14",
"@loadable/component": "^5.15.3",
"@tanstack/react-table": "^8.7.9",
"buffer": "^6.0.3",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
"formik": "^2.2.9",
Expand Down
2 changes: 1 addition & 1 deletion src/app/auth/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default function Login() {
loadProviders();
}, [setProviders, setError]);

if (session) {
if (session && (!session.required || session.authenticated)) {
return <Navigate to="/" />;
}

Expand Down
38 changes: 23 additions & 15 deletions src/components/SessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { createContext, useEffect, useMemo } from 'react';
import { getAuthSelf, getInfo } from '~/data/api';
import { useStorage } from '~/data/hooks/storage';
import { AuthMethodOIDCSelf } from '~/types/Auth';
import { Info } from '~/types/Meta';

type Session = {
info: Info;
self: AuthMethodOIDCSelf;
required: boolean;
authenticated: boolean;
self?: AuthMethodOIDCSelf;
};

interface SessionContextType {
session?: Session;
setSession: (data: any) => void;
clearSession: () => void;
}

export const SessionContext = createContext({} as SessionContextType);
Expand All @@ -21,17 +22,17 @@ export default function SessionProvider({
}: {
children: React.ReactNode;
}) {
const [session, setSession, clearSession] = useStorage('flipt', null);
const [session, setSession, clearSession] = useStorage('session', null);

useEffect(() => {
const loadSession = async () => {
let data = null;
let session = {
required: true,
authenticated: false
} as Session;

try {
const info = await getInfo();
data = {
info: info
};
await getInfo();
} catch (err) {
// if we can't get the info, we're not logged in
// or there was an error, either way, clear the session so we redirect
Expand All @@ -42,16 +43,22 @@ export default function SessionProvider({

try {
const self = await getAuthSelf();
data = {
...data,
session = {
authenticated: true,
required: true,
self: self
};
} catch (err) {
// if we can't get the self info and we got here then auth is likely not enabled
// so we can just return
return;
session = {
authenticated: false,
required: false
};
} finally {
setSession(data);
if (session) {
setSession(session);
}
}
};
if (!session) loadSession();
Expand All @@ -61,9 +68,10 @@ export default function SessionProvider({
const value = useMemo(
() => ({
session,
setSession
setSession,
clearSession
}),
[session, setSession]
[session, setSession, clearSession]
);

return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export default function UserProfile(props: UserProfileProps) {
const { name, imgURL } = props;

const { setError } = useError();
const { setSession } = useSession();
const { clearSession } = useSession();

const logout = async () => {
expireAuthSelf()
.then(() => {
setSession(null);
clearSession();
window.location.href = '/';
})
.catch((err) => {
Expand Down
7 changes: 5 additions & 2 deletions src/data/hooks/storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Buffer } from 'buffer';
import { useState } from 'react';

export const useStorage = (key: string, initialValue: any) => {
const [storedValue, setStoredValue] = useState(() => {
try {
const item = window.localStorage.getItem(key);
return item ? JSON.parse(item) : initialValue;
const buffer = item ? Buffer.from(item, 'base64') : null;
return buffer ? JSON.parse(buffer.toLocaleString()) : initialValue;
} catch (error) {
console.error(error);
return initialValue;
Expand All @@ -16,7 +18,8 @@ export const useStorage = (key: string, initialValue: any) => {
const valueToStore =
value instanceof Function ? value(storedValue) : value;
setStoredValue(valueToStore);
window.localStorage.setItem(key, JSON.stringify(valueToStore));
const buffer = Buffer.from(JSON.stringify(valueToStore));
window.localStorage.setItem(key, buffer.toString('base64'));
} catch (error) {
console.error(error);
}
Expand Down