mirror of
https://github.com/rzmk/learnhouse.git
synced 2025-12-18 20:09:25 +00:00
feat: add authentication only component
This commit is contained in:
parent
924656ce13
commit
0c6ec386c2
2 changed files with 45 additions and 0 deletions
42
front/components/security/AuthenticatedOnly.tsx
Normal file
42
front/components/security/AuthenticatedOnly.tsx
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import React from "react";
|
||||||
|
import { getRefreshToken, getUserInfo } from "../../services/auth/auth";
|
||||||
|
import { Auth, AuthContext } from "./AuthProvider";
|
||||||
|
|
||||||
|
const AuthenticatedOnly = (props: any) => {
|
||||||
|
const [auth, setAuth] = React.useState<Auth>({ access_token: "", isAuthenticated: false, userInfo: {}, isLoading: true });
|
||||||
|
|
||||||
|
async function checkRefreshToken() {
|
||||||
|
let data = await getRefreshToken();
|
||||||
|
return data.access_token;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function checkAuth() {
|
||||||
|
let access_token = await checkRefreshToken();
|
||||||
|
let isAuthenticated = false;
|
||||||
|
let userInfo = {};
|
||||||
|
let isLoading = false;
|
||||||
|
|
||||||
|
if (access_token) {
|
||||||
|
userInfo = await getUserInfo(access_token);
|
||||||
|
isAuthenticated = true;
|
||||||
|
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||||
|
} else {
|
||||||
|
isAuthenticated = false;
|
||||||
|
setAuth({ access_token, isAuthenticated, userInfo, isLoading });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
checkAuth();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
{auth.isLoading && <div>Loading...</div>}
|
||||||
|
{!auth.isLoading && auth.isAuthenticated && <div>{props.children}</div>}
|
||||||
|
{!auth.isLoading && !auth.isAuthenticated && <div>Not Authenticated</div>}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AuthenticatedOnly;
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import AuthenticatedOnly from "../../components/security/AuthenticatedOnly";
|
||||||
import Layout from "../../components/ui/layout";
|
import Layout from "../../components/ui/layout";
|
||||||
import { Title } from "../../components/ui/styles/title";
|
import { Title } from "../../components/ui/styles/title";
|
||||||
import { deleteOrganizationFromBackend, getUserOrganizations } from "../../services/orgs";
|
import { deleteOrganizationFromBackend, getUserOrganizations } from "../../services/orgs";
|
||||||
|
|
@ -32,6 +33,7 @@ const Organizations = () => {
|
||||||
return (
|
return (
|
||||||
|
|
||||||
<Layout>
|
<Layout>
|
||||||
|
<AuthenticatedOnly>
|
||||||
<Title>
|
<Title>
|
||||||
Your Organizations{" "}
|
Your Organizations{" "}
|
||||||
<Link href={"/organizations/new"}>
|
<Link href={"/organizations/new"}>
|
||||||
|
|
@ -59,6 +61,7 @@ const Organizations = () => {
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
</AuthenticatedOnly>
|
||||||
|
|
||||||
</Layout>
|
</Layout>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue