Skip to content

Commit

Permalink
Adds configurable redirect path after login
Browse files Browse the repository at this point in the history
  • Loading branch information
gousta committed May 1, 2024
1 parent 3dd8046 commit 9e17187
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/components/SignInButton/SignInButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ import Button from 'payload/dist/admin/components/elements/Button';
import React from 'react';

export default function SignInButton() {
let signInUrl = '/oidc/signin';

try {
const redirectUrl = new URLSearchParams(window.location.search).get('redirect');
if (redirectUrl) {
signInUrl = `${signInUrl}?redirect=${redirectUrl}`;
}
} catch (e) {
console.error('Failed to read `redirect` query parameter from the url', e);
}

return (
<div style={{ width: '100%', textAlign: 'center' }}>
<h4 style={{ marginBottom: 0 }}>Deliverback Content</h4>

<Button className="SignInButton" el="anchor" url="/oidc/signin">
<Button className="SignInButton" el="anchor" url={signInUrl}>
Sign in with your identity
</Button>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/lib/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import getCookieExpiration from 'payload/dist/utilities/getCookieExpiration';
import { PayloadRequest, SanitizedCollectionConfig } from 'payload/types';

export const loginHandler =
(userCollectionSlug: string) => async (req: PayloadRequest, res: Response) => {
(userCollectionSlug: string, redirectPathAfterLogin: string) =>
async (req: PayloadRequest, res: Response) => {
// Get the Mongoose user
const collectionConfig = payload.collections[userCollectionSlug].config;

Expand All @@ -33,7 +34,7 @@ export const loginHandler =
});

// Redirect to admin dashboard
return res.redirect('/admin');
return res.redirect(redirectPathAfterLogin);
};

const getFieldsToSign = (collectionConfig: SanitizedCollectionConfig, user: any) => {
Expand Down
12 changes: 6 additions & 6 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export const oidcPlugin =
(opts: oidcPluginOptions) =>
(incomingConfig: Config): Config => {
let config = { ...incomingConfig };
const buttonComponentPosition = opts.components?.position ?? "beforeLogin";
const buttonComponentPosition = opts.components?.position ?? 'beforeLogin';
let componentConfigs = config.admin?.components?.beforeLogin || [];

if(buttonComponentPosition == "afterLogin"){
if (buttonComponentPosition == 'afterLogin') {
componentConfigs = config.admin?.components?.afterLogin || [];
}

Expand All @@ -33,9 +33,9 @@ export const oidcPlugin =
...(config.admin?.components || {}),
[buttonComponentPosition]: [
...(componentConfigs || []),
opts.components?.Button ?? SignInButton
]
}
opts.components?.Button ?? SignInButton,
],
},
};

if (isUI) return config;
Expand Down Expand Up @@ -75,7 +75,7 @@ export const oidcPlugin =
path: callbackPath,
method: 'get',
root: true,
handler: loginHandler(userCollectionSlug),
handler: loginHandler(userCollectionSlug, opts.redirectPathAfterLogin || '/admin'),
},
];

Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export interface oidcPluginOptions extends StrategyOptions {
/** Which path to mount for the callback endpoint in express, defaults to the path in callbackURL */
callbackPath?: string;

/** Path to redirect to after login, defaults to `/admin` */
redirectPathAfterLogin?: string;

/** Override button component */
components?: {
Button?: ComponentType<any>;
Expand Down

0 comments on commit 9e17187

Please sign in to comment.