Skip to content

Commit

Permalink
Merge pull request #875 from exogee-technology/feat/login-with-redirect
Browse files Browse the repository at this point in the history
Auth0 Login with Redirect
  • Loading branch information
steve-keep authored Jul 16, 2024
2 parents bc03d6a + 8a1bd89 commit 64e3446
Show file tree
Hide file tree
Showing 33 changed files with 132 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/examples/auth-zero/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-auth-zero",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of connecting a SQLite database with Auth0.",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/auth-zero/src/admin-ui/custom-pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Auth, Auth0, Auth0Logout } from '@exogee/graphweaver-auth-ui-components';

export const customPages = {
sidebarFooter: () => <Auth0Logout />,
sidebarFooter: () => <Auth0Logout redirectTo={`${window.location.origin}/auth/login`} />,
routes: () => [
{
path: '/auth',
Expand Down
2 changes: 1 addition & 1 deletion src/examples/aws-cognito/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@exogee/graphweaver-example-aws-cognito",
"private": true,
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"description": "Example of connecting to AWS Cognito",
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/databases/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-databases",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of connecting a MySQL and PostgreSQL database together.",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/federation/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@exogee/graphweaver-example-federation",
"private": true,
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"description": "Example app used to test federation compatibility",
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-rest",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of connecting a Rest API with a MySQL database.",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/s3-storage/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-s3-storage",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of connecting Graphweaver to an AWS S3 bucket.",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/sqlite/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-sqlite",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of connecting a SQLite database.",
Expand Down
2 changes: 1 addition & 1 deletion src/examples/xero/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-example-xero",
"version": "2.0.3",
"version": "2.0.4",
"license": "MIT",
"private": true,
"description": "Example of using @exogee/graphweaver to connect two Xero instances",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/admin-ui-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-admin-ui-components",
"version": "2.0.3",
"version": "2.0.4",
"description": "Components from Graphweaver's admin UI which you can use in your projects as you like",
"license": "MIT",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/admin-ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-admin-ui",
"version": "2.0.3",
"version": "2.0.4",
"type": "module",
"main": "dist/main.js",
"types": "src/main.tsx",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/apollo-client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-apollo-client",
"version": "2.0.3",
"version": "2.0.4",
"description": "Useful helpers for working with Apollo Client and Graphweaver",
"license": "MIT",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/auth-ui-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-auth-ui-components",
"version": "2.0.3",
"version": "2.0.4",
"description": "Components from Graphweaver's Auth UI which you can use in your projects as you like",
"license": "MIT",
"type": "module",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,72 @@
import { useCallback, useEffect, useRef, useState } from 'react';
import { Button } from '@exogee/graphweaver-admin-ui-components';
import { useNavigate } from 'react-router-dom';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { getAuth0Client } from '../client';

export const Auth0 = () => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | undefined>();

const [searchParams] = useSearchParams();
const shouldRedirect = useRef(true);
const navigate = useNavigate();

// In this effect we are checking if the user is coming back from the Auth0 login page or
// if the user is coming to the page for the first time. If the user is coming back from
// the Auth0 login page, we process the redirect. If the user is coming to the page for the
// first time, we redirect the user to the Auth0 login page.
useEffect(() => {
if (shouldRedirect.current) {
shouldRedirect.current = false;
requestLogin();
const code = searchParams.get('code');
const state = searchParams.get('state');

if (code && state) {
// The user is coming back from the Auth0 login page
processRedirect();
} else {
// The user is coming to the page for the first time
requestLogin();
}
}
}, []);

useEffect(() => {
if (!loading && !error) {
navigate('/');
// This function is called when the user clicks the login button or when the user is coming to the page for the first time
const requestLogin = useCallback(async () => {
try {
const client = await getAuth0Client();
const options = {
authorizationParams: {
redirect_uri: window.location.toString(),
},
};
await client.loginWithRedirect(options);
} catch (e: any) {
if (e.message) setError(e.message);
setLoading(false);
}
}, [loading, error]);
}, []);

const requestLogin = useCallback(async () => {
// This function is called when the user is coming back from the Auth0 login page and we need to process the redirect
// This function will handle the redirect and redirect the user to the home page
const processRedirect = useCallback(async () => {
try {
const client = await getAuth0Client();
await client.loginWithPopup();
await client.handleRedirectCallback();
navigate('/');
} catch (e: any) {
if (e.message) setError(e.message);
} finally {
setLoading(false);
}
}, []);

const handleRetry = () => {
requestLogin();
};

if (loading) return <div>Loading...</div>;

return (
<div>
{error && <div>{error}</div>}
<Button onClick={handleRetry}>Retry</Button>
<Button onClick={requestLogin}>Login</Button>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Logout } from '../../logout';
import { getAuth0Client } from '../client';

export const Auth0Logout = () => {
type Auth0LogoutProps = {
redirectTo?: string;
};

export const Auth0Logout = ({ redirectTo }: Auth0LogoutProps) => {
const handleLogout = async () => {
const client = await getAuth0Client();
await client.logout();
await client.logout({
logoutParams: { returnTo: redirectTo ?? window.location.origin },
});
};

return <Logout onLogout={handleLogout} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ export const Logout = ({ onLogout }: LogoutProps) => {

const handleOnLogout = async () => {
try {
if (onLogout) await onLogout();
localStorage.removeItem(localStorageAuthKey);
await apolloClient.clearStore();
await apolloClient.resetStore();
navigate(0);
if (onLogout) {
await onLogout();
} else {
navigate(0);
}
} catch (error: any) {
const message = error?.message || 'Unknown error.';
toast.error(`Failed to logout. Please try again. Error: ${message}`, {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-auth",
"version": "2.0.3",
"version": "2.0.4",
"description": "Row-Level Security support for @exogee/graphweaver",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/aws-cognito/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-aws",
"version": "2.0.3",
"version": "2.0.4",
"main": "lib/index.js",
"source": "src/index.ts",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-builder",
"version": "2.0.3",
"version": "2.0.4",
"description": "A tool for building and running Graphweaver projects",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphweaver",
"version": "2.0.3",
"version": "2.0.4",
"description": "A tool for managing, running, debugging and building Graphweaver projects",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-config",
"version": "2.0.3",
"version": "2.0.4",
"description": "Retrieve and parse Graphweaver configurations",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver",
"version": "2.0.3",
"version": "2.0.4",
"description": "Graphweaver Core Package",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/end-to-end/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-end-to-end",
"version": "2.0.3",
"version": "2.0.4",
"description": "Graphweaver Test Suite",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/logger",
"version": "2.0.3",
"version": "2.0.4",
"description": "Common logging output for Exogee projects",
"license": "MIT",
"directories": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/mikro-orm-sqlite-wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mikro-orm-sqlite-wasm",
"version": "2.0.3",
"version": "2.0.4",
"description": "MikroORM SQLite Driver Wasm",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/mikroorm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-mikroorm",
"version": "2.0.3",
"version": "2.0.4",
"description": "MikroORM backend for @exogee/graphweaver",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/rest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-rest",
"version": "2.0.3",
"version": "2.0.4",
"description": "RESTful backend for @exogee/graphweaver",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/scalars/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-scalars",
"version": "2.0.3",
"version": "2.0.4",
"description": "Common scalar types for use with @exogee/graphweaver",
"license": "MIT",
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-server",
"version": "2.0.3",
"version": "2.0.4",
"description": "Server support for @exogee/graphweaver",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/storage-provider/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-storage-provider",
"version": "2.0.3",
"version": "2.0.4",
"description": "Graphweaver Storage Provider Package",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/vite-plugin-graphweaver/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-graphweaver",
"version": "2.0.3",
"version": "2.0.4",
"description": "A vite plugin for use with @exogee/graphweaver's admin UI",
"license": "MIT",
"main": "lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/packages/xero/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@exogee/graphweaver-xero",
"version": "2.0.3",
"version": "2.0.4",
"description": "Xero backend for @exogee/graphweaver",
"license": "MIT",
"scripts": {
Expand Down
Loading

0 comments on commit 64e3446

Please sign in to comment.