Skip to content
Permalink
Browse files

implement likes, feed, profile

  • Loading branch information...
abhi40308 committed Aug 25, 2019
1 parent cba1426 commit 7b3e3a031c0f2990e44fa2fffb9a11fa32291917

Some generated files are not rendered by default. Learn more.

@@ -5,13 +5,15 @@
"dependencies": {
"@apollo/react-hoc": "^3.0.1",
"@apollo/react-hooks": "^3.0.1",
"@auth0/auth0-spa-js": "^1.1.1",
"apollo-boost": "^0.4.4",
"apollo-link-context": "^1.0.18",
"bootstrap": "^4.3.1",
"graphql": "^14.4.2",
"react": "^16.9.0",
"react-bootstrap": "^1.0.0-beta.11",
"react-dom": "^16.9.0",
"react-router-dom": "^5.0.1",
"react-scripts": "3.1.1"
},
"scripts": {
@@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>Instagram Clone</title>
</head>

<body>
@@ -0,0 +1,6 @@
{
"domain": "dev-z5h3dp-b.auth0.com",
"clientId": "Iv6qSZ1M6YD1a8lt0M4kbJ4BVEz0pFFF",
"redirect_uri": "http://localhost:3000",
"audience": "https://instagram-clone-3.herokuapp.com/v1/graphql"
}
@@ -0,0 +1,87 @@
// src/react-auth0-wrapper.js
import React, { useState, useEffect, useContext } from "react";
import createAuth0Client from "@auth0/auth0-spa-js";

const DEFAULT_REDIRECT_CALLBACK = () =>
window.history.replaceState({}, document.title, window.location.pathname);

export const Auth0Context = React.createContext();
export const useAuth0 = () => useContext(Auth0Context);
export const Auth0Provider = ({
children,
onRedirectCallback = DEFAULT_REDIRECT_CALLBACK,
...initOptions
}) => {
const [isAuthenticated, setIsAuthenticated] = useState();
const [user, setUser] = useState();
const [auth0Client, setAuth0] = useState();
const [loading, setLoading] = useState(true);
const [popupOpen, setPopupOpen] = useState(false);

useEffect(() => {
const initAuth0 = async () => {
const auth0FromHook = await createAuth0Client(initOptions);
setAuth0(auth0FromHook);

if (window.location.search.includes("code=")) {
const { appState } = await auth0FromHook.handleRedirectCallback();
onRedirectCallback(appState);
}

const isAuthenticated = await auth0FromHook.isAuthenticated();

setIsAuthenticated(isAuthenticated);

if (isAuthenticated) {
const user = await auth0FromHook.getUser();
setUser(user);
}

setLoading(false);
};
initAuth0();
// eslint-disable-next-line
}, []);

const loginWithPopup = async (params = {}) => {
setPopupOpen(true);
try {
await auth0Client.loginWithPopup(params);
} catch (error) {
console.error(error);
} finally {
setPopupOpen(false);
}
const user = await auth0Client.getUser();
setUser(user);
setIsAuthenticated(true);
};

const handleRedirectCallback = async () => {
setLoading(true);
await auth0Client.handleRedirectCallback();
const user = await auth0Client.getUser();
setLoading(false);
setIsAuthenticated(true);
setUser(user);
};
return (
<Auth0Context.Provider
value={{
isAuthenticated,
user,
loading,
popupOpen,
loginWithPopup,
handleRedirectCallback,
getIdTokenClaims: (...p) => auth0Client.getIdTokenClaims(...p),
loginWithRedirect: (...p) => auth0Client.loginWithRedirect(...p),
getTokenSilently: (...p) => auth0Client.getTokenSilently(...p),
getTokenWithPopup: (...p) => auth0Client.getTokenWithPopup(...p),
logout: (...p) => auth0Client.logout(...p)
}}
>
{children}
</Auth0Context.Provider>
);
};

0 comments on commit 7b3e3a0

Please sign in to comment.
You can’t perform that action at this time.